I found this little snippet in a post (from 2000) by Randal L. Schwartz:

  http://www.perlmonks.org/?node_id=20235

The discussion was about:

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

And a previous poster shows an example of doing it with `select';

Randal responded:

   I just go:

   my $contents = do { local $/; <HANDLE> };

   The $/ variable is not per-filehandle, so no need to select.

I have not been able to get this to work.

My code uses the old style `open' just in case it would matter:

  use strict;
  use warnings;

  my $file = '.bashrc';
  open(FH, $file) or die "Can't open $file: $!";
  my $content = do { local $/; <FH> };

  print $content . "\n";

But when I print `$content' my .bashrc file looks totally normal.

I expected to see it as one long string.

I also tried it with nothing in the script except(`use' stuff) and
Randal's code slightly modified:

    use strict;
    use warnings;

    my $content = do { local $/; <> };
    print $content . "\n";

Of course it changed nothing.  The file was still printed with all
newlines in place.

Should I be seeing a file with no newlines being printed as a very
long line?


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to