in reply to slurping into scalars
$data = do { local $/ = undef; <$file> };
What this line of code does is clear the input record separator, $/, which is a newline character by default and delimits Perl's concept of what a "line" is. By undefining this variable, the first read of this file handle, <$file>, slurps the entire file into the variable. This behaviour is documented in perlvar.
Update
See particle's post in this thread here for an alternate method which also harnesses the magic of @ARGV allowing for the slurping of entire files into either an array or scalar using only a single line of code - For example: [@data|$data] = do { local( $/ ) = wantarray ? $/ : undef; local( @ARGV ) = @_; <> };
perl -le 'print+unpack("N",pack("B32","00000000000000000000000111110111"))'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: slurping into scalars
by Anonymous Monk on Dec 16, 2002 at 10:17 UTC | |
by rob_au (Abbot) on Dec 16, 2002 at 10:22 UTC | |
by dingus (Friar) on Dec 16, 2002 at 11:11 UTC |