in reply to Grepping the filehandler
The main problem is context. Both print on line 2 and list assignment on line 4 force list context, which means the whole contents of the file is returned from the readline. Line 3 uses readline in the binding operator which forces scalar context, which means it only reads one line from the file. If the very first line doesn't contain the error, it won't be printed.
Inserted: To shorten the code, you can use
as grep also forces list context to its non-first argument(s).print grep /error/, <$f>;
Some other comments:
open my $f, '<', 'file' or die $!;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Grepping the filehandler
by arunkumar.rk141 (Initiate) on Jul 23, 2020 at 16:36 UTC | |
by haukex (Archbishop) on Jul 23, 2020 at 17:39 UTC |