perl - How do I pass an opened filehandle as the parameter of a function? -
i have main script calls function module. open file in main script, , want use filehandle attached file parameter in function called (i don't want open file in function because calling same function on same file several times, opening every time wouldn't make sense).
in main script:
open(my $in_file, "<input.txt") or die "can't open: $!\n"; open(my $out_file, ">output.txt") or die "can't open: $!\n"; anothermodule->processdbu($in_file, $out_file);
in anothermodule
:
sub processdbu{ $in_file = $_[0]; $out_file = $_[1]; local $/ = ' '; while(<$in_file>){ someprocess(); } }
however, when try run script, error occurs:
readline() on unopened filehandle @ anothermodule.pm line 7.
what's preventing me using filehandle? why unopened? can fix problem?
you calling function method.
anothermodule->processdbu($in_file, $out_file);
should be
anothermodule::processdbu($in_file, $out_file);
three differences:
- method calls search inheritance hierarchy,
- method calls ignore prototypes, and
- method calls pass invocant (what's left of
->
) first argument.
Comments
Post a Comment