sgtbaker has asked for the wisdom of the Perl Monks concerning the following question:

How can I read an entire file into a variable so I can search the entire thing for patterns.
  • Comment on How do I read an entire file into a variable?

Replies are listed 'Best First'.
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
      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; }
        Good point... although I think now that I see nate's method I think that will be my method of choice in the future.
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.

RE: How do I read an entire file into a variable?
by Anonymous Monk on Dec 30, 1999 at 05:18 UTC
    Use something like this:
    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.
      I'd like to see that tic tac toe program.
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.
      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..)
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;
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"; }

      Its very old for you. But i am very new to perl. So i am trying the existing programs which solved here. When i tried the above program i commented the two if cases

      # 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

        Hi ravi45722 , this is how its done :) with Path::Tiny

        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