PDA

View Full Version : when one sub prints, the rest dont. why?


bazz
02-16-2005, 06:33 PM
below is the code. When I add the sub put_header containg the line print "Location: $Header"; the rest of the subs stop printing so that my page only has the header in it.

if I remove the header the content prints out, however, if I manage to get it to print the sub put_footer, I also lose the content. What thing stupid am I doing wrong?


#!/usr/bin/perl -w
#


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

$MenuName= $Path;
$MenuName=~ s/\_/ /g; # my changes to try to entitle the menu
if ($Path eq "") {
$Path = "ALL";
}

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

$Directory = $Request;

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

#That splits to give vars like these?

#$vars2 = cgi-bin
#$vars3 = ClientName
#$vars4 = MenuType
#$vars5 = file.pl

#################################
#establish incoming domain and split to get name
###############################

$Domain = $Referer;

chomp ($Domain);
($ref1,$ref2,$ref3) = split(/\//,$Domain);

#That splits to give vars like these?
#$ref3 = cms.thechrissystem.com
##################################
#split domain name into it components
##################################

chomp ($ref3);
($dom1,$dom2,$dom3) = split(/\./,$ref3);

#$dom1 = cms
#$dom2 = thechrissystem
#$dom3 = com

###################################
#Set headers and footers
###################################
$Header = "/headers_and_footers/$vars3/header.htm";
$Footer = "/headers_and_footers/$vars3/footer.htm";

#$client = $dom2;
#if (-e "/headers_and_footers/$client/header.htm")

#{
#$Header = "/headers_and_footers/$client/header.htm";
#$Footer = "/headers_and_footers/$client/footer.htm";
#}
#else
#{

#sub put_headers {
#
#printContent-type: text/html;
#print "file not available";

#}
#}
###############################
#END OF MY AMENDMENT
###############################
#$Files = `ls $Root/cgi-bin/a_default/Meal_Menus/files/$Path/*`;
#$Files = `ls $Root/$vars2/$vars3/$vars4/files/$Path/*`;

$Action = "View";
$Fdir = "$Root/$vars2/$vars3/$vars4/files/ALL";
$Base = "NOFILE";

#
# Call processing functions
#
&get_posts;

&put_header;
&open_file;
&put_file;
&close_file;
&put_trailer;

&put_footer;
#
# Retrieve postings
#
sub get_posts {
if ($Method eq "POST") {
while (<>) {
@Posts = split(/&/);
&parse_posts;
}
} else {
$Query = $Request;
$Query =~ s/\/cgi-bin\/$vars3\/$vars4\/publicdetail.pl\///;
@Posts = split(/&/,$Query);
&parse_posts;
$FDir="$Root/cgi-bin/$vars3/$vars4/files/$Path";
$File="$FDir/$Base";
}
}

#
# Parse postings
#
sub parse_posts {
foreach $PostItem (@Posts) {
($PostKey,$PostVal) = split(/=/,$PostItem);
$PostVal =~ s/\+/_/g;
$PostVal =~ s/%(..)/pack("c",hex($1))/ge;
$PostVal =~ s/\n//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;
} elsif ($PostKey eq "path") {
$Path = $PostVal;
}
}
}


sub put_header {

print "Location: $Header\n\n";

}
#
# Open the data file
#
sub open_file {
if (!open (DAT,"< $File")) {
print "<H3>This file does not exist!</H3>\n";
&put_trailer;
exit;
}
}

#
# Display the file
#
sub put_file {

#
# Set defaults
#
$Date = "00/00/1900";
$Price = "";
$Availability = "";
$Title = "Unknown";
$Reference = "No reference";
$Summary = "";
$Body = "";

#
# Extract all data from the file
#
while (($Line = <DAT>)) {
chop $Line;
($Keyword,$Value) = split(/:/,$Line);
if ($Keyword eq "Date") {
$Date = $Value;
} elsif ($Keyword eq "Title") {
$Title = $Value;
} elsif ($Keyword eq "Availability") {
$Availability = $Value;
} elsif ($Keyword eq "Price") {
$Price = $Value;
} elsif ($Keyword eq "Reference") {
$Reference = $Value;
} elsif ($Keyword eq "Summary") {
$Summary = $Summary . $Value . "<BR>\n";
} elsif ($Keyword eq "Body") {
$Body = $Body . $Value . "<BR>\n";
}
}
chop $Summary;
chop $Body;

#
# Generate the item table
#
print "<table class=table>\n";
print "<tr><td class=headings_td>Name</td><td class=title_td>$Title</td><td class=price_td>$Price</td></tr>\n";
print "<tr><td class=headings_td>Description</td><td class=normal_td>$Summary</td><td class=normal_td></td></tr>\n";
print "<tr><td class=headings_td>Additional Info</td><td class=normal_td>$Body</td><td class=normal_td></td></tr>\n";

#
# Close the item table
#
print "</table>\n";
}

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

#
# Print the trailer block
#
sub put_trailer {
print <<ENDPR;

<a href="/$vars2/$vars3/$vars4/publicindex.pl/$Path">back to menu</a>
</BODY>
</HTML>
ENDPR
}



sub put_footer {

print "Location: $Footer\n\n";

}

#
# 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 "Method = $Method\n";
print "Query = $Query\n";
print "File = $File\n";
print "Fdir = $Fdir\n";
print "Base = $Base\n";
print "</PRE>\n";
print "<HR>\n";
}




Bazz

mlseim
02-16-2005, 09:12 PM
Bazz...

I think: print "Location: $Header\n\n";
actually ends your script.

What you're doing is really hard to see from our perspective,
and not knowing what the $Header file is (HTML?) ...

Have you thought about opening the $Header file and reading
in the text lines one by one, printing them to the browser as
they are read?

bazz
02-17-2005, 03:04 PM
Hmmm, I tried (but failed), to see it from your perpsective. Working too long on this whole project perhaps. :eek:

The content to be included must be done on the fly, because it is deterimed by incoming url info. ie, if you conme to the script form url1, header1.html and footer1.html will display and if coming from url2, then header2 and footer2 display.

The header and footer files are plain html though I can try to change this if needs be.

I shall be enabling numerous sites to access this perl script which is interactive and so cannot function if the clients were to include it in an html or php page. (Effectively this script can create at least (for my application), about 100 pages, so as far as my limited understanding goes, it has to be maintained as a perl file).

If you imagine that the script is to appear in a specific part of a clients web page. (In the case of the Demo (http://cms.thechrissystem.com/cgi-bin/1/Meal_Menus/publicdetail2.pl/path=Tabledhote&action=View&base=item01), it shows in the <DIV id="text"> part. So I thought that if I took the chunk of their html up to that div start tag, and called it header.html then; did the same with their html after that div start tag, and call it the footer.html I could include it in the perl script).

Otherwise I can hard code the html inside print qq( ) but that would seemingly enable it to match only single site however, scriptwise, it does work that way.

I'll check back an see if I included the code. Yup the code is in my first post. I know it is long so to make it easier, if I can, focus plz on the sub put_header. and the sub below it. I only gave you the rest of the code to help understanding though I could give the cms to anyone who can solve this and one later query which I am trying begin but which is being held up by this problem.

Bazz

mlseim
02-17-2005, 03:32 PM
bazz...

email me a copy of a customer's "header.html" file
and "footer.html"

I'm thinking of a subroutine to "build" the webpage
on the fly.

--max--