in reply to Making lexically-scoped ref available to non-OO subs in another package
This seems to work fine for me:
#!/usr/bin/env perl use v5.14; package The::Package { use Carp; use base "Exporter"; BEGIN { our @EXPORT = qw( my_generator foo takes_coderef ) }; my $scratchpad; sub my_generator (&) { $scratchpad = {}; shift->(); my $tmp = $scratchpad; undef $scratchpad; return $tmp; } sub foo ($) { croak "foo must be called within my_generator" unless $scratch +pad; $scratchpad->{foo} = shift; } sub takes_coderef (&) { croak "takes_coderef must be called within my_generator" unles +s $scratchpad; $scratchpad->{coderef} = shift; } BEGIN { $INC{"The/Package.pm"} = __FILE__ }; }; use The::Package; use Data::Dumper; my $complex_result = my_generator { foo 'bar'; takes_coderef { ... }; }; print Dumper($complex_result); foo 'baz'; # Should not affect $complex_result. # Ideally, should croak, or not even compile.
If you want to be able to nest calls to my_generator then you need to have a stack of scratchpads, but that's very little extra work.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Making lexically-scoped ref available to non-OO subs in another package
by wanna_code_perl (Friar) on Jul 12, 2013 at 13:30 UTC | |
by tobyink (Canon) on Jul 13, 2013 at 07:35 UTC |