Symbol.pm 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package Symbol;
  2. =head1 NAME
  3. Symbol - manipulate Perl symbols and their names
  4. =head1 SYNOPSIS
  5. use Symbol;
  6. $sym = gensym;
  7. open($sym, "filename");
  8. $_ = <$sym>;
  9. # etc.
  10. ungensym $sym; # no effect
  11. # replace *FOO{IO} handle but not $FOO, %FOO, etc.
  12. *FOO = geniosym;
  13. print qualify("x"), "\n"; # "main::x"
  14. print qualify("x", "FOO"), "\n"; # "FOO::x"
  15. print qualify("BAR::x"), "\n"; # "BAR::x"
  16. print qualify("BAR::x", "FOO"), "\n"; # "BAR::x"
  17. print qualify("STDOUT", "FOO"), "\n"; # "main::STDOUT" (global)
  18. print qualify(\*x), "\n"; # returns \*x
  19. print qualify(\*x, "FOO"), "\n"; # returns \*x
  20. use strict refs;
  21. print { qualify_to_ref $fh } "foo!\n";
  22. $ref = qualify_to_ref $name, $pkg;
  23. use Symbol qw(delete_package);
  24. delete_package('Foo::Bar');
  25. print "deleted\n" unless exists $Foo::{'Bar::'};
  26. =head1 DESCRIPTION
  27. C<Symbol::gensym> creates an anonymous glob and returns a reference
  28. to it. Such a glob reference can be used as a file or directory
  29. handle.
  30. For backward compatibility with older implementations that didn't
  31. support anonymous globs, C<Symbol::ungensym> is also provided.
  32. But it doesn't do anything.
  33. C<Symbol::geniosym> creates an anonymous IO handle. This can be
  34. assigned into an existing glob without affecting the non-IO portions
  35. of the glob.
  36. C<Symbol::qualify> turns unqualified symbol names into qualified
  37. variable names (e.g. "myvar" -E<gt> "MyPackage::myvar"). If it is given a
  38. second parameter, C<qualify> uses it as the default package;
  39. otherwise, it uses the package of its caller. Regardless, global
  40. variable names (e.g. "STDOUT", "ENV", "SIG") are always qualified with
  41. "main::".
  42. Qualification applies only to symbol names (strings). References are
  43. left unchanged under the assumption that they are glob references,
  44. which are qualified by their nature.
  45. C<Symbol::qualify_to_ref> is just like C<Symbol::qualify> except that it
  46. returns a glob ref rather than a symbol name, so you can use the result
  47. even if C<use strict 'refs'> is in effect.
  48. C<Symbol::delete_package> wipes out a whole package namespace. Note
  49. this routine is not exported by default--you may want to import it
  50. explicitly.
  51. =head1 BUGS
  52. C<Symbol::delete_package> is a bit too powerful. It undefines every symbol that
  53. lives in the specified package. Since perl, for performance reasons, does not
  54. perform a symbol table lookup each time a function is called or a global
  55. variable is accessed, some code that has already been loaded and that makes use
  56. of symbols in package C<Foo> may stop working after you delete C<Foo>, even if
  57. you reload the C<Foo> module afterwards.
  58. =cut
  59. BEGIN { require 5.005; }
  60. require Exporter;
  61. @ISA = qw(Exporter);
  62. @EXPORT = qw(gensym ungensym qualify qualify_to_ref);
  63. @EXPORT_OK = qw(delete_package geniosym);
  64. $VERSION = '1.07';
  65. my $genpkg = "Symbol::";
  66. my $genseq = 0;
  67. my %global = map {$_ => 1} qw(ARGV ARGVOUT ENV INC SIG STDERR STDIN STDOUT);
  68. #
  69. # Note that we never _copy_ the glob; we just make a ref to it.
  70. # If we did copy it, then SVf_FAKE would be set on the copy, and
  71. # glob-specific behaviors (e.g. C<*$ref = \&func>) wouldn't work.
  72. #
  73. sub gensym () {
  74. my $name = "GEN" . $genseq++;
  75. my $ref = \*{$genpkg . $name};
  76. delete $$genpkg{$name};
  77. $ref;
  78. }
  79. sub geniosym () {
  80. my $sym = gensym();
  81. # force the IO slot to be filled
  82. select(select $sym);
  83. *$sym{IO};
  84. }
  85. sub ungensym ($) {}
  86. sub qualify ($;$) {
  87. my ($name) = @_;
  88. if (!ref($name) && index($name, '::') == -1 && index($name, "'") == -1) {
  89. my $pkg;
  90. # Global names: special character, "^xyz", or other.
  91. if ($name =~ /^(([^a-z])|(\^[a-z_]+))\z/i || $global{$name}) {
  92. # RGS 2001-11-05 : translate leading ^X to control-char
  93. $name =~ s/^\^([a-z_])/'qq(\c'.$1.')'/eei;
  94. $pkg = "main";
  95. }
  96. else {
  97. $pkg = (@_ > 1) ? $_[1] : caller;
  98. }
  99. $name = $pkg . "::" . $name;
  100. }
  101. $name;
  102. }
  103. sub qualify_to_ref ($;$) {
  104. return \*{ qualify $_[0], @_ > 1 ? $_[1] : caller };
  105. }
  106. #
  107. # of Safe.pm lineage
  108. #
  109. sub delete_package ($) {
  110. my $pkg = shift;
  111. # expand to full symbol table name if needed
  112. unless ($pkg =~ /^main::.*::$/) {
  113. $pkg = "main$pkg" if $pkg =~ /^::/;
  114. $pkg = "main::$pkg" unless $pkg =~ /^main::/;
  115. $pkg .= '::' unless $pkg =~ /::$/;
  116. }
  117. my($stem, $leaf) = $pkg =~ m/(.*::)(\w+::)$/;
  118. my $stem_symtab = *{$stem}{HASH};
  119. return unless defined $stem_symtab and exists $stem_symtab->{$leaf};
  120. # free all the symbols in the package
  121. my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH};
  122. foreach my $name (keys %$leaf_symtab) {
  123. undef *{$pkg . $name};
  124. }
  125. # delete the symbol table
  126. %$leaf_symtab = ();
  127. delete $stem_symtab->{$leaf};
  128. }
  129. 1;