X<reference, hard> X<hard reference>
References are easy to use in Perl. There is just one overriding
-principle: Perl does no implicit referencing or dereferencing. When a
-scalar is holding a reference, it always behaves as a simple scalar. It
-doesn't magically start being an array or hash or subroutine; you have to
-tell it explicitly to do so, by dereferencing it.
-
-References are easy to use in Perl. There is just one overriding
principle: in general, Perl does no implicit referencing or dereferencing.
When a scalar is holding a reference, it always behaves as a simple scalar.
It doesn't magically start being an array or hash or subroutine; you have to
On the other hand, if you want the other meaning, you can do this:
- sub showem { { @_ } } # ambiguous (currently ok, but may change)
+ sub showem { { @_ } } # ambiguous (currently ok,
+ # but may change)
sub showem { {; @_ } } # ok
sub showem { { return @_ } } # ok
$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
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.