That's nontrivial, but if you're really interested in how to do this, then look at the code of Data::Dumper::Names, it uses PadWalker to inspect the lexical variables in the calling scope.

use Data::Dumper::Names; my $foo = "Bar"; print Dumper($foo); __END__ $foo = 'Bar';

Update: I guess I should expand on the "nontrivial" a little more. There is no "simple" mechanism in Perl for a subroutine like your proposed pprint to know where its arguments in @_ came from. Consider the simple case of my @bar = ("a","b"); foo(@bar,"c"), what the sub foo sees is @_ = ("a","b","c"), with no easy way of knowing where those values came from, and in the case of the literal "c" there is no variable. Diving into this topic will bring you to places like caller, Symbol Tables, lexical variables and modules like PadWalker, and perhaps even the guts of the debugger, "which range from difficult to impossible to understand for anyone who isn't incredibly intimate with Perl's guts. Caveat lector."


In reply to Re: printing variable and evaluation (updated) by haukex
in thread printing variable and evaluation by f77coder

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.