Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

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

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
Replies are listed 'Best First'.
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 :-)

      Try this on for size...

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

      oooh, I get goosebumps...

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


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.

Back to Snippets Section

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2015-11-28 21:34 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (745 votes), past polls