Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

read file into scalar

by Anonymous Monk
on Oct 28, 2006 at 15:16 UTC ( [id://581087]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, how do I read a file straight into a scalar?
open PAGE, '<', $success or die "Can't open $success : $!"; flock (PAGE, 1) or die "Can't lock $success for reading: $!"; my $page = <PAGE>; close PAGE;
only seems to read the first line. I know I can read it into an array, but I don't want that...

Replies are listed 'Best First'.
Re: read file into scalar
by Limbic~Region (Chancellor) on Oct 28, 2006 at 15:21 UTC
    Anonymous Monk,
    That is because in scalar context, the angle bracket operator (see also readline) uses $/ (see perlvar) to return a single line. To get it to read the entire file, simply undef it (locally of course).
    my $page; { local $/; open(my $fh, '<', $file) or die $!; $page = <$fh>; }

    Cheers - L~R

Re: read file into scalar
by davido (Cardinal) on Oct 28, 2006 at 15:33 UTC

    do{...} is a nice way to encapsulate the process.

    my $page = do { local $/; open my $pagefile, '<', $success or die $! <$pagefile>; };

    Dave

Re: read file into scalar
by imp (Priest) on Oct 28, 2006 at 15:22 UTC
    You can change the special $/ variable to enable 'slurp mode'.
    open PAGE, '<', $success or die "Can't open $success : $!"; flock (PAGE, 1) or die "Can't lock $success for reading: $!"; local $/; my $page = <PAGE>; close PAGE;
Re: read file into scalar
by jdporter (Paladin) on Oct 28, 2006 at 15:46 UTC

    I often to this:

    my $text = do { local( @ARGV, $/ ) = ( $file ); <> };

    although it's not as robust as the other solutions which die on error.

    We're building the house of the future together.
Re: read file into scalar
by arkturuz (Curate) on Oct 28, 2006 at 18:59 UTC
    Use File::Slurp. It has everything you need for simple file reading and writing, and the most simplified interface.
      follow an advice from 'Perl Best Practices' and use Perl6::Slurp.

      Enjoy,
      Mickey

        I follow my advice not to mess with unfinished programming languages and to-be-announced features :-)
        Anyway, thanks for the advice, I'll explore it.
      Everything you need except for locking, which this OP seems to need. Though if a lock isn't needed for the whole read/update data/write cycle, File::Slurp's atomic option may be enough.
Re: read file into scalar
by tsee (Curate) on Oct 28, 2006 at 18:46 UTC

    Hi,

    perhaps you're doing it just for brevity, but I can't resist pointing out that it's a very bad idea to use flock() with numeric constants (e.g. 1). You should use the Fcntl module and import the flock constants:

    use Fcntl qw/:flock/; ... open flock($fh, FLOCK_EX); # or: flock($fh, FLOCK_SH); ...

    Aside from being portable, it has the added benefit of being more friendly on the eyes and less demanding to your memory.

    Steffen

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://581087]
Approved by Limbic~Region
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-19 20:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found