in reply to Re: passing file handle to functions
in thread passing file handle to functions
foreach $file (@ARGV) { process($file, 'fh00'); } sub process { my($filename, $input) = @_; $input++; # this is a string increment unless (open($input, $filename)) { print STDERR "Can't open $filename: $!\n"; return; } local $_; while (<$input>) { # note use of indirection if (/^#include "(.*)"/) { process($1, $input); next; } #... # whatever } }
Did you try that under "use strict"?
Can't use string ("fh01") as a symbol ref while "strict refs" in use
From perldoc open:
If FILEHANDLE is an undefined scalar variable (or array or hash element) the variable is assigned a reference to a new anonymous filehandle, otherwise if FILEHANDLE is an expression, its value is used as the name of the real filehandle wanted. (This is considered a symbolic reference, so use strict 'refs' should not be in effect.)
So what you're trying only works with "use strict" if the variable that you use for the filehandle contains an undefined value.
"The first rule of Perl club is you do not talk about
Perl club."
-- Chip Salzenberg
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: passing file handle to functions
by UnderMine (Friar) on May 18, 2006 at 11:32 UTC | |
by japhy (Canon) on May 18, 2006 at 11:38 UTC |