Leaked source code of windows server 2003
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.

97 lines
2.0 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 shownamearray {
  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. my $sv = $els[$i];
  22. if (class($sv) ne "SPECIAL") {
  23. printf "%s (0x%lx) %s\n", class($sv), $$sv, $sv->PVX;
  24. } else {
  25. $sv->terse;
  26. }
  27. }
  28. }
  29. sub showvaluearray {
  30. my ($name, $av) = @_;
  31. my @els = $av->ARRAY;
  32. my $count = @els;
  33. my $i;
  34. print "$name has $count entries\n";
  35. for ($i = 0; $i < $count; $i++) {
  36. print "$i: ";
  37. $els[$i]->terse;
  38. }
  39. }
  40. sub showlex {
  41. my ($objname, $namesav, $valsav) = @_;
  42. shownamearray("Pad of lexical names for $objname", $namesav);
  43. showvaluearray("Pad of lexical values for $objname", $valsav);
  44. }
  45. sub showlex_obj {
  46. my ($objname, $obj) = @_;
  47. $objname =~ s/^&main::/&/;
  48. showlex($objname, svref_2object($obj)->PADLIST->ARRAY);
  49. }
  50. sub showlex_main {
  51. showlex("comppadlist", comppadlist->ARRAY);
  52. }
  53. sub compile {
  54. my @options = @_;
  55. if (@options) {
  56. return sub {
  57. my $objname;
  58. foreach $objname (@options) {
  59. $objname = "main::$objname" unless $objname =~ /::/;
  60. eval "showlex_obj('&$objname', \\&$objname)";
  61. }
  62. }
  63. } else {
  64. return \&showlex_main;
  65. }
  66. }
  67. 1;
  68. __END__
  69. =head1 NAME
  70. B::Showlex - Show lexical variables used in functions or files
  71. =head1 SYNOPSIS
  72. perl -MO=Showlex[,SUBROUTINE] foo.pl
  73. =head1 DESCRIPTION
  74. When a subroutine name is provided in OPTIONS, prints the lexical
  75. variables used in that subroutine. Otherwise, prints the file-scope
  76. lexicals in the file.
  77. =head1 AUTHOR
  78. Malcolm Beattie, C<[email protected]>
  79. =cut