home *** CD-ROM | disk | FTP | other *** search
/ Netrunner 2004 October / NETRUNNER0410.ISO / regular / ActivePerl-5.8.4.810-MSWin32-x86.msi / _ffc7c137a4afdc914d88804846206da3 < prev    next >
Text File  |  2004-06-01  |  5KB  |  172 lines

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