Acme-Lexical-Thief
view release on metacpan or search on metacpan
if (steal $foo) { ... } # not this!
# this works...
if (do { steal $foo; defined $foo })
{
# ... but $foo won't exist in this block!
...
}
If you attempt to steal a variable which does not exist, then a run-time
exception will be thrown.
WHY YOU SHOULD NOT USE THIS MODULE
When people declare lexical (`my`) variables within a sub, they (quite
reasonably) expect these to stay local to the sub. If they rename those
variables, change them (say replacing a hashref with a hash), drop them or
whatever, then they don't expect code outside the sub to pay much
attention.
Peeking at your caller's lexicals breaks those expectations.
lib/Acme/Lexical/Thief.pm view on Meta::CPAN
if (steal $foo) { ... } # not this!
# this works...
if (do { steal $foo; defined $foo })
{
# ... but $foo won't exist in this block!
...
}
If you attempt to steal a variable which does not exist, then a run-time
exception will be thrown.
=head1 WHY YOU SHOULD NOT USE THIS MODULE
When people declare lexical (C<my>) variables within a sub, they (quite
reasonably) expect these to stay local to the sub. If they rename those
variables, change them (say replacing a hashref with a hash), drop them
or whatever, then they don't expect code outside the sub to pay much
attention.
Peeking at your caller's lexicals breaks those expectations.
t/02thievery.t view on Meta::CPAN
=head1 PURPOSE
Tests that variable stealing works.
Scalars, arrays and hashes are tested; multiple call stack levels are tested;
decimal and hexadecimal notations for call stack levels are tested; the syntax
with and without parentheses is tested; line breaks and other insignicant
white space in the syntax is tested.
There is a test that an exception is thrown when you try to steal a
non-existant variable.
There is a test that stolen variables are writable.
=head1 AUTHOR
Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
=head1 COPYRIGHT AND LICENCE
t/02thievery.t view on Meta::CPAN
'@A' => [qw/ A-quux /],
'@B' => [qw/ B-bar /],
'%C' => { C => 'bar' },
},
'steal can steal scalars, arrays and hashes from several call stack depths',
);
like(
exception { foo() },
qr{steal..x. failed. caller has no .x defined},
'throws an exception when unable to steal',
);
sub xxx1 {
steal $x;
$x++;
}
sub xxx2 {
my $x = 41;
xxx1(123);
( run in 0.256 second using v1.01-cache-2.11-cpan-496ff517765 )