Acme-Lexical-Thief

 view release on metacpan or  search on metacpan

lib/Acme/Lexical/Thief.pm  view on Meta::CPAN

		exists $MY->{$_}  ? $MY->{$_} :
		exists $OUR->{$_} ? $OUR->{$_} :
		croak "$KEYWORD($_) failed; caller has no $_ defined";
	} @vars;
}

1;

__END__

=head1 NAME

Acme::Lexical::Thief - steal lexical variables from your caller

=head1 SYNOPSIS

   use 5.012;
   use strict;
   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:

   steal $car, @treasures, %stash;

Parentheses can surround the list of variables to steal:

   steal ($car, @treasures, %stash);

Generally everything should "just work" as you expect it to. Except when it
does not.

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
must be a literal integer in your source code. (It can be in decimal, octal,
hexadecimal or binary notation.) The integer must immediately follow the
C<steal> keyword, and not be followed by a comma.

The C<steal> keyword cannot be used in an expression; it must be a
standalone statement.

   steal $foo;
   if (defined $foo) { ... } # ok
   
   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.

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.

Just think about that for a minute, won't you?

=head1 BUGS

Please report any bugs to
L<http://rt.cpan.org/Dist/Display.html?Queue=Acme-Lexical-Thief>.

=head1 SEE ALSO

L<PadWalker> - a slightly more sane alternative to peeking at your caller's
lexicals.

L<Data::Alias> - a slightly more sane way of creating lexical aliases.

This package was initially published on PerlMonks as C<Acme::Asplode>



( run in 2.084 seconds using v1.01-cache-2.11-cpan-140bd7fdf52 )