Charlie somerville wrote:
Yeah, that looks pretty good
"Alexander Blüm" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]

that doesn't sound too good. sounds like loading all into memory, split
it up into arrays and then finally printing the contents to the browser.
bleh!

how aout this:

#somewhere in the code:
&include("/includes/header.html");
...
&include("/includes/footer.html");

### subroutine
sub include{
 my $filename = shift;
 open(FH, "<$filename") || die "cannot open $filename: $!\n";
 while (<FH>){
   print $_;
 }
 close FH;
}


Drop the ampersands, they aren't needed in this context... from perldoc perlsub:


"Subroutines may be called recursively. If a subroutine is called using the "&" form, the argument list is optional, and if omitted, no @_ array is set up for the subroutine: the @_ array at the time of the call is visible to subroutine instead. This is an efficiency mechanism that new users may wish to avoid."

Also remember that your CGI script has no way to understand your relative paths, so /includes is at the root of the server *NOT* the root of your web document tree, so you will need relative paths or full filesystem paths, unless you really do have an 'includes' directory off of /, which I suppose is possible in a chroot jail or the like (though isn't all that common).

If you are going to use $_ then you might as well drop it from the print, it is implicit.

perldoc -f print
"If LIST is also omitted, prints $_ to the currently selected output channel."


Personally I prefer to avoid using $_ altogether, but this is probably the simplest scenario where it makes sense, but if you are going to use it, use it all the way.

And you may want to either not 'die' in your include sub or wrap the whole call in an eval, at this point you have to have printed a header, and if you die because of a failed open (missing file, permissions, or many other reasons) then you will be sending back a malformed document that possibly (probably?) won't render properly, if at all, your user rather than getting a silly error may be staring at a completely blank screen.

eval { include('../htdocs/includes/header.html'); };
if ($@) {
   print "<!--Failed to include file: [EMAIL PROTECTED]>";
}

perldoc -f eval

http://danconia.org

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to