in reply to printing variable and evaluation

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."

Replies are listed 'Best First'.
Re^2: printing variable and evaluation (updated)
by f77coder (Beadle) on Jul 07, 2017 at 11:17 UTC

    Thank you for the insights.

    This "Guts of Perl debugging" looks a bit intimidating.

    Is there no way to read the input variable

    $variable

    as a literal string before any processing or evaluation is done? then let perl do an eval of the variable?

      Given what I already said (it is unwise) and good modules suggested as symbol table inspector ie Data::Dumper::Names which uses PadWalker you probably mean something like the following (working for scalar, must be modified for arrays and hashes)?

      perl -e " my $x = 15; print map {$_.' = '.eval eval{$_} } '$x' " $x = 15

      Again: this leads you on a wrong path, might be worth to know but not to be used

      L*

      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.