Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
1.5 KiB

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