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 | |
by haukex (Archbishop) on Jul 07, 2017 at 11:27 UTC | |
by Discipulus (Canon) on Jul 08, 2017 at 09:26 UTC |