Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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.

--
<http://www.dave.org.uk>

"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>.

      Jeroen
      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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2021-05-09 13:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (100 votes). Check out past polls.

    Notices?