RE: How do I read an entire file into a variable?
by vroom (His Eminence) on Dec 28, 1999 at 21:39 UTC
|
$/=undef; #sets the record separator to [undef] so i
+t keeps reading
#until there is nothing more to read
open FILE, "<yourfile";
my $data=<FILE>;
close FILE;
$/="\n"; #set it back to \n so if you try to read f
+rom the
#terminal it only takes a line at a time
| [reply] [d/l] |
|
|
Probably better to use local() and a block so as not to cause problems if somebody else set $/ to something other than \n (assumptions can come back to haunt you) .
my $data;
{
local($/) = undef;
open (FILE, "<yourfile");
$data = <FILE>;
close FILE;
}
| [reply] [d/l] |
|
|
Good point... although I think now that I see nate's method I think that will be my method of choice in the future.
| [reply] |
Re: How do I read an entire file into a variable?
by nate (Monk) on Dec 29, 1999 at 00:09 UTC
|
I usually do it slightly differently:
open FILE "myfile";
my $data = join "", <FILE>;
close FILE;
...but of course, there's more than one way to do it. | [reply] [d/l] |
RE: How do I read an entire file into a variable?
by Anonymous Monk on Dec 30, 1999 at 05:18 UTC
|
open FOO,"myfile";
$data = join('',<FOO>);
close FOO;
Then you can manipulate it:
$data =~ s/foo/bar/g;
In most cases you would be better off processing each line
by itself, but this is helpful for multi-line constructs,
and for magical things like 15-line tic-tac-toe AI programs. | [reply] [d/l] [select] |
|
|
I'd like to see that tic tac toe program.
| [reply] |
Re: How do I read an entire file into a variable?
by Anonymous Monk on Dec 30, 1999 at 21:31 UTC
|
Be careful with things like
join("",<HANDLE>);
If you have thousands of consecutive newlines, perl has to first create the array and then join it. That can get expensive. Do benchmarking.
But I like something simillar to the following:
my $buf=$content='';
$content.=$buf while read(HANDLE,$buf,8192);
8192 is arbitrary so optimize with glee. | [reply] [d/l] [select] |
|
|
The simple thought of reading a whole file (read: HD => BIG) into a variable (read: RAM => small) is pretty expensive by itself..
of course i get your point of it being even more expensive when you also need to use processing time to join it after clogging your ram..
(btw, my first post ;-} )
(sorry if i sounded harsh..)
| [reply] |
RE: How do I read an entire file into a variable?
by Anonymous Monk on Dec 29, 1999 at 23:35 UTC
|
#!/usr/bin/perl
open(FILE,"</path/to/file");
undef $/; # this makes the following line pull in everything instead o
+f just the first line
$contents=<FILE>;
close FILE;
| [reply] [d/l] |
Re: How do I read an entire file into a variable?
by Anonymous Monk on Dec 29, 1999 at 23:29 UTC
|
#!/usr/bin/perl
# open your file using a filehandle
# called MYFILE
open(MYFILE, "/home/me/myfile");
# slurp the contents of MYFILE into
# the variable $myfle
$myfile = (<MYFILE>);
# seach for your text ...
if ($myfile =~ m/some txt i look for/) {
print "Found the text\n";
}
# and look for more text
if ($myfile =~ m/some other text i look/) {
print "Got other text\n";
}
| [reply] [d/l] |
|
|
# seach for your text ...
if ($myfile =~ m/some txt i look for/) {
print "Found the text\n";
}
# and look for more text
if ($myfile =~ m/some other text i look/) {
print "Got other text\n";
}
and in the last i printed the variable like this. print $myfile,"\n";
As per the question the variable contain the total file in that variable. But its printing only one line. Please give some clarity where i missing | [reply] [d/l] [select] |
|
|
use Path::Tiny qw/ path /;
my $myfile = path( '/home/me/myfile' )->slurp_raw;
See reading an entire file into memory, however big it might be, is commonly called "slurp"ing
You can also read about it perlintro and free book Modern Perl | [reply] [d/l] |