Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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);
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.
Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://20235]
[1nickt]: marioroy Yes, I am using it with MCE, as is Discipulus I believe. I was trying to work out how to make a cpanfile that would be smart enough to know which deps to require.
[1nickt]: See this code. (I expected to simply eval loading threads as a check, but weirdness happened with Perlbrew so it's a grep of -V ...)
[choroba]: Config might be better than grepping -V
[Corion]: Also see Config::V, which is less of that hackery, or that hackery hidden in a module ;)
[1nickt]: The problem was with Perlbrew
[Corion]: Whoops - Config::Perl::V
[1nickt]: I found that when using Perlbrew as recommended, with cpanminus in the system perl lib, such tests were failing to detect the data about the perl that was the install destination.

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (11)
As of 2017-10-18 15:35 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (249 votes). Check out past polls.