Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Load file into a scalar without File::Slurp

by BlueLines (Hermit)
on Jun 28, 2000 at 23:22 UTC ( #20235=snippet: print w/ replies, xml ) Need Help??

Description: File::Slurp allows you read a filehandle into a scalar. However there is another way to do this without having to load an extra module at runtime. The select statement changes $/ (the input record separator) to a null character instead of a \n. And there you go..
#!/usr/bin/perl

my $file="foobar";
open (FILE, $file) or
    die "Can't open $file: $!\n";
select((select(FILE), $/ = undef)[0]);
my $contents = <FILE>;
close (FILE);
Comment on Load file into a scalar without File::Slurp
Download Code
RE: Load file into a scalar without File::Slurp
by merlyn (Sage) on Jun 28, 2000 at 23:32 UTC
      rock on. it's nice to try to share an easy way to do something and end up with an even easier way in return :-)

      --jon
      Try this on for size...

      <xmp> my $content = join"",<FILEHANDLE>; </xmp>

      oooh, I get goosebumps...

      -Matthew
        But now you've built an array just to discard it when you create the string. So, you win points for "easy to type", but lose points for "wasteful implementation".

        -- Randal L. Schwartz, Perl hacker

RE: Load file into a scalar without File::Slurp
by turnstep (Parson) on Jul 17, 2000 at 04:23 UTC

    Or the semi-obfuscated version: as long as you just emptied out $/, why not use it? :)

    { local $/; $/ .= $_ while <STDIN>; }

    P.S. In general, you want to make changes to global variables a temporary condition, as in merlyn's do loop above, and the more general form:

    { local $/; ## This is already undef, no need to set it ## Read in the file, etc... } ## $/ is now back where it was
      Here's a shorter version of that. I feel dirty:
      { local $/ = <STDIN>; }
      Note to anyone who has to look at $/ to understand this -- do not use this code. It's a seriously ugly thing.
RE: Load file into a scalar without File::Slurp
by audreyt (Hermit) on Jul 25, 2000 at 01:25 UTC
    I usually use this Perl Golf-ish subroutine that doesn't require a filehandle:
    $contents=slurp('foobar'); sub slurp {local$/=<>if local@ARGV=@_}
    or use an in-line block:
    {local$/;$contents=<>if local@ARGV='foobar'};
    or, if you could afford a filehandle and wasted CPU time:
    $contents=join'',<_>if open _,'foobar';
    Of course, if this is really a Perl Golf contest entry, I'll probably write:
    $contents=`cat foobar`
    But it's platform dependent and cheating, so don't do that. :-p

    /Autrijus/

Back to Snippets Section

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (10)
As of 2014-09-16 21:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (49 votes), past polls