You are *so* close on this one - I think the following is what you are after ...
$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"))'
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.