Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Perl: the Markov chain saw
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

OK, so as not to disappoint...

Introducing Acme::Asplode: it lets you drag your caller's lexical variables kicking and screaming into your sub. Ought to work in Perl 5.12+.

package Acme::Asplode; use 5.012; use strict; use warnings; use Keyword::Simple (); use PadWalker (); use Text::Balanced (); use Carp; sub import { Keyword::Simple::define asplode => sub { my $ref = shift; $$ref =~ s/^\s+//; my $extracted; if ($$ref =~ /^\(/) { $extracted = Text::Balanced::extract_bracketed($$ref) or croak "usage: asplode (VARIABLES);"; $extracted =~ s/(^\(|\)$)//g; } else { ($extracted, $$ref) = ($$ref =~ /^([^;]+)(;.*)$/s) or croak "usage: asplode VARIABLES;"; } (my $globs = $extracted) =~ s/[\$\%\@]/*/g; $$ref = "our($extracted); local($globs) = Acme::Asplode::_call +back(q($extracted));$$ref"; } } sub unimport { Keyword::Simple::undefine asplode => (); } sub _callback { my $vars = shift; $vars =~ s/(^\s*|\s*$)//g; my @vars = split /\s*,\s*/, $vars; my $MY = PadWalker::peek_my(2); my $OUR = PadWalker::peek_our(2); return map { exists $MY->{$_} ? $MY->{$_} : exists $OUR->{$_} ? $OUR->{$_} : croak "asplode($_) failed; caller has no $_ defined"; } @vars; } 'ASPLODE!';

Example usage...

use 5.012; use strict; use warnings; use Acme::Asplode; sub bar { asplode($x, $y); # steal lexical variables from our caller! say $x; say $y; asplode @A, $z; # steal moar lexicals!!! say $z; $z++; # alter our caller's lexical variable!! say for @A; }; sub foo { my $x = 1; my $y = 2; my $z = 3; my @A = qw(a b c); bar(); say "Z is changed: $z"; } foo();

Update: Acme::Asplode, renamed Acme::Lexical::Thief is now on CPAN.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

In reply to Re^3: using eval or equivalent to automatically generate my statements by tobyink
in thread using eval or equivalent to automatically generate my statements by ISAI student

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (7)
    As of 2014-04-18 00:54 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (460 votes), past polls