6 use B qw(svref_2object comppadlist class);
12 # perl -MO=Showlex,foo bar.pl
13 # to see the names of lexical variables used by &foo
15 # perl -MO=Showlex bar.pl
16 # to see the names of file scope lexicals used by bar.pl
20 # borrowed from B::Concise
21 our $walkHandle = \*STDOUT;
23 sub walk_output { # updates $walkHandle
24 $walkHandle = B::Concise::walk_output(@_);
25 #print "got $walkHandle";
26 #print $walkHandle "using it";
35 print $walkHandle "$name has $count entries\n";
36 for ($i = 0; $i < $count; $i++) {
38 if (class($sv) ne "SPECIAL") {
39 printf $walkHandle "$i: %s (0x%lx) %s\n", class($sv), $$sv, $sv->PVX;
41 printf $walkHandle "$i: %s\n", $sv->terse;
42 #printf $walkHandle "$i: %s\n", B::Concise::concise_sv($sv);
52 print $walkHandle "$name has $count entries\n";
53 for ($i = 0; $i < $count; $i++) {
54 printf $walkHandle "$i: %s\n", $els[$i]->terse;
55 #print $walkHandle "$i: %s\n", B::Concise::concise_sv($els[$i]);
60 my ($objname, $namesav, $valsav) = @_;
61 shownamearray("Pad of lexical names for $objname", $namesav);
62 showvaluearray("Pad of lexical values for $objname", $valsav);
65 sub newlex { # drop-in for showlex
66 my ($objname, $names, $vals) = @_;
67 my @names = $names->ARRAY;
68 my @vals = $vals->ARRAY;
70 print $walkHandle "$objname Pad has $count entries\n";
71 printf $walkHandle "0: %s\n", $names[0]->terse;
72 for (my $i = 1; $i < $count; $i++) {
73 printf $walkHandle "$i: %s = %s\n", $names[$i]->terse, $vals[$i]->terse;
77 my $newlex; # rendering state var
80 my ($objname, $obj) = @_;
81 $objname =~ s/^&main::/&/;
82 showlex($objname, svref_2object($obj)->PADLIST->ARRAY) if !$newlex;
83 newlex ($objname, svref_2object($obj)->PADLIST->ARRAY) if $newlex;
87 showlex("comppadlist", comppadlist->ARRAY);
91 my @options = grep(/^-/, @_);
92 my @args = grep(!/^-/, @_);
93 for my $o (@options) {
94 $newlex = 1 if $o eq "-newlex";
97 return \&showlex_main unless @args;
99 foreach my $objname (@args) {
102 print $walkHandle "B::Showlex::compile($objname)\n";
105 $objname = "main::$objname" unless $objname =~ /::/;
106 print $walkHandle "$objname:\n";
108 die "err: unknown function ($objname)\n"
109 unless *{$objname}{CODE};
110 $objref = \&$objname;
112 showlex_obj($objname, $objref);
123 B::Showlex - Show lexical variables used in functions or files
127 perl -MO=Showlex[,SUBROUTINE] foo.pl
131 When a subroutine name is provided in OPTIONS, prints the lexical
132 variables used in that subroutine. Otherwise, prints the file-scope
133 lexicals in the file.
137 Malcolm Beattie, C<mbeattie@sable.ox.ac.uk>