Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Slurp a file

by davorg (Chancellor)
on Jan 08, 2001 at 21:53 UTC ( #50529=note: print w/replies, xml ) Need Help??

in reply to Slurp a file

Oops. You've just left $/ set to undef for the rest of the program. This can lead to all sorts of nastiness.

Better to do something like this:

my $file_text; { local $/; $file_text = <FILE>; } print $file_text;

Which returns $/ to its original value once you leave the bare block.

Update: I should point out that my correction was to the original version of this node. It's now been changed to use my suggestion.


"Perl makes the fun jobs fun
and the boring jobs bearable" - me

Replies are listed 'Best First'.
Re: Re: Slurp a file
by merlyn (Sage) on Jan 08, 2001 at 21:55 UTC
      Tilly pointed me to the use of join in this case. Methinks it's pretty easy:
      $content = join '', <SOMEHANDLE>;
      Chipmunk, just 4u: One might see the actual response of tilly in this node <grin>.

      I was dreaming of guitarnotes that would irritate an executive kind of guy (FZ)

        That will be slower though.

        The reason I suggested it is in case someone wanted to use your SuperSplit with tied filehandles. I would not depend on a tied filehandle correctly paying attention to $/, so the join makes sense.

        But if you care about performance or have good reason to believe that people won't use tied filehandles, then avoid join.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://50529]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2023-02-06 13:47 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (34 votes). Check out past polls.