in reply to slurp
The main problem with this (aside from the fact that it's not complete) is that you've set the input record separator ($/) incorrectly. Setting it to "" tells Perl to read in paragraphs at a time, not the whole file.
To read in the entire file, set $/ to undef. So your sub should look something like this:
Your other problem, which I've fixed, is that you were using $_, not $_[0].sub slurp { local $/ = undef; local *X; open X, $_[0] or die "Can't open $_[0]: $!"; my $slurp = <X>; close X or die "Can't close $_[0]: $!"; $slurp; }
The code should be used like this:
The sub will die when it can't open or close the file, so you may want to change that; but you should always check for errors, no matter what.my $contents = slurp("/home/foo/bar.txt");
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: RE: slurp
by Vane (Novice) on Jun 19, 2000 at 10:31 UTC |