1N/A=head1 NAME
1N/A
1N/Aperlref - Perl references and nested data structures
1N/A
1N/A=head1 NOTE
1N/A
1N/AThis is complete documentation about all aspects of references.
1N/AFor a shorter, tutorial introduction to just the essential features,
1N/Asee L<perlreftut>.
1N/A
1N/A=head1 DESCRIPTION
1N/A
1N/ABefore release 5 of Perl it was difficult to represent complex data
1N/Astructures, because all references had to be symbolic--and even then
1N/Ait was difficult to refer to a variable instead of a symbol table entry.
1N/APerl now not only makes it easier to use symbolic references to variables,
1N/Abut also lets you have "hard" references to any piece of data or code.
1N/AAny scalar may hold a hard reference. Because arrays and hashes contain
1N/Ascalars, you can now easily build arrays of arrays, arrays of hashes,
1N/Ahashes of arrays, arrays of hashes of functions, and so on.
1N/A
1N/AHard references are smart--they keep track of reference counts for you,
1N/Aautomatically freeing the thing referred to when its reference count goes
1N/Ato zero. (Reference counts for values in self-referential or
1N/Acyclic data structures may not go to zero without a little help; see
1N/AL<perlobj/"Two-Phased Garbage Collection"> for a detailed explanation.)
1N/AIf that thing happens to be an object, the object is destructed. See
1N/AL<perlobj> for more about objects. (In a sense, everything in Perl is an
1N/Aobject, but we usually reserve the word for references to objects that
1N/Ahave been officially "blessed" into a class package.)
1N/A
1N/ASymbolic references are names of variables or other objects, just as a
1N/Asymbolic link in a Unix filesystem contains merely the name of a file.
1N/AThe C<*glob> notation is something of a symbolic reference. (Symbolic
1N/Areferences are sometimes called "soft references", but please don't call
1N/Athem that; references are confusing enough without useless synonyms.)
1N/A
1N/AIn contrast, hard references are more like hard links in a Unix file
1N/Asystem: They are used to access an underlying object without concern for
1N/Awhat its (other) name is. When the word "reference" is used without an
1N/Aadjective, as in the following paragraph, it is usually talking about a
1N/Ahard reference.
1N/A
1N/AReferences are easy to use in Perl. There is just one overriding
1N/Aprinciple: Perl does no implicit referencing or dereferencing. When a
1N/Ascalar is holding a reference, it always behaves as a simple scalar. It
1N/Adoesn't magically start being an array or hash or subroutine; you have to
1N/Atell it explicitly to do so, by dereferencing it.
1N/A
1N/A=head2 Making References
1N/A
1N/AReferences can be created in several ways.
1N/A
1N/A=over 4
1N/A
1N/A=item 1.
1N/A
1N/ABy using the backslash operator on a variable, subroutine, or value.
1N/A(This works much like the & (address-of) operator in C.)
1N/AThis typically creates I<another> reference to a variable, because
1N/Athere's already a reference to the variable in the symbol table. But
1N/Athe symbol table reference might go away, and you'll still have the
1N/Areference that the backslash returned. Here are some examples:
1N/A
1N/A $scalarref = \$foo;
1N/A $arrayref = \@ARGV;
1N/A $hashref = \%ENV;
1N/A $coderef = \&handler;
1N/A $globref = \*foo;
1N/A
1N/AIt isn't possible to create a true reference to an IO handle (filehandle
1N/Aor dirhandle) using the backslash operator. The most you can get is a
1N/Areference to a typeglob, which is actually a complete symbol table entry.
1N/ABut see the explanation of the C<*foo{THING}> syntax below. However,
1N/Ayou can still use type globs and globrefs as though they were IO handles.
1N/A
1N/A=item 2.
1N/A
1N/AA reference to an anonymous array can be created using square
1N/Abrackets:
1N/A
1N/A $arrayref = [1, 2, ['a', 'b', 'c']];
1N/A
1N/AHere we've created a reference to an anonymous array of three elements
1N/Awhose final element is itself a reference to another anonymous array of three
1N/Aelements. (The multidimensional syntax described later can be used to
1N/Aaccess this. For example, after the above, C<< $arrayref->[2][1] >> would have
1N/Athe value "b".)
1N/A
1N/ATaking a reference to an enumerated list is not the same
1N/Aas using square brackets--instead it's the same as creating
1N/Aa list of references!
1N/A
1N/A @list = (\$a, \@b, \%c);
1N/A @list = \($a, @b, %c); # same thing!
1N/A
1N/AAs a special case, C<\(@foo)> returns a list of references to the contents
1N/Aof C<@foo>, not a reference to C<@foo> itself. Likewise for C<%foo>,
1N/Aexcept that the key references are to copies (since the keys are just
1N/Astrings rather than full-fledged scalars).
1N/A
1N/A=item 3.
1N/A
1N/AA reference to an anonymous hash can be created using curly
1N/Abrackets:
1N/A
1N/A $hashref = {
1N/A 'Adam' => 'Eve',
1N/A 'Clyde' => 'Bonnie',
1N/A };
1N/A
1N/AAnonymous hash and array composers like these can be intermixed freely to
1N/Aproduce as complicated a structure as you want. The multidimensional
1N/Asyntax described below works for these too. The values above are
1N/Aliterals, but variables and expressions would work just as well, because
1N/Aassignment operators in Perl (even within local() or my()) are executable
1N/Astatements, not compile-time declarations.
1N/A
1N/ABecause curly brackets (braces) are used for several other things
1N/Aincluding BLOCKs, you may occasionally have to disambiguate braces at the
1N/Abeginning of a statement by putting a C<+> or a C<return> in front so
1N/Athat Perl realizes the opening brace isn't starting a BLOCK. The economy and
1N/Amnemonic value of using curlies is deemed worth this occasional extra
1N/Ahassle.
1N/A
1N/AFor example, if you wanted a function to make a new hash and return a
1N/Areference to it, you have these options:
1N/A
1N/A sub hashem { { @_ } } # silently wrong
1N/A sub hashem { +{ @_ } } # ok
1N/A sub hashem { return { @_ } } # ok
1N/A
1N/AOn the other hand, if you want the other meaning, you can do this:
1N/A
1N/A sub showem { { @_ } } # ambiguous (currently ok, but may change)
1N/A sub showem { {; @_ } } # ok
1N/A sub showem { { return @_ } } # ok
1N/A
1N/AThe leading C<+{> and C<{;> always serve to disambiguate
1N/Athe expression to mean either the HASH reference, or the BLOCK.
1N/A
1N/A=item 4.
1N/A
1N/AA reference to an anonymous subroutine can be created by using
1N/AC<sub> without a subname:
1N/A
1N/A $coderef = sub { print "Boink!\n" };
1N/A
1N/ANote the semicolon. Except for the code
1N/Ainside not being immediately executed, a C<sub {}> is not so much a
1N/Adeclaration as it is an operator, like C<do{}> or C<eval{}>. (However, no
1N/Amatter how many times you execute that particular line (unless you're in an
1N/AC<eval("...")>), $coderef will still have a reference to the I<same>
1N/Aanonymous subroutine.)
1N/A
1N/AAnonymous subroutines act as closures with respect to my() variables,
1N/Athat is, variables lexically visible within the current scope. Closure
1N/Ais a notion out of the Lisp world that says if you define an anonymous
1N/Afunction in a particular lexical context, it pretends to run in that
1N/Acontext even when it's called outside the context.
1N/A
1N/AIn human terms, it's a funny way of passing arguments to a subroutine when
1N/Ayou define it as well as when you call it. It's useful for setting up
1N/Alittle bits of code to run later, such as callbacks. You can even
1N/Ado object-oriented stuff with it, though Perl already provides a different
1N/Amechanism to do that--see L<perlobj>.
1N/A
1N/AYou might also think of closure as a way to write a subroutine
1N/Atemplate without using eval(). Here's a small example of how
1N/Aclosures work:
1N/A
1N/A sub newprint {
1N/A my $x = shift;
1N/A return sub { my $y = shift; print "$x, $y!\n"; };
1N/A }
1N/A $h = newprint("Howdy");
1N/A $g = newprint("Greetings");
1N/A
1N/A # Time passes...
1N/A
1N/A &$h("world");
1N/A &$g("earthlings");
1N/A
1N/AThis prints
1N/A
1N/A Howdy, world!
1N/A Greetings, earthlings!
1N/A
1N/ANote particularly that $x continues to refer to the value passed
1N/Ainto newprint() I<despite> "my $x" having gone out of scope by the
1N/Atime the anonymous subroutine runs. That's what a closure is all
1N/Aabout.
1N/A
1N/AThis applies only to lexical variables, by the way. Dynamic variables
1N/Acontinue to work as they have always worked. Closure is not something
1N/Athat most Perl programmers need trouble themselves about to begin with.
1N/A
1N/A=item 5.
1N/A
1N/AReferences are often returned by special subroutines called constructors.
1N/APerl objects are just references to a special type of object that happens to know
1N/Awhich package it's associated with. Constructors are just special
1N/Asubroutines that know how to create that association. They do so by
1N/Astarting with an ordinary reference, and it remains an ordinary reference
1N/Aeven while it's also being an object. Constructors are often
1N/Anamed new() and called indirectly:
1N/A
1N/A $objref = new Doggie (Tail => 'short', Ears => 'long');
1N/A
1N/ABut don't have to be:
1N/A
1N/A $objref = Doggie->new(Tail => 'short', Ears => 'long');
1N/A
1N/A use Term::Cap;
1N/A $terminal = Term::Cap->Tgetent( { OSPEED => 9600 });
1N/A
1N/A use Tk;
1N/A $main = MainWindow->new();
1N/A $menubar = $main->Frame(-relief => "raised",
1N/A -borderwidth => 2)
1N/A
1N/A=item 6.
1N/A
1N/AReferences of the appropriate type can spring into existence if you
1N/Adereference them in a context that assumes they exist. Because we haven't
1N/Atalked about dereferencing yet, we can't show you any examples yet.
1N/A
1N/A=item 7.
1N/A
1N/AA reference can be created by using a special syntax, lovingly known as
1N/Athe *foo{THING} syntax. *foo{THING} returns a reference to the THING
1N/Aslot in *foo (which is the symbol table entry which holds everything
1N/Aknown as foo).
1N/A
1N/A $scalarref = *foo{SCALAR};
1N/A $arrayref = *ARGV{ARRAY};
1N/A $hashref = *ENV{HASH};
1N/A $coderef = *handler{CODE};
1N/A $ioref = *STDIN{IO};
1N/A $globref = *foo{GLOB};
1N/A
1N/AAll of these are self-explanatory except for C<*foo{IO}>. It returns
1N/Athe IO handle, used for file handles (L<perlfunc/open>), sockets
1N/A(L<perlfunc/socket> and L<perlfunc/socketpair>), and directory
1N/Ahandles (L<perlfunc/opendir>). For compatibility with previous
1N/Aversions of Perl, C<*foo{FILEHANDLE}> is a synonym for C<*foo{IO}>, though it
1N/Ais deprecated as of 5.8.0. If deprecation warnings are in effect, it will warn
1N/Aof its use.
1N/A
1N/AC<*foo{THING}> returns undef if that particular THING hasn't been used yet,
1N/Aexcept in the case of scalars. C<*foo{SCALAR}> returns a reference to an
1N/Aanonymous scalar if $foo hasn't been used yet. This might change in a
1N/Afuture release.
1N/A
1N/AC<*foo{IO}> is an alternative to the C<*HANDLE> mechanism given in
1N/AL<perldata/"Typeglobs and Filehandles"> for passing filehandles
1N/Ainto or out of subroutines, or storing into larger data structures.
1N/AIts disadvantage is that it won't create a new filehandle for you.
1N/AIts advantage is that you have less risk of clobbering more than
1N/Ayou want to with a typeglob assignment. (It still conflates file
1N/Aand directory handles, though.) However, if you assign the incoming
1N/Avalue to a scalar instead of a typeglob as we do in the examples
1N/Abelow, there's no risk of that happening.
1N/A
1N/A splutter(*STDOUT); # pass the whole glob
1N/A splutter(*STDOUT{IO}); # pass both file and dir handles
1N/A
1N/A sub splutter {
1N/A my $fh = shift;
1N/A print $fh "her um well a hmmm\n";
1N/A }
1N/A
1N/A $rec = get_rec(*STDIN); # pass the whole glob
1N/A $rec = get_rec(*STDIN{IO}); # pass both file and dir handles
1N/A
1N/A sub get_rec {
1N/A my $fh = shift;
1N/A return scalar <$fh>;
1N/A }
1N/A
1N/A=back
1N/A
1N/A=head2 Using References
1N/A
1N/AThat's it for creating references. By now you're probably dying to
1N/Aknow how to use references to get back to your long-lost data. There
1N/Aare several basic methods.
1N/A
1N/A=over 4
1N/A
1N/A=item 1.
1N/A
1N/AAnywhere you'd put an identifier (or chain of identifiers) as part
1N/Aof a variable or subroutine name, you can replace the identifier with
1N/Aa simple scalar variable containing a reference of the correct type:
1N/A
1N/A $bar = $$scalarref;
1N/A push(@$arrayref, $filename);
1N/A $$arrayref[0] = "January";
1N/A $$hashref{"KEY"} = "VALUE";
1N/A &$coderef(1,2,3);
1N/A print $globref "output\n";
1N/A
1N/AIt's important to understand that we are specifically I<not> dereferencing
1N/AC<$arrayref[0]> or C<$hashref{"KEY"}> there. The dereference of the
1N/Ascalar variable happens I<before> it does any key lookups. Anything more
1N/Acomplicated than a simple scalar variable must use methods 2 or 3 below.
1N/AHowever, a "simple scalar" includes an identifier that itself uses method
1N/A1 recursively. Therefore, the following prints "howdy".
1N/A
1N/A $refrefref = \\\"howdy";
1N/A print $$$$refrefref;
1N/A
1N/A=item 2.
1N/A
1N/AAnywhere you'd put an identifier (or chain of identifiers) as part of a
1N/Avariable or subroutine name, you can replace the identifier with a
1N/ABLOCK returning a reference of the correct type. In other words, the
1N/Aprevious examples could be written like this:
1N/A
1N/A $bar = ${$scalarref};
1N/A push(@{$arrayref}, $filename);
1N/A ${$arrayref}[0] = "January";
1N/A ${$hashref}{"KEY"} = "VALUE";
1N/A &{$coderef}(1,2,3);
1N/A $globref->print("output\n"); # iff IO::Handle is loaded
1N/A
1N/AAdmittedly, it's a little silly to use the curlies in this case, but
1N/Athe BLOCK can contain any arbitrary expression, in particular,
1N/Asubscripted expressions:
1N/A
1N/A &{ $dispatch{$index} }(1,2,3); # call correct routine
1N/A
1N/ABecause of being able to omit the curlies for the simple case of C<$$x>,
1N/Apeople often make the mistake of viewing the dereferencing symbols as
1N/Aproper operators, and wonder about their precedence. If they were,
1N/Athough, you could use parentheses instead of braces. That's not the case.
1N/AConsider the difference below; case 0 is a short-hand version of case 1,
1N/AI<not> case 2:
1N/A
1N/A $$hashref{"KEY"} = "VALUE"; # CASE 0
1N/A ${$hashref}{"KEY"} = "VALUE"; # CASE 1
1N/A ${$hashref{"KEY"}} = "VALUE"; # CASE 2
1N/A ${$hashref->{"KEY"}} = "VALUE"; # CASE 3
1N/A
1N/ACase 2 is also deceptive in that you're accessing a variable
1N/Acalled %hashref, not dereferencing through $hashref to the hash
1N/Ait's presumably referencing. That would be case 3.
1N/A
1N/A=item 3.
1N/A
1N/ASubroutine calls and lookups of individual array elements arise often
1N/Aenough that it gets cumbersome to use method 2. As a form of
1N/Asyntactic sugar, the examples for method 2 may be written:
1N/A
1N/A $arrayref->[0] = "January"; # Array element
1N/A $hashref->{"KEY"} = "VALUE"; # Hash element
1N/A $coderef->(1,2,3); # Subroutine call
1N/A
1N/AThe left side of the arrow can be any expression returning a reference,
1N/Aincluding a previous dereference. Note that C<$array[$x]> is I<not> the
1N/Asame thing as C<< $array->[$x] >> here:
1N/A
1N/A $array[$x]->{"foo"}->[0] = "January";
1N/A
1N/AThis is one of the cases we mentioned earlier in which references could
1N/Aspring into existence when in an lvalue context. Before this
1N/Astatement, C<$array[$x]> may have been undefined. If so, it's
1N/Aautomatically defined with a hash reference so that we can look up
1N/AC<{"foo"}> in it. Likewise C<< $array[$x]->{"foo"} >> will automatically get
1N/Adefined with an array reference so that we can look up C<[0]> in it.
1N/AThis process is called I<autovivification>.
1N/A
1N/AOne more thing here. The arrow is optional I<between> brackets
1N/Asubscripts, so you can shrink the above down to
1N/A
1N/A $array[$x]{"foo"}[0] = "January";
1N/A
1N/AWhich, in the degenerate case of using only ordinary arrays, gives you
1N/Amultidimensional arrays just like C's:
1N/A
1N/A $score[$x][$y][$z] += 42;
1N/A
1N/AWell, okay, not entirely like C's arrays, actually. C doesn't know how
1N/Ato grow its arrays on demand. Perl does.
1N/A
1N/A=item 4.
1N/A
1N/AIf a reference happens to be a reference to an object, then there are
1N/Aprobably methods to access the things referred to, and you should probably
1N/Astick to those methods unless you're in the class package that defines the
1N/Aobject's methods. In other words, be nice, and don't violate the object's
1N/Aencapsulation without a very good reason. Perl does not enforce
1N/Aencapsulation. We are not totalitarians here. We do expect some basic
1N/Acivility though.
1N/A
1N/A=back
1N/A
1N/AUsing a string or number as a reference produces a symbolic reference,
1N/Aas explained above. Using a reference as a number produces an
1N/Ainteger representing its storage location in memory. The only
1N/Auseful thing to be done with this is to compare two references
1N/Anumerically to see whether they refer to the same location.
1N/A
1N/A if ($ref1 == $ref2) { # cheap numeric compare of references
1N/A print "refs 1 and 2 refer to the same thing\n";
1N/A }
1N/A
1N/AUsing a reference as a string produces both its referent's type,
1N/Aincluding any package blessing as described in L<perlobj>, as well
1N/Aas the numeric address expressed in hex. The ref() operator returns
1N/Ajust the type of thing the reference is pointing to, without the
1N/Aaddress. See L<perlfunc/ref> for details and examples of its use.
1N/A
1N/AThe bless() operator may be used to associate the object a reference
1N/Apoints to with a package functioning as an object class. See L<perlobj>.
1N/A
1N/AA typeglob may be dereferenced the same way a reference can, because
1N/Athe dereference syntax always indicates the type of reference desired.
1N/ASo C<${*foo}> and C<${\$foo}> both indicate the same scalar variable.
1N/A
1N/AHere's a trick for interpolating a subroutine call into a string:
1N/A
1N/A print "My sub returned @{[mysub(1,2,3)]} that time.\n";
1N/A
1N/AThe way it works is that when the C<@{...}> is seen in the double-quoted
1N/Astring, it's evaluated as a block. The block creates a reference to an
1N/Aanonymous array containing the results of the call to C<mysub(1,2,3)>. So
1N/Athe whole block returns a reference to an array, which is then
1N/Adereferenced by C<@{...}> and stuck into the double-quoted string. This
1N/Achicanery is also useful for arbitrary expressions:
1N/A
1N/A print "That yields @{[$n + 5]} widgets\n";
1N/A
1N/A=head2 Symbolic references
1N/A
1N/AWe said that references spring into existence as necessary if they are
1N/Aundefined, but we didn't say what happens if a value used as a
1N/Areference is already defined, but I<isn't> a hard reference. If you
1N/Ause it as a reference, it'll be treated as a symbolic
1N/Areference. That is, the value of the scalar is taken to be the I<name>
1N/Aof a variable, rather than a direct link to a (possibly) anonymous
1N/Avalue.
1N/A
1N/APeople frequently expect it to work like this. So it does.
1N/A
1N/A $name = "foo";
1N/A $$name = 1; # Sets $foo
1N/A ${$name} = 2; # Sets $foo
1N/A ${$name x 2} = 3; # Sets $foofoo
1N/A $name->[0] = 4; # Sets $foo[0]
1N/A @$name = (); # Clears @foo
1N/A &$name(); # Calls &foo() (as in Perl 4)
1N/A $pack = "THAT";
1N/A ${"${pack}::$name"} = 5; # Sets $THAT::foo without eval
1N/A
1N/AThis is powerful, and slightly dangerous, in that it's possible
1N/Ato intend (with the utmost sincerity) to use a hard reference, and
1N/Aaccidentally use a symbolic reference instead. To protect against
1N/Athat, you can say
1N/A
1N/A use strict 'refs';
1N/A
1N/Aand then only hard references will be allowed for the rest of the enclosing
1N/Ablock. An inner block may countermand that with
1N/A
1N/A no strict 'refs';
1N/A
1N/AOnly package variables (globals, even if localized) are visible to
1N/Asymbolic references. Lexical variables (declared with my()) aren't in
1N/Aa symbol table, and thus are invisible to this mechanism. For example:
1N/A
1N/A local $value = 10;
1N/A $ref = "value";
1N/A {
1N/A my $value = 20;
1N/A print $$ref;
1N/A }
1N/A
1N/AThis will still print 10, not 20. Remember that local() affects package
1N/Avariables, which are all "global" to the package.
1N/A
1N/A=head2 Not-so-symbolic references
1N/A
1N/AA new feature contributing to readability in perl version 5.001 is that the
1N/Abrackets around a symbolic reference behave more like quotes, just as they
1N/Aalways have within a string. That is,
1N/A
1N/A $push = "pop on ";
1N/A print "${push}over";
1N/A
1N/Ahas always meant to print "pop on over", even though push is
1N/Aa reserved word. This has been generalized to work the same outside
1N/Aof quotes, so that
1N/A
1N/A print ${push} . "over";
1N/A
1N/Aand even
1N/A
1N/A print ${ push } . "over";
1N/A
1N/Awill have the same effect. (This would have been a syntax error in
1N/APerl 5.000, though Perl 4 allowed it in the spaceless form.) This
1N/Aconstruct is I<not> considered to be a symbolic reference when you're
1N/Ausing strict refs:
1N/A
1N/A use strict 'refs';
1N/A ${ bareword }; # Okay, means $bareword.
1N/A ${ "bareword" }; # Error, symbolic reference.
1N/A
1N/ASimilarly, because of all the subscripting that is done using single
1N/Awords, we've applied the same rule to any bareword that is used for
1N/Asubscripting a hash. So now, instead of writing
1N/A
1N/A $array{ "aaa" }{ "bbb" }{ "ccc" }
1N/A
1N/Ayou can write just
1N/A
1N/A $array{ aaa }{ bbb }{ ccc }
1N/A
1N/Aand not worry about whether the subscripts are reserved words. In the
1N/Arare event that you do wish to do something like
1N/A
1N/A $array{ shift }
1N/A
1N/Ayou can force interpretation as a reserved word by adding anything that
1N/Amakes it more than a bareword:
1N/A
1N/A $array{ shift() }
1N/A $array{ +shift }
1N/A $array{ shift @_ }
1N/A
1N/AThe C<use warnings> pragma or the B<-w> switch will warn you if it
1N/Ainterprets a reserved word as a string.
1N/ABut it will no longer warn you about using lowercase words, because the
1N/Astring is effectively quoted.
1N/A
1N/A=head2 Pseudo-hashes: Using an array as a hash
1N/A
1N/AB<WARNING>: This section describes an experimental feature. Details may
1N/Achange without notice in future versions.
1N/A
1N/AB<NOTE>: The current user-visible implementation of pseudo-hashes
1N/A(the weird use of the first array element) is deprecated starting from
1N/APerl 5.8.0 and will be removed in Perl 5.10.0, and the feature will be
1N/Aimplemented differently. Not only is the current interface rather ugly,
1N/Abut the current implementation slows down normal array and hash use quite
1N/Anoticeably. The 'fields' pragma interface will remain available.
1N/A
1N/ABeginning with release 5.005 of Perl, you may use an array reference
1N/Ain some contexts that would normally require a hash reference. This
1N/Aallows you to access array elements using symbolic names, as if they
1N/Awere fields in a structure.
1N/A
1N/AFor this to work, the array must contain extra information. The first
1N/Aelement of the array has to be a hash reference that maps field names
1N/Ato array indices. Here is an example:
1N/A
1N/A $struct = [{foo => 1, bar => 2}, "FOO", "BAR"];
1N/A
1N/A $struct->{foo}; # same as $struct->[1], i.e. "FOO"
1N/A $struct->{bar}; # same as $struct->[2], i.e. "BAR"
1N/A
1N/A keys %$struct; # will return ("foo", "bar") in some order
1N/A values %$struct; # will return ("FOO", "BAR") in same some order
1N/A
1N/A while (my($k,$v) = each %$struct) {
1N/A print "$k => $v\n";
1N/A }
1N/A
1N/APerl will raise an exception if you try to access nonexistent fields.
1N/ATo avoid inconsistencies, always use the fields::phash() function
1N/Aprovided by the C<fields> pragma.
1N/A
1N/A use fields;
1N/A $pseudohash = fields::phash(foo => "FOO", bar => "BAR");
1N/A
1N/AFor better performance, Perl can also do the translation from field
1N/Anames to array indices at compile time for typed object references.
1N/ASee L<fields>.
1N/A
1N/AThere are two ways to check for the existence of a key in a
1N/Apseudo-hash. The first is to use exists(). This checks to see if the
1N/Agiven field has ever been set. It acts this way to match the behavior
1N/Aof a regular hash. For instance:
1N/A
1N/A use fields;
1N/A $phash = fields::phash([qw(foo bar pants)], ['FOO']);
1N/A $phash->{pants} = undef;
1N/A
1N/A print exists $phash->{foo}; # true, 'foo' was set in the declaration
1N/A print exists $phash->{bar}; # false, 'bar' has not been used.
1N/A print exists $phash->{pants}; # true, your 'pants' have been touched
1N/A
1N/AThe second is to use exists() on the hash reference sitting in the
1N/Afirst array element. This checks to see if the given key is a valid
1N/Afield in the pseudo-hash.
1N/A
1N/A print exists $phash->[0]{bar}; # true, 'bar' is a valid field
1N/A print exists $phash->[0]{shoes};# false, 'shoes' can't be used
1N/A
1N/Adelete() on a pseudo-hash element only deletes the value corresponding
1N/Ato the key, not the key itself. To delete the key, you'll have to
1N/Aexplicitly delete it from the first hash element.
1N/A
1N/A print delete $phash->{foo}; # prints $phash->[1], "FOO"
1N/A print exists $phash->{foo}; # false
1N/A print exists $phash->[0]{foo}; # true, key still exists
1N/A print delete $phash->[0]{foo}; # now key is gone
1N/A print $phash->{foo}; # runtime exception
1N/A
1N/A=head2 Function Templates
1N/A
1N/AAs explained above, a closure is an anonymous function with access to the
1N/Alexical variables visible when that function was compiled. It retains
1N/Aaccess to those variables even though it doesn't get run until later,
1N/Asuch as in a signal handler or a Tk callback.
1N/A
1N/AUsing a closure as a function template allows us to generate many functions
1N/Athat act similarly. Suppose you wanted functions named after the colors
1N/Athat generated HTML font changes for the various colors:
1N/A
1N/A print "Be ", red("careful"), "with that ", green("light");
1N/A
1N/AThe red() and green() functions would be similar. To create these,
1N/Awe'll assign a closure to a typeglob of the name of the function we're
1N/Atrying to build.
1N/A
1N/A @colors = qw(red blue green yellow orange purple violet);
1N/A for my $name (@colors) {
1N/A no strict 'refs'; # allow symbol table manipulation
1N/A *$name = *{uc $name} = sub { "<FONT COLOR='$name'>@_</FONT>" };
1N/A }
1N/A
1N/ANow all those different functions appear to exist independently. You can
1N/Acall red(), RED(), blue(), BLUE(), green(), etc. This technique saves on
1N/Aboth compile time and memory use, and is less error-prone as well, since
1N/Asyntax checks happen at compile time. It's critical that any variables in
1N/Athe anonymous subroutine be lexicals in order to create a proper closure.
1N/AThat's the reasons for the C<my> on the loop iteration variable.
1N/A
1N/AThis is one of the only places where giving a prototype to a closure makes
1N/Amuch sense. If you wanted to impose scalar context on the arguments of
1N/Athese functions (probably not a wise idea for this particular example),
1N/Ayou could have written it this way instead:
1N/A
1N/A *$name = sub ($) { "<FONT COLOR='$name'>$_[0]</FONT>" };
1N/A
1N/AHowever, since prototype checking happens at compile time, the assignment
1N/Aabove happens too late to be of much use. You could address this by
1N/Aputting the whole loop of assignments within a BEGIN block, forcing it
1N/Ato occur during compilation.
1N/A
1N/AAccess to lexicals that change over type--like those in the C<for> loop
1N/Aabove--only works with closures, not general subroutines. In the general
1N/Acase, then, named subroutines do not nest properly, although anonymous
1N/Aones do. If you are accustomed to using nested subroutines in other
1N/Aprogramming languages with their own private variables, you'll have to
1N/Awork at it a bit in Perl. The intuitive coding of this type of thing
1N/Aincurs mysterious warnings about ``will not stay shared''. For example,
1N/Athis won't work:
1N/A
1N/A sub outer {
1N/A my $x = $_[0] + 35;
1N/A sub inner { return $x * 19 } # WRONG
1N/A return $x + inner();
1N/A }
1N/A
1N/AA work-around is the following:
1N/A
1N/A sub outer {
1N/A my $x = $_[0] + 35;
1N/A local *inner = sub { return $x * 19 };
1N/A return $x + inner();
1N/A }
1N/A
1N/ANow inner() can only be called from within outer(), because of the
1N/Atemporary assignments of the closure (anonymous subroutine). But when
1N/Ait does, it has normal access to the lexical variable $x from the scope
1N/Aof outer().
1N/A
1N/AThis has the interesting effect of creating a function local to another
1N/Afunction, something not normally supported in Perl.
1N/A
1N/A=head1 WARNING
1N/A
1N/AYou may not (usefully) use a reference as the key to a hash. It will be
1N/Aconverted into a string:
1N/A
1N/A $x{ \$a } = $a;
1N/A
1N/AIf you try to dereference the key, it won't do a hard dereference, and
1N/Ayou won't accomplish what you're attempting. You might want to do something
1N/Amore like
1N/A
1N/A $r = \@a;
1N/A $x{ $r } = $r;
1N/A
1N/AAnd then at least you can use the values(), which will be
1N/Areal refs, instead of the keys(), which won't.
1N/A
1N/AThe standard Tie::RefHash module provides a convenient workaround to this.
1N/A
1N/A=head1 SEE ALSO
1N/A
1N/ABesides the obvious documents, source code can be instructive.
1N/ASome pathological examples of the use of references can be found
1N/Ain the F<t/op/ref.t> regression test in the Perl source directory.
1N/A
1N/ASee also L<perldsc> and L<perllol> for how to use references to create
1N/Acomplex data structures, and L<perltoot>, L<perlobj>, and L<perlbot>
1N/Afor how to use them to create objects.