Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

read file into scalar

by Anonymous Monk
on Oct 28, 2006 at 15:16 UTC ( #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...

Comment on read file into scalar
Download Code
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 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 davido (Archbishop) 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 jdporter (Canon) 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 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

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.

Log In?
Username:
Password:

What's my password?
Create A New User
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? | Other CB clients
Other Users?
Others surveying the Monastery: (15)
As of 2014-07-30 09:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (230 votes), past polls