in reply to Re^3: restrict use of regex module in script
in thread restrict use of regex module in script
{ local $/; # slurp in a file my $contents = <$fh> ... } # now we go back to reading a file line-by-line again while (<$fh>){ ... }
Parenthetic note: in the quoted example code, the $fh file handle would become "exhausted" after the "slurp" read to $contents and the subsequent while-loop read of the handle would produce nothing. A seek operation is needed to reset the logical file position back to the beginning of the file (update: or at least to somewhere other than the end) so that subsequent reads may be successful. Try the following code without the seek statement:
c:\@Work\Perl\monks\pmpmmpmp>perl -wMstrict -le "open my $fh, '<', 'junk.txt' or die $!; ;; my $contents = do { local $/; <$fh>; }; print qq{[[$contents]]}; ;; seek $fh, 0, 0; print 'now we go back to reading a file line-by-line again'; while (<$fh>){ print qq{<<$_>>}; } " [[line 1 fee fie foe line 2 wibble wobble line 3 blah yada ]] now we go back to reading a file line-by-line again <<line 1 fee fie foe >> <<line 2 wibble wobble >> <<line 3 blah yada >>
Give a man a fish: <%-{-{-{-<
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: restrict use of regex module in script
by stevieb (Canon) on Apr 08, 2017 at 00:37 UTC |