Hi Harry I was about to try to explain it but sometimes a picture is worth a thousand words (even if it's a picture of code:)
=========================================== 22:35 ~/tmp $ cat bar.txt this is a very long test 22:35 ~/tmp $ cat foo.pl #!/usr/bin/perl use strict; use warnings; my $arg = $ARGV[0]; open(my $fh, '<', $arg); { #local $/; my $i = 0; while (my $foo = <$fh>) { print "Line $i : $foo"; $i++; } } 22:35 ~/tmp $ ./foo.pl bar.txt Line 0 : this Line 1 : is Line 2 : a Line 3 : very long test # Now, uncomment the call to local 22:36 ~/tmp $ ./foo.pl bar.txt Line 0 : this is a very long test ========================================= If you want to change the newlines of that string into spaces, just add this as the first statement inside the while: $foo =~ s/\n/ /g; Andrew On Tue, Jan 20, 2015 at 10:10 PM, Harry Putnam <rea...@newsguy.com> wrote: > 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/ > > -- Andrew Solomon Mentor@Geekuni http://geekuni.com/ http://www.linkedin.com/in/asolomon -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/