in reply to How do I read an entire file into a variable?

#!/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"; }

Replies are listed 'Best First'.
Re^2: How do I read an entire file into a variable?
by ravi45722 (Pilgrim) on Aug 25, 2015 at 06:17 UTC

    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