in reply to passing filename in subroutines
The first is, you can just not pass it. Filehandles are global. So you can just use it, like this:
Second, you can open the filehandle as a variable and pass that. This is probably the best method.sub foo { open FILE, "file.txt"; bar(); close FILE; } sub bar { my @lines = <FILE>; # do something here }
Lastly, I think you can use a typeglob. But I'm not sure how, and it'll be ugly. My advice would be to forget that I even mentioned it and use one of the other methods. But it's there if you need it.sub foo { open my $file, "file.txt"; bar($file); close $file; } sub bar { my ($file) = @_; my @lines = <$file>; # do something here }
elusion : http://matt.diephouse.com
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: passing filename in subroutines
by Hofmator (Curate) on Mar 04, 2003 at 11:36 UTC | |
|
Re: Re: passing filename in subroutines
by snam (Novice) on Mar 04, 2003 at 04:12 UTC |