Using
read() to slurp up an entire file is kinda
silly.
read() is more useful when reading in a
very large file in small chunks. Since you are reading in
the entire file, storing the contents into RAM, you might
as well just slurp the file into a scalar. If this is all
that your code is doing ... converting the contents of a
file to an array then shouldn't it be named
file2array or similar? Also,
die()ing in
the middle of a subroutine like that is not nice to the
client who uses your code.
Here is slightly different version that returns a scalar
instead of an array.
sub file2scalar {
my $filename = shift;
return "Error: $filename doesn't exist" unless -f $filename;
open FH, $filename || return "Error: can't read $filename";
return do{local $/; <FH>};
}
When you call this sub:
my $include = file2scalar('foo.html');
$include will either contain the contents of the
file or an error message. Instead of differentiating between
the two, i recommend just printing
$include to the
browers, much like PHP does when you give it's
include() function a file that doesn't exist or
can't be read. (of course, my
real recommendation is
to just go ahead and install
CGI::SSI instead)
jeffa
L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.