perldoc perlref

       Making References

       References can be created in several ways.

       1.  By using the backslash operator on a variable, subroutine, or
value.  (This works much like the & (address-of) opera-
           tor in C.)  This typically creates another reference to a
variable, because there’s already a reference to the vari-
           able in the symbol table.  But the symbol table reference might
go away, and you’ll still have the reference that the
           backslash returned.  Here are some examples:

               $scalarref = \$foo;
               $arrayref  = \...@argv;
               $hashref   = \%ENV;
               $coderef   = \&handler;
               $globref   = \*foo;

           It isn’t possible to create a true reference to an IO handle
(filehandle or dirhandle) using the backslash operator.
           The most you can get is a reference to a typeglob, which is
actually a complete symbol table entry.  But see the
           explanation of the *foo{THING} syntax below.  However, you can
still use type globs and globrefs as though they were
           IO handles.

       2.  A reference to an anonymous array can be created using square
brackets:

               $arrayref = [1, 2, [’a’, ’b’, ’c’]];

           Here we’ve created a reference to an anonymous array of three
elements whose final element is itself a reference to
           another anonymous array of three elements.  (The multidimensional
syntax described later can be used to access this.
           For example, after the above, "$arrayref->[2][1]" would have the
value "b".)

 /////////    Taking a reference to an enumerated list is not the same as
using square brackets--instead it’s the same as creating
           a list of references!

               @list = (\$a, \...@b, \%c);
               @list = \($a, @b, %c);      # same thing!

           As a special case, "\(@foo)" returns a list of references to the
contents of @foo, not a reference to @foo itself.
           Likewise for %foo, except that the key references are to copies
(since the keys are just strings rather than full-
           fledged scalars).

> -----邮件原件-----
> 发件人: Mark_Galeck [mailto:mark_galeck_spam_mag...@yahoo.com]
> 发送时间: 2009年11月日 11:53
> 收件人: beginners@perl.org
> 主题: reference to anonymous array, references last element instead??
> 
> Why does
> 
> $foobar = \("foo", "bar");
> print $$foobar;
> 
> 
> print "bar"  ??
> 
> Thank you for any insight.  Mark
> 
> 
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to