Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
P is for Practical
 
PerlMonks  

Re: Perl Idioms Explained - my $string = do { local $/; <FILEHANDLE> };

by Zaxo (Archbishop)
on Aug 29, 2003 at 14:52 UTC ( #287686=note: print w/ replies, xml ) Need Help??


in reply to Perl Idioms Explained - my $string = do { local $/; <FILEHANDLE> };

It may be useful to restrict the scope of the file handle as well, since slurping is a one pass approach. That may be done with

my $string = do { local $/; open local(*FILEHANDLE), 'somefile.txt' or die $!; <FILEHANDLE> };
or, better if lexical filehandles are available,
my $string = do { local $/; open my $fh, 'somefile.txt' or die $!; <$fh> };
since the lexical handle is properly closed when it goes out of scope. Either way ensures no interference with other handles of the same name.

After Compline,
Zaxo


Comment on Re: Perl Idioms Explained - my $string = do { local $/; <FILEHANDLE> };
Select or Download Code
Re: Re: Perl Idioms Explained - my $string = do { local $/; <FILEHANDLE> };
by John M. Dlugosz (Monsignor) on Sep 02, 2003 at 20:30 UTC
    How about opening it in binary mode? I would historically use a binmode after opening, but in 5.8 I should be able to use :raw or something like that. When I tried once, I spent a few minutes trying to make it work, and went back to the old way. What's the polished form of the idiom that uses the new syntax for binary files?

Re: Re: Perl Idioms Explained - my $string = do { local $/; <FILEHANDLE> };
by Zaxo (Archbishop) on Sep 03, 2003 at 02:42 UTC

    The truly modern PerlIO approach would be,     open my $fh, '<:raw', '/path/to/somefile.txt' or die $!;
    but I'd do it with binmode still, right after opening.

    After Compline,
    Zaxo

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (13)
As of 2014-04-17 13:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (447 votes), past polls