My quick & dirty getfile subroutine. And a one liner too, courtesy of Merlyn (with some modifications) ... not sure which would be faster.
Added one after reading Best Practices. I don't know how fast it would be, or if it would be faster. I haven't even syntax checked this. I just wanted to get this down during a quick stop.
Minor fix to the last sample
sub getfile {
my $filename = shift;
open my $FH, "<$filename" or die "Unable to open $filename: $!";
return wantarray ? map /(.*)/, <$FH> : do { local $/ ; <$FH> };
}
sub getfile { local *ARGV ; @ARGV = shift ; wantarray ? map /(.*)/, <>
+ : do { local $/ ; <> } }
sub getfile {
my $filename = shift;
open my $FH, "<$filename" or die "Unable to open $filename: $!";
sysread $FH, my $contents, -s $FH;
return wantarray ? split m{$/}, $contents : $contents;
}
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.