I'd like to pass a reference to a file handle to a subroutine, such that code in the sub can read from the file and the position in the file handle changes in the calling environment, rather like using a pointer in C.
This kind of thing:
open my $fh, '<', $somefile or die;
dosomething(\$fh);
sub dosomething
{
my $fh_ref = shift;
while (my $l = <$$fh_ref>)
{
print $l;
print "\n";
}
}
This gives this output instead of writing each line:
GLOB(0x20b8b38)
Obviously I am dereferencing the file handle reference wrong.
Addendum:
while (my $l = readline($$fh_ref))
{
etc.
seems to do the trick. I am still interested in why the first approach doesn't work.