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 my ($newlex, $nosp1); # rendering state vars
67 sub newlex { # drop-in for showlex
68 my ($objname, $names, $vals) = @_;
69 my @names = $names->ARRAY;
70 my @vals = $vals->ARRAY;
72 print $walkHandle "$objname Pad has $count entries\n";
73 printf $walkHandle "0: %s\n", $names[0]->terse unless $nosp1;
74 for (my $i = 1; $i < $count; $i++) {
75 printf $walkHandle "$i: %s = %s\n", $names[$i]->terse, $vals[$i]->terse
76 unless $nosp1 and $names[$i]->terse =~ /SPECIAL/;
81 my ($objname, $obj) = @_;
82 $objname =~ s/^&main::/&/;
83 showlex($objname, svref_2object($obj)->PADLIST->ARRAY) if !$newlex;
84 newlex ($objname, svref_2object($obj)->PADLIST->ARRAY) if $newlex;
88 showlex("comppadlist", comppadlist->ARRAY) if !$newlex;
89 newlex ("main", comppadlist->ARRAY) if $newlex;
93 my @options = grep(/^-/, @_);
94 my @args = grep(!/^-/, @_);
95 for my $o (@options) {
96 $newlex = 1 if $o eq "-newlex";
97 $nosp1 = 1 if $o eq "-nosp";
100 return \&showlex_main unless @args;
103 foreach my $objname (@args) {
104 next unless $objname; # skip nulls w/o carping
107 print $walkHandle "B::Showlex::compile($objname)\n";
110 $objname = "main::$objname" unless $objname =~ /::/;
111 print $walkHandle "$objname:\n";
113 die "err: unknown function ($objname)\n"
114 unless *{$objname}{CODE};
115 $objref = \&$objname;
117 showlex_obj($objname, $objref);
128 B::Showlex - Show lexical variables used in functions or files
132 perl -MO=Showlex[,-OPTIONS][,SUBROUTINE] foo.pl
136 When a comma-separated list of subroutine names is given as options, Showlex
137 prints the lexical variables used in those subroutines. Otherwise, it prints
138 the file-scope lexicals in the file.
144 $ perl -MO=Showlex -e 'my ($i,$j,$k)=(1,"foo")'
145 Pad of lexical names for comppadlist has 4 entries
146 0: SPECIAL #1 &PL_sv_undef
147 1: PVNV (0x9db0fb0) $i
148 2: PVNV (0x9db0f38) $j
149 3: PVNV (0x9db0f50) $k
150 Pad of lexical values for comppadlist has 5 entries
151 0: SPECIAL #1 &PL_sv_undef
160 $ perl -MO=Showlex,-newlex -e 'my ($i,$j,$k)=(1,"foo")'
161 main Pad has 4 entries
162 0: SPECIAL #1 &PL_sv_undef
163 1: PVNV (0xa0c4fb8) "$i" = NULL (0xa0b8234)
164 2: PVNV (0xa0c4f40) "$j" = NULL (0xa0c4f34)
165 3: PVNV (0xa0c4f58) "$k" = NULL (0xa0c4f4c)
168 New form, no specials, outside O framework:
170 $ perl -MB::Showlex -e \
171 'my ($i,$j,$k)=(1,"foo"); B::Showlex::compile(-newlex,-nosp)->()'
172 main Pad has 4 entries
173 1: PVNV (0x998ffb0) "$i" = IV (0x9983234) 1
174 2: PVNV (0x998ff68) "$j" = PV (0x998ff5c) "foo"
175 3: PVNV (0x998ff80) "$k" = NULL (0x998ff74)
177 Note that this example shows the values of the lexicals, whereas the other
178 examples did not (as they're compile-time only).
182 The C<-newlex> option produces a more readable C<< name => value >> format,
183 and is shown in the second example above.
185 The C<-nosp> option eliminates reporting of SPECIALs, such as C<0: SPECIAL
186 #1 &PL_sv_undef> above. Reporting of SPECIALs can sometimes overwhelm
187 your declared lexicals.
191 C<B::Showlex> can also be used outside of the O framework, as in the third
192 example. See C<B::Concise> for a fuller explanation of reasons.
196 Some of the reported info, such as hex addresses, is not particularly
197 valuable. Other information would be more useful for the typical
198 programmer, such as line-numbers, pad-slot reuses, etc.. Given this,
199 -newlex isnt a particularly good flag-name.
203 Malcolm Beattie, C<mbeattie@sable.ox.ac.uk>