in reply to Re: printing the line after grepping
in thread printing the line after grepping

A beginner's version of that would be:

use strict; use warnings; use 5.010; my $pattern = '0 Items in Feed & 0 New Fetched items'; open(my $INFILE, "<", "data2.txt") or die "There was a problem: $!"; while (my $line = <$INFILE>) { print $line if $line =~ /$pattern/; }

If you write the while loop like this:

while (<$INFILE>) { }

it becomes magical in perl. Every time through the while loop, the next line from the file will be read into a variable named $_. Calling print with no arguments will print $_, and a regex will match against $_ by default.

Replies are listed 'Best First'.
Re^3: printing the line after grepping
by ikegami (Patriarch) on Nov 11, 2009 at 17:50 UTC
    Actually, while (my $line = <$INFILE>) is equally magical.
    while (<$INFILE>) => while (defined($_ = <$INFILE>) while (my $line = <$INFILE>) => while (defined(my $line = <$INFILE>)
      Equally magical? Nothing gets assigned to $_ in the latter case.
        Yes. In both cases, the code that's executed is different than the code in the file.