Commit | Line | Data |
a798dbf2 |
1 | package B::Showlex; |
2 | use strict; |
3 | use B qw(svref_2object comppadlist class); |
4 | use B::Terse (); |
5 | |
6 | # |
7 | # Invoke as |
8 | # perl -MO=Showlex,foo bar.pl |
9 | # to see the names of lexical variables used by &foo |
10 | # or as |
11 | # perl -MO=Showlex bar.pl |
12 | # to see the names of file scope lexicals used by bar.pl |
13 | # |
14 | |
15 | sub showarray { |
16 | my ($name, $av) = @_; |
17 | my @els = $av->ARRAY; |
18 | my $count = @els; |
19 | my $i; |
20 | print "$name has $count entries\n"; |
21 | for ($i = 0; $i < $count; $i++) { |
22 | print "$i: "; |
23 | $els[$i]->terse; |
24 | } |
25 | } |
26 | |
27 | sub showlex { |
28 | my ($objname, $namesav, $valsav) = @_; |
29 | showarray("Pad of lexical names for $objname", $namesav); |
30 | showarray("Pad of lexical values for $objname", $valsav); |
31 | } |
32 | |
33 | sub showlex_obj { |
34 | my ($objname, $obj) = @_; |
35 | $objname =~ s/^&main::/&/; |
36 | showlex($objname, svref_2object($obj)->PADLIST->ARRAY); |
37 | } |
38 | |
39 | sub showlex_main { |
40 | showlex("comppadlist", comppadlist->ARRAY); |
41 | } |
42 | |
43 | sub compile { |
44 | my @options = @_; |
45 | if (@options) { |
46 | return sub { |
47 | my $objname; |
48 | foreach $objname (@options) { |
49 | $objname = "main::$objname" unless $objname =~ /::/; |
50 | eval "showlex_obj('&$objname', \\&$objname)"; |
51 | } |
52 | } |
53 | } else { |
54 | return \&showlex_main; |
55 | } |
56 | } |
57 | |
58 | 1; |
7f20e9dd |
59 | |
60 | __END__ |
61 | |
62 | =head1 NAME |
63 | |
64 | B::Showlex - Show lexical variables used in functions or files |
65 | |
66 | =head1 SYNOPSIS |
67 | |
68 | perl -MO=Showlex[,SUBROUTINE] foo.pl |
69 | |
70 | =head1 DESCRIPTION |
71 | |
72 | When a subroutine name is provided in OPTIONS, prints the lexical |
73 | variables used in that subroutine. Otherwise, prints the file-scope |
74 | lexicals in the file. |
75 | |
76 | =head1 AUTHOR |
77 | |
78 | Malcolm Beattie, C<mbeattie@sable.ox.ac.uk> |
79 | |
80 | =cut |