Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

How else can I read from a file without using a while loop, testing for the EOF?

( #36572=categorized question: print w/ replies, xml ) Need Help??
Contributed by Anonymous Monk on Oct 13, 2000 at 16:41 UTC
Q&A  > files


Answer: How else can I read from a file without using a while loop, testing for the EOF?
contributed by S@nto

Much less dangerous than undef-ing $/ globably (and maybe forgetting to set it back) local-ize it to a block that just does your file-reading:

my $string = do{ local $/; <$filehandle> };

Or you can use $RS if you can't (or don't want to) remember $/ .

Answer: How else can I read from a file without using a while loop, testing for the EOF?
contributed by Fastolfe

A while (<FH>) { ... } block implicitely tests for eof. Are you trying to avoid the <FH> construct (which reads line-by-line, unless you've undefined $/ as mentioned by another poster), such as with binary files?

There are also the very standard and documented built-ins: read and sysread. These will read an arbitrary amount of data from a file.

Answer: How else can I read from a file without using a while loop, testing for the EOF?
contributed by ickiller

As i know, the most efficient method is to set the input record separator to nothing like the following snippet:

$/ = undef; $wholefile = <FILE>;
Roli
Answer: How else can I read from a file without using a while loop, testing for the EOF?
contributed by t0j0

or maybe you can use file-slurp function..

use File::Slurp; # to get your data into scalar variable $output_string = read_file($filename); # or this to get your data into array @output_array = read_file($filename);

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (18)
    As of 2014-07-31 18:31 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (250 votes), past polls