in reply to Re: passing filename in subroutines
in thread passing filename in subroutines
Lastly, I think you can use a typeglob. But I'm not sure how, and it'll be ugly.Depends on your definition of ugly ;-)
It's not difficult either - taking up on your example:
sub foo { local *FILE; open FILE, "file.txt"; bar(*FILE); } sub bar { my $file = shift; my @lines = <$file>; # do something here }
Your alternative that's using the lexical variable as a filehandle is still the best, imho, but compared to not passing it and relying on the global variable, the typeglob approach is surely better - unless the script is very short and trivially simple. Btw, note my use of local to avoid clobbering the global filehandle FILE.
-- Hofmator
|
|---|