Acme-Lexical-Thief
view release on metacpan or search on metacpan
use warnings;
use Acme::Lexical::Thief;
sub greet {
my $name = shift;
greet_verbally();
}
sub greet_verbally {
steal $name; # caller variable
say "Hello $name";
}
DESCRIPTION
This package allows you access to your caller's lexical variables, without
them knowing! Full read/write access. This is generally a pretty bad idea,
hence the Acme namespace.
You can steal scalars, arrays and hashes:
steal $car, @treasures, %stash;
it does not.
Technically speaking, your stolen $car is a package-scoped (`our`)
variable which is lexically aliased (`local *car`) to the caller's
variable of the same name. Because `steal` is parsed at compile-time, you
don't need to (and indeed should not!) pre-declare your stolen variables.
sub greet_verbally {
my $name; # don't do this!
steal $name;
say "Hello $name";
}
By default, this module steals from your *immediate* caller. You can
thieve higher up the call stack using:
steal 0 ($car); # caller's $car
steal 1 @boats; # caller's caller's @boats
steal 2 %stash; # caller's caller's caller's @stash
You cannot indicate the level you wish to steal from using a variable; it
# ... 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.
Peeking at your caller's lexicals leaks an abstraction.
Peeking at your caller's lexicals can cause spooky action at a distance.
Every time you peek at your caller's lexicals, a fairy dies.
lib/Acme/Lexical/Thief.pm view on Meta::CPAN
use warnings;
use Acme::Lexical::Thief;
sub greet {
my $name = shift;
greet_verbally();
}
sub greet_verbally {
steal $name; # caller variable
say "Hello $name";
}
=head1 DESCRIPTION
This package allows you access to your caller's lexical variables, without
them knowing! Full read/write access. This is generally a pretty bad idea,
hence the Acme namespace.
You can steal scalars, arrays and hashes:
lib/Acme/Lexical/Thief.pm view on Meta::CPAN
Technically speaking, your stolen C<< $car >> is a package-scoped (C<our>)
variable which is lexically aliased (C<< local *car >>) to the caller's
variable of the same name. Because C<steal> is parsed at compile-time,
you don't need to (and indeed should not!) pre-declare your stolen
variables.
sub greet_verbally {
my $name; # don't do this!
steal $name;
say "Hello $name";
}
By default, this module steals from your I<immediate> caller. You can
thieve higher up the call stack using:
steal 0 ($car); # caller's $car
steal 1 @boats; # caller's caller's @boats
steal 2 %stash; # caller's caller's caller's @stash
You cannot indicate the level you wish to steal from using a variable; it
lib/Acme/Lexical/Thief.pm view on Meta::CPAN
...
}
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.
Peeking at your caller's lexicals leaks an abstraction.
Peeking at your caller's lexicals can cause spooky action at a distance.
Every time you peek at your caller's lexicals, a fairy dies.
( run in 1.647 second using v1.01-cache-2.11-cpan-a9496e3eb41 )