MarkofPerl has asked for the wisdom of the Perl Monks concerning the following question:
I have tried every suggestion I could find on how to pull (aka Slurp) the text of a file into a perl program and all of them that say they should work weren't. I am on a Mac Mini with Lion (10.7), and the perl version is 5.12. Here are some that I've tried:
#!/usr/local/env Perl5.12 ### filename:Hello.pl use strict; use File::Slurp; my $string; open (my $QBF, "<QBF.txt"); $string = read_file($QBF); print "\n"; print $string + "\n"; # doesn't print anything. (or prints "0") print "\n";
## -----------------
use File::Slurp; open my $fh, "<", "QBF.txt" or die $!; local $/; # enable localized slurp mode my $content = <$fh>; close $fh; print $content; # doesn't print anything.
At least last night is wasn't working. I had started with a while loop which worked fine by itself:
print"\n"; open (my $QBF, "QBF.txt"); while (<$QBF>) { print; }
after that I was trying to add the other methods for "slurping", into that same program, and it wasn't until I segregated them into two separate files that the second version worked.
use File::Slurp; open my $fh, "<", "QBF.txt" or die $!; local $/; # enable localized slurp mode my $content = <$fh>; close $fh; print $content; # prints now.
So what I'm wondering is if i put these two pieces of code together, is the read pointer already at the end of the file, and that's why they won't work in succession? It seemed like I was only getting one output even though I had two different routines for extracting (one just prints, the other saves the file contents to a local scalar for processing, and then prints.) Also I'd like to know more about what "$/" is and how it works Thanks.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Contents of a file into a scalar
by davido (Cardinal) on Aug 09, 2013 at 23:01 UTC | |
by MarkofPerl (Initiate) on Aug 10, 2013 at 02:31 UTC | |
by davido (Cardinal) on Aug 10, 2013 at 05:21 UTC | |
by toolic (Bishop) on Aug 10, 2013 at 02:35 UTC | |
|
Re: Contents of a file into a scalar
by Kenosis (Priest) on Aug 10, 2013 at 02:48 UTC | |
|
Re: Contents of a file into a scalar
by tobyink (Canon) on Aug 10, 2013 at 07:13 UTC | |
|
Re: Contents of a file into a scalar
by toolic (Bishop) on Aug 09, 2013 at 23:04 UTC | |
|
Re: Contents of a file into a scalar
by AnomalousMonk (Archbishop) on Aug 10, 2013 at 17:48 UTC |