in reply to SOLVED: Looking for suitable spells to get open to return a filehandle from a module
Hello talexb,
You are passing in $fh as a scalar, then overwriting it in the line $fh = Symbol::gensym;, so $fh works correctly inside Extra::open but remains undef in the caller. You need to pass in a reference, and dereference it accordingly within Extra::open:
{ my $fh; Extra::open ( \$fh, '>', 'extra-open.txt' ) or die "2. Open failed: $!"; defined $fh or die "2. Filehandle not defined"; print $fh "Extra open works fine.\n"; close ( $fh ); }
package Extra; use Symbol; sub open { my ( $fh, $direction, $filename ) = @_; $$fh = Symbol::gensym; open ( $$fh, $direction, $filename ); } 1;
In my (minimal) testing, this writes the correct messages to the two output files, and no error is generated.
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Looking for suitable spells to get open to return a filehandle from a module
by afoken (Chancellor) on Nov 28, 2016 at 06:17 UTC | |
by AnomalousMonk (Archbishop) on Nov 28, 2016 at 07:02 UTC | |
by afoken (Chancellor) on Dec 03, 2016 at 16:58 UTC | |
by pryrt (Abbot) on Dec 05, 2016 at 21:40 UTC | |
by Laurent_R (Canon) on Nov 28, 2016 at 07:26 UTC | |
|
Re^2: Looking for suitable spells to get open to return a filehandle from a module
by talexb (Chancellor) on Nov 28, 2016 at 13:34 UTC |