2

With my oneliners I often want to fetch a whole file inside a string. With only core modules the only solutions I know considering $file = 'foo' are:

$str = do {local $/; open $fh, '<', $file; <$fh>};

Or

$str = qx{cat $file};

The second method is shorter to write, easier to understand, but involves a lot more complexity behind the curtain (sys_execve).

Is there any better way or a nice sugar that I don't know?

nowox
  • 25,978
  • 39
  • 143
  • 293
  • See also [how can I read an entire file into a string?](http://stackoverflow.com/questions/953707/in-perl-how-can-i-read-an-entire-file-into-a-string) and [What is the best way to slurp a file into a string in Perl?](http://stackoverflow.com/questions/206661/what-is-the-best-way-to-slurp-a-file-into-a-string-in-perl). – Håkon Hægland Jun 28 '15 at 10:02
  • 2
    The second isn't shorter. It only looks that way because you forgot that you needed error checking. Problems opening a file is the most common error! – ikegami Jun 29 '15 at 07:05
  • "With my oneliners I often want to fetch a whole file inside a string." `perl -0777 -nwE 'say $_' /path/to/file` – ThisSuitIsBlackNot Jun 29 '15 at 14:13

2 Answers2

1

another way is:

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

Cool, huh? This works by localizing @ARGV and saving $filename as the first element of @ARGV. The empty diamond operator <> automatically opens a filehandle to the first element of @ARGV, which is the filename. If you need to set a PerlIO layer, the filehandle name is ARGV so you can use binmode to set the layer (before the file is read!):

http://perltricks.com/article/21/2013/4/21/Read-an-entire-file-into-a-string

Perl 6 (still in alpha version) has a built in function to slurp in files. That is, to read the contents of a whole file into a scalar variable.

my $data = slurp $filename;

http://perl6maven.com/perl6-files

frhack
  • 4,862
  • 2
  • 28
  • 25
  • 1
    Good hack, I like it. Unfortunately it is still very cryptical for newbies. Perl miss a native way to slurp files *i.e.* `$str = slurp($file)` – nowox Jun 28 '15 at 10:07
  • 1
    "Perl 6 has a built in function to slurp in files. That is, to read the contents of a whole file into a scalar variable." http://perl6maven.com/perl6-files – frhack Jun 28 '15 at 10:09
  • Perl 6 is still in alpha version. http://perl6.guide/ – frhack Jun 28 '15 at 10:13
  • 1
    Yes and since 2001... I am afraid Perl will be dead before the first release of Perl 6 :( – nowox Jun 28 '15 at 10:18
  • http://hastebin.com/irisonamog.pl modernized it for you. – Dmytro Aug 26 '16 at 17:21
0

There is a good module at CPAN to do that: File::Slurp, a Simple and Efficient Reading/Writing/Modifying of Complete Files

# read in a whole file into a scalar variable ($text)
my $text = read_file( $filename ) ;
Miguel Prz
  • 13,718
  • 29
  • 42
  • Indeed it is good, but not part of core modules. Thus, it is not a very compatible solution :( – nowox Jun 28 '15 at 10:30
  • 4
    I never understand why people are so against modules in Perl and only want to use the core library. The modules exist to help people and to save people having to write their own implementations of features. You never see many people in other languages like java saying i only want to use the core java library and no others jars. – Chris Doyle Jun 28 '15 at 14:35
  • 2
    not so rarely install a new module requires to local compile it and when you do "make" it fails. – frhack Jun 29 '15 at 10:46
  • 2
    second if you have a script that do something simple and you have to distribute it, you don' like to have many dependencies. – frhack Jun 29 '15 at 10:50
  • 1
    File::Slurp is a pure Perl module, so compilation is not required in this case – Miguel Prz Jun 29 '15 at 12:49