in reply to printing the line after grepping

use strict; use warnings; use autodie qw(open close); ... open my $handle, '<', $filepath; while (<$handle>) { print if /0 Items in Feed & 0 New Fetched items/; } close $handle;
Perl 6 - links to (nearly) everything that is Perl 6.

Replies are listed 'Best First'.
Re^2: printing the line after grepping
by 7stud (Deacon) on Nov 11, 2009 at 17:13 UTC

    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.

      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.