Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: Making lexically-scoped ref available to non-OO subs in another package

by wanna_code_perl (Pilgrim)
on Jul 12, 2013 at 13:30 UTC ( #1043971=note: print w/ replies, xml ) Need Help??


in reply to Re: Making lexically-scoped ref available to non-OO subs in another package
in thread Making lexically-scoped ref available to non-OO subs in another package

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; }

Just when I thought I was getting the hang of Perl..! I had no idea a my package variable would work like this.

I'm amazed at the elegance of this, and that you came up with this rather instantaneously. Thank you!


Comment on Re^2: Making lexically-scoped ref available to non-OO subs in another package
Select or Download Code
Re^3: Making lexically-scoped ref available to non-OO subs in another package
by tobyink (Abbot) on Jul 13, 2013 at 07:35 UTC

    I probably wouldn't have come up with it so quickly if I hadn't been playing around with p5-mop-redux which is a prototype of a hopefully future Perl core feature. The prototype uses rather a similar technique (combined with lots of Devel::Declare magic) to provide its class-building syntax, though it uses localized package variables rather than lexicals.

    (I've also done something fairly similar in Smart::Dispatch.)

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1043971]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2014-08-30 03:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (291 votes), past polls