I'm using someone else's code which opens a file for reading like so:
I'd never seen that trick before. I found an explanation here: http://perl.plover.com/local.html#3_The_First_Class_Filehandle_Tr.$fh = do {local *FH}; open ($fh, $filename)
I want to be able to report the last line number read. I could use $., but I thought IO::Handle's input_line_number method would be better (so I don't have to worry about whether this filehandle was the last one accessed).
However, IO::Handle seems not to like the "do {local *FH};" trick. Here's a small example:
I have two easy workarounds -- use $. carefully, or modify the original code -- but I was wondering if there's a cleaner way of getting the line number?use strict; use warnings; use IO::Handle; my $fh; # $fh = do {local *FH}; #bad open ($fh, './test') or die("open failed: $!"); print "line: " . $fh->input_line_number(); __END__ output with "#bad" commented out: line: 0 output with "#bad" included: Can't call method "input_line_number" without a package or object refe +rence at test line 10.
Thanks,
Joe
In reply to Last Line Read? (Any way to use IO::Handle methods on a "First-Class" Filehandle?) by blahblahblah
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |