PDA

View Full Version : my $incs=new includes; busts the functioning of my script.


bazz
03-22-2005, 09:17 PM
the attached code works perfectly, if I omit the code at line 8. But I have other similar scripts in the same DIR which use this code and work as expected. It's just this one that is messin' about.

Can anyone shed light on the reason why and help to fix it?

The logs show no reason but my code (which normally says "The file has been saved an added to the database), instead says,
"This file cannot be saved!<BR>please alert the system administrator".

Sorry that I can't give you more.



BTW the purpose of the code before the #####################line, is to insert two files which are being called from the specific_control_panel.pm They effectively put website appearance into the outpuuted page. Those two files are the html header and footer, which form the remainder of the page.

Bazz



#!/usr/bin/perl -w
use CGI 'param';

use lib 'relative/path/to/perl/';

require '/domains/574/1617/html/cgi-bin/specific_control_panel.pm';
print 'Content-type: text/html'."\n\n";
my $incs=new includes; #i THINK THAT THIS IS THE LINE THAT CAUSES THE TROUBLE

######################################

# Print the HTML content header; set global names
#
$Root = $ENV{"DOCUMENT_ROOT"};
$Path = $ENV{"PATH_INFO"};
$Referer = $ENV{"HTTP_REFERER"};
$Request = $ENV{"REQUEST_URI"};
$Path =~ s/[^a-zA-Z0-9\.\_]//g;

#################################
#MY AMENDMENT
###############################

$Directory = $Request;

chomp ($Directory);
($vars2,$vars3,$vars4) = split(/\//,$Directory);

#That splits to give vars like these?

#$vars1 = cgi-bin
#$vars2 = ClientName
#$vars3 = whether admin folder or not
#$vars4 = MenuType
#$vars5 = file.pl
#$vars6 = specific menu
###############################
#END OF MY AMENDMENT
###############################


if ($Path eq "") {
$Path = "ALL";
}
$Action = "Save";
#$Fdir = "$Root/FILES/$vars3/ALL";

$Fdir = "$Root/FILES/$vars3/ALL";

$Base = "";

#
# Call processing functions
#
&get_posts;
&put_header;
if ($Action eq "Save") {
&open_file;
&save_file;
&close_file;
} elsif ($Action eq "Delete") {
&delete_file;
}
&put_trailer;

#
# Retrieve postings
#
sub get_posts {
while (<>) {
@Posts = split(/&/);
foreach $PostItem (@Posts) {
($PostKey,$PostVal) = split(/=/,$PostItem);
$PostVal =~ s/\+/ /g;
$PostVal =~ s/%(..)/pack("c",hex($1))/ge;
$PostVal =~ s/:/;/g;
if ($PostKey eq "action") {
$Action = $PostVal;
} elsif ($PostKey eq "file") {
$File = $PostVal;
$File =~ s/ /_/g;
} elsif ($PostKey eq "fdir") {
$Fdir = $PostVal;
$Fdir =~ s/ /_/g;
} elsif ($PostKey eq "base") {
$Base = $PostVal;
$Base =~ s/ /_/g;
} elsif ($PostKey eq "title") {
$Title = $PostVal;
$Title =~ s/\r\n/ /g;
$Title =~ s/\n/ /g;
$Title =~ s/ *$//g;
} elsif ($PostKey eq "price") {
$Price = $PostVal;
$Price =~ s/\r\n/ /g;
$Price =~ s/\n/ /g;
$Price =~ s/ *$//g;
} elsif ($PostKey eq "availability") {
$Availability = $PostVal;
$Availability =~ s/\r\n/ /g;
$Availability =~ s/\n/ /g;
$Availability =~ s/ *$//g;
} elsif ($PostKey eq "date") {
$Date = $PostVal;
$Date =~ s/ *$//g;
} elsif ($PostKey eq "day") {
$Day = $PostVal;
$Day =~ s/ *$//g;
} elsif ($PostKey eq "month") {
$Month = $PostVal;
$Month =~ s/ *$//g;
} elsif ($PostKey eq "year") {
$Year = $PostVal;
$Year =~ s/ *$//g;
} elsif ($PostKey eq "reference") {
$Reference = $PostVal;
$Reference =~ s/\r\n/ /g;
$Reference =~ s/\n/ /g;
$Reference =~ s/ *$//g;
} elsif ($PostKey eq "summary") {
$Summary = $PostVal;
$Summary =~ s/\r//g;
} elsif ($PostKey eq "body") {
$Body = $PostVal;
$Body =~ s/\r//g;
}
}
}
}

#
# Print the header block
#



sub put_header {
#print $incs->header;

print <<ENDPR;


<br /><br /><br /><br /><br /><br />



ENDPR
}

#
# Open the data file
#
sub open_file {
if (!open (DAT,"> $File")) {
print "<FONT SIZE = -1><B>This file cannot be saved!<BR>please alert the system administrator.\n";
&put_trailer;
exit;
}
}

#
# Save the file
#
sub save_file {
print DAT "Date:$Day/$Month/$Year\n";
print DAT "Reference:$Reference\n";
print DAT "Availability:$Availability\n";
print DAT "Title:$Title\n";
@Summary = split(/\n/,$Summary);
foreach $Line (@Summary) {
$Line =~ s/<BR>//g;
print DAT "Summary:$Line\n";
}
@Body = split(/\n/,$Body);
foreach $Line (@Body) {
$Line =~ s/<BR>//g;
print DAT "Body:$Line\n";
}
print "<FONT SIZE = -1><B>This file has been saved<BR>and added to the Database.\n";
}

#
# Close the data file
#
sub close_file {
close DAT;
}

#
# Zap the file
#
sub delete_file {
if (unlink $File) {
print "<FONT SIZE = -1><B>This file has been deleted<BR>and removed from the Database.\n";
} else {
print "<FONT SIZE = -1><B>This file cannot be deleted.<BR>please alert the system administrator.\n";
}
}

#
# Print the trailer block
#
sub put_trailer {
print <<ENDPR;
<br /><br /><br /><br /><a href="/$vars2/$vars3/$vars4/HTMLbuilddb.pl/$Path">Back
to Database index</a>


ENDPR

}



#
# Display key data
#
sub put_keys {
print "<PRE>";
print "Root = $Root\n";
print "Path = $Path\n";
print "Referer = $Referer\n";
print "Request = $Request\n";
print "File = $File\n";
print "Fdir = $Fdir\n";
print "Base = $Base\n";
print "Date = $Date\n";
print "Title = $Availability\n";
print "Title = $Title\n";
print "Price = $Price\n";
print "Reference = $Reference\n";
print "Summary = $Summary\n";
print "Body = $Body\n";
print "</PRE>\n";
print "<HR>\n";
}

mlseim
03-22-2005, 09:46 PM
Bazz,

my $incs=new includes;

what is: new includes ?

I know this won't cause an actual script failure,
but I'm a bit baffled why is makes your $File
variable bad.

Try printing out $File before you open it, to see
what it contains.

bazz
03-22-2005, 10:12 PM
Ah,, If I print $File without the includes bit added, I get this

/domains/574/1617/html/FILES/thechrissystem/homepage/homepage02

If I try it with the inlcudes 'in', I get nothing.

Any ideas? plz. I am baffled.

Hmmm.

I took a look at the includes file. Here it is. below Is it possible that $file conflicts with $File?



package includes;
use strict;
use CGI 'param';
# Header & footer module, includes.pm:

sub new(){
my $self=shift;
my($host)=(param('host'))?(param('host')):($ENV{'HTTP_REFERER'}=~m#//([^/]+)#);
my %sites=(
'cms.mydomain.com'=>['../../../headers_and_footers/cmsthechrissystem/header.html','../../../headers_and_footers/cmsthechrissystem/footer.html','../../../headers_and_footers/cmsthechrissystem/leftdiv.html','../../../control_panels/admin_and_general.html'],

);
my @files=((exists $sites{$host})?(@{$sites{$host}}):('../../../headers_and_footers/cmsthechrissystem/header.html','../../../headers_and_footers/cmsthechrissystem/footer.html','../../../headers_and_footers/cmsthechrissystem/leftdiv.html','../../../control_panels/admin_and_general.html'));
bless {'host'=>$host,'files'=>\@files},$self;
}

sub hostname(){
return $_[0]->{'host'};
}

sub header(){
return showfile($_[0]->{'files'}->[0]);
}

sub footer(){
return showfile($_[0]->{'files'}->[1]);
}

sub leftdiv(){
return showfile($_[0]->{'files'}->[2]);
}
sub admin (){
return showfile($_[0]->{'files'}->[3]);
}
sub showfile{
local $/;
print 'This is you\'re problem here' unless -e $_[0];
open(my $file,$_[0]) or die "Can\'t open".$!;
my $content=<$file>;
close($file);
return $content;
}

1;




Bazz

mlseim
03-22-2005, 11:31 PM
I would guess that $file and $File are two different variables,
but you could just change $File to $Filefile just to test it out.

How do you know what this line does?
my $incs=new includes;

Did you read a tutorial or something on assigning it to a variable?

Strange syntax, I've never used before.

Can you explain how the variable assignment is supposed to work?

bazz
03-23-2005, 12:44 PM
OK, my $incs=new includes; is normally used near the top of the script like this:


#!/usr/bin/perl -w
use CGI 'param';

use lib 'relative/path/to/perl/';

require '/domains/574/1617/html/cgi-bin/specific_control_panel.pm';
print 'Content-type: text/html'."\n\n";
my $incs=new includes;


Then later on, I use the line: ' print $incs->header; '

This pulls the header info from the .pm file so that the HTML file of the header is placed, where the 'print' line is put.

Since your last post, Max (if your reading), I have found that if I put print $incs->header; after this next line of code, it is OK, except that the header appears lower down the html page than it should (after the output from the script).

If I put it before this part of script then there seems to be a conflict as the code then drops the file name and cannot then save the details.

I thought for a second that the word 'file' might be the problem but I recall, from last night, that I changed the word 'file' and the problem persisted.

So I'm pretty much out of ideas; yet so close to having all my pages use only one header and footer.

Bazz
here's the code that seems to confilct with the .pm which I posted earlier.

sub get_posts {
while (<>) {
@Posts = split(/&/);
foreach $PostItem (@Posts) {
($PostKey,$PostVal) = split(/=/,$PostItem);
$PostVal =~ s/\+/ /g;
$PostVal =~ s/%(..)/pack("c",hex($1))/ge;
$PostVal =~ s/:/;/g;
if ($PostKey eq "action") {
$Action = $PostVal;
} elsif ($PostKey eq "file") {
$File = $PostVal;
$File =~ s/ /_/g;
} elsif ($PostKey eq "fdir") {
$Fdir = $PostVal;
$Fdir =~ s/ /_/g;
} elsif ($PostKey eq "base") {
$Base = $PostVal;
$Base =~ s/ /_/g;
} elsif ($PostKey eq "title") {
$Title = $PostVal;
$Title =~ s/\r\n/ /g;
$Title =~ s/\n/ /g;
$Title =~ s/ *$//g;
} elsif ($PostKey eq "price") {
$Price = $PostVal;
$Price =~ s/\r\n/ /g;
$Price =~ s/\n/ /g;
$Price =~ s/ *$//g;
} elsif ($PostKey eq "availability") {
$Availability = $PostVal;
$Availability =~ s/\r\n/ /g;
$Availability =~ s/\n/ /g;
$Availability =~ s/ *$//g;
} elsif ($PostKey eq "date") {
$Date = $PostVal;
$Date =~ s/ *$//g;
} elsif ($PostKey eq "day") {
$Day = $PostVal;
$Day =~ s/ *$//g;
} elsif ($PostKey eq "month") {
$Month = $PostVal;
$Month =~ s/ *$//g;
} elsif ($PostKey eq "year") {
$Year = $PostVal;
$Year =~ s/ *$//g;
} elsif ($PostKey eq "reference") {
$Reference = $PostVal;
$Reference =~ s/\r\n/ /g;
$Reference =~ s/\n/ /g;
$Reference =~ s/ *$//g;
} elsif ($PostKey eq "summary") {
$Summary = $PostVal;
$Summary =~ s/\r//g;
} elsif ($PostKey eq "body") {
$Body = $PostVal;
$Body =~ s/\r//g;
}
}
}
}

bazz
03-23-2005, 01:06 PM
YAY!!! how simple it was after all.

As a complete guess, I put the two lines of code into the last-posted script just before the last '}' like this

sub get_posts {
while (<>) {
@Posts = split(/&/);
foreach $PostItem (@Posts) {
($PostKey,$PostVal) = split(/=/,$PostItem);
$PostVal =~ s/\+/ /g;
$PostVal =~ s/%(..)/pack("c",hex($1))/ge;
$PostVal =~ s/:/;/g;
if ($PostKey eq "action") {
$Action = $PostVal;
} elsif ($PostKey eq "file") {
$File = $PostVal;
$File =~ s/ /_/g;
} elsif ($PostKey eq "fdir") {
$Fdir = $PostVal;
$Fdir =~ s/ /_/g;
} elsif ($PostKey eq "base") {
$Base = $PostVal;
$Base =~ s/ /_/g;
} elsif ($PostKey eq "title") {
$Title = $PostVal;
$Title =~ s/\r\n/ /g;
$Title =~ s/\n/ /g;
$Title =~ s/ *$//g;
} elsif ($PostKey eq "price") {
$Price = $PostVal;
$Price =~ s/\r\n/ /g;
$Price =~ s/\n/ /g;
$Price =~ s/ *$//g;
} elsif ($PostKey eq "availability") {
$Availability = $PostVal;
$Availability =~ s/\r\n/ /g;
$Availability =~ s/\n/ /g;
$Availability =~ s/ *$//g;
} elsif ($PostKey eq "date") {
$Date = $PostVal;
$Date =~ s/ *$//g;
} elsif ($PostKey eq "day") {
$Day = $PostVal;
$Day =~ s/ *$//g;
} elsif ($PostKey eq "month") {
$Month = $PostVal;
$Month =~ s/ *$//g;
} elsif ($PostKey eq "year") {
$Year = $PostVal;
$Year =~ s/ *$//g;
} elsif ($PostKey eq "reference") {
$Reference = $PostVal;
$Reference =~ s/\r\n/ /g;
$Reference =~ s/\n/ /g;
$Reference =~ s/ *$//g;
} elsif ($PostKey eq "summary") {
$Summary = $PostVal;
$Summary =~ s/\r//g;
} elsif ($PostKey eq "body") {
$Body = $PostVal;
$Body =~ s/\r//g;
}
}
}my $incs=new includes;
print $incs->header;
}



Suddenly I was jumping for joy (But then I remembered: I dunno anyone called Joy) :D

Bazz

mlseim
03-23-2005, 01:45 PM
Bazz ...

Wow ...

If anything ever happens to you, your scripts will be
very safe. Nobody will ever be able to figure out
what you've done. I can even hear the Perl compiler
saying, "Oh no, now he wants me to do what?" :p

(and you're getting good at troubleshooting too).

--max--