This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Document and test *glob{NAME} and *glob{PACKAGE}
authorFather Chrysostomos <sprout@cpan.org>
Sun, 18 Nov 2012 01:22:01 +0000 (17:22 -0800)
committerFather Chrysostomos <sprout@cpan.org>
Sun, 18 Nov 2012 22:02:25 +0000 (14:02 -0800)
These have been supported since *foo{THING} was added in perl 5.005.
If only I had known about these sooner....  I could have been writing
*$AUTOLOAD{NAME} all this time!

pod/perlref.pod
t/op/gv.t

index 1571613..c20d20a 100644 (file)
@@ -262,8 +262,11 @@ known as foo).
     $ioref     = *STDIN{IO};
     $globref   = *foo{GLOB};
     $formatref = *foo{FORMAT};
+    $globname  = *foo{NAME};    # "foo"
+    $pkgname   = *foo{PACKAGE}; # "main"
 
-All of these are self-explanatory except for C<*foo{IO}>.  It returns
+Most of these are self-explanatory, but C<*foo{IO}>
+deserves special attention.  It returns
 the IO handle, used for file handles (L<perlfunc/open>), sockets
 (L<perlfunc/socket> and L<perlfunc/socketpair>), and directory
 handles (L<perlfunc/opendir>).  For compatibility with previous
@@ -276,6 +279,13 @@ except in the case of scalars.  C<*foo{SCALAR}> returns a reference to an
 anonymous scalar if $foo hasn't been used yet.  This might change in a
 future release.
 
+C<*foo{NAME}> and C<*foo{PACKAGE}> are the exception, in that they return
+strings, rather than references.  These return the package and name of the
+typeglob itself, rather than one that has been assigned to it.  So, after
+C<*foo=*Foo::bar>, C<*foo> will become "*Foo::bar" when used as a string,
+but C<*foo{PACKAGE}> and C<*foo{NAME}> will continue to produce "main" and
+"foo", respectively.
+
 C<*foo{IO}> is an alternative to the C<*HANDLE> mechanism given in
 L<perldata/"Typeglobs and Filehandles"> for passing filehandles
 into or out of subroutines, or storing into larger data structures.
index e2eae33..34cc922 100644 (file)
--- a/t/op/gv.t
+++ b/t/op/gv.t
@@ -12,7 +12,7 @@ BEGIN {
 
 use warnings;
 
-plan( tests => 239 );
+plan( tests => 241 );
 
 # type coercion on assignment
 $foo = 'foo';
@@ -185,6 +185,8 @@ is (*{*x{GLOB}}, "*main::STDOUT");
     curr_test(++$test);
 }
 
+is *x{NAME}, 'x', '*foo{NAME}';
+is *x{PACKAGE}, 'main', '*foo{PACKAGE}';
 
 {
     # test if defined() doesn't create any new symbols