Are you trying to pass a filehandle to a subroutine? If so, there are a variety of methods you can use.
The first is, you can just not pass it. Filehandles are global. So you can just use it, like this:
sub foo {
open FILE, "file.txt";
bar();
close FILE;
}
sub bar {
my @lines = <FILE>;
# do something here
}
Second, you can open the filehandle as a variable and pass that. This is probably the best method.
sub foo {
open my $file, "file.txt";
bar($file);
close $file;
}
sub bar {
my ($file) = @_;
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.
elusion : http://matt.diephouse.com | [reply] [Watch: Dir/Any] [d/l] [select] |
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 | [reply] [Watch: Dir/Any] [d/l] |
How can I pass filename to subroutine?
Either of your examples is potentially correct, though I suspect neigher is what you want.
my $filename = aa;
will put a value into $filename if you've previously defined a suboutine "aa" that returns a value.
my $filename = <aa>;
will read a line from a file that you've opened using the symbol "aa" to hold the file descriptor.
I suspect you want
my $filename = "foo";
In any case,
subroutine($filename);
is the correct way to invoke a subroutine, passing whatever $filename holds as the single argument.
| [reply] [Watch: Dir/Any] [d/l] [select] |