<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">NAME
    lexicals - Get a hash of your current 'my' variables

SYNOPSIS
        use Template::Toolkit::Simple;
        use lexicals;

        sub mail {
            my $self = shift;
            my $name = 'Mr. ' . $self-&gt;get_name;
            my $address = $self-&gt;fetch_address($name);
            my $stamp = Postage::Stamp-&gt;new(0.44);
            my $envelope = tt-&gt;render('envelope', lexicals);
        }

DESCRIPTION
    Python has a builtin function called "locals()" that returns the
    lexically scoped variables in a name/value mapping. This is a very
    useful idiom. Instead of needing to create a hash like this:

        my $hash = {
            foo =&gt; $foo,
            bar =&gt; $bar,
        };

    Just say:

        my $hash = lexicals;

    Assuming you have a $foo and $bar defined, you get the same thing.

    The "lexicals" module exports a function called "lexicals". This
    function returns the lexicals as a hash reference (in scalar or list
    context).

ARRAYS AND HASHES
    The above examples deal with lexical scalars. You can also get back
    lexical arrays and hashes. Note: since there is no sigil to tell scalars
    from arrays from hashes, you can't get back a scalar and an array or
    hash of the same name. In this case, SCALAR beats HASH beats ARRAY. Why?
    Because I said so! (Actually I just used the sort order of the sigils).

        sub foo {
            my %h = ( O =&gt; 'HAI' );
            my @a = [ qw( foo bar baz ) ];
            my $s = 42;
            my %x = ( O =&gt; 'HAI' );
            my @x = [ qw( foo bar baz ) ];
            my $x = 42;
            print Dump lexicals;
        }

    would yield:

        ---
        a:
        - foo
        - bar
        - baz
        h:
          O: HAI
        s: 42
        x: 42

NOTE
    The "lexicals" function only reports the lexical variables variables
    that were defined before where it gets called.

DEBUGGING TRICK
    This could be a handy idiom for debugging:

        use XXX;

        sub foo {
            ...
            XXX lexicals;     # See your lexicals in the nude.
            ...
        }

SEE
    *   PadWalker

    *   Acme::Locals

    *   XXX

AUTHOR
    Ingy dÃ¶t Net &lt;ingy@cpan.org&gt;

COPYRIGHT
    Copyright 2011-2014. Ingy dÃ¶t Net.

    This program is free software; you can redistribute it and/or modify it
    under the same terms as Perl itself.

    See &lt;http://www.perl.com/perl/misc/Artistic.html&gt;

</pre></body></html>