[prog] perl woes...
Elizabeth Barham
lizzy at soggytrousers.net
Sat May 31 04:16:26 EST 2003
Patricia writes:
> First, I don't understand what's happening in a statement like
>
> $tmpl->param( supplist => \@suppliers );
>
> I know what the result is - the actual value held in the hash to which
> the array refers gets pasted into a drop-down list in the template file
> I have set up. It works *fine*, and the drop-down list has all the
> right stuff in it. What I don't understand is *how*, so I can apply a
> principle elsewhere.
>
> (code samples on request - too long to post in this mail!)
>
> I guess there are a couple of things I don't understand; first, what
> "=>" does as opposed to what "->" does, but more to the point,
> what's actually happening when I'm tracking back through references
> (which I've figured out are actually memory addresses) to get back
> the stored value, which is what I want.
Hi Trish,
Its been a while since I did much coding in perl, but as I recall the
"=>" operator is a hash assignment operator with a special property:
The => digraph is mostly just a synonym for the comma operator. It's
useful for documenting arguments that come in pairs. As of release
5.001, it also forces any word to the left of it to be interpreted
as a string.
http://www.perldoc.com/perl5.6/pod/perlop.html
Here is an example of it's use:
%HoL = ( "flintstones" => [ "fred", "barney" ],
"jetsons" => [ "george", "jane", "elroy" ],
"simpsons" => [ "homer", "marge", "bart" ],
); # From: http://www.emerson.emory.edu/services/perl/perldoc/manual/perldsc.html
So, in your example:
$tmpl->param( supplist => \@suppliers );
you are associating (as in an associative array) "supplist" with a
reference to a list called suppliers. You could just as well have
done:
$tmpl->param( "supplist" => \@suppliers);
The Arrow (->) Operator
"->" is an infix dereference operator, just as it is in C and
C++. If the right side is either a [...], {...}, or a (...)
subscript, then the left side must be either a hard or symbolic
reference to an array, a hash, or a subroutine respectively. (Or
technically speaking, a location capable of holding a hard
reference, if it's an array or hash reference being used for
assignment.) See perlreftut and perlref.
Otherwise, the right side is a method name or a simple scalar
variable containing either the method name or a subroutine
reference, and the left side must be either an object (a blessed
reference) or a class name (that is, a package name). See perlobj.
http://www.perldoc.com/perl5.6/pod/perlop.html
It appears that the $tmpl is a blessed reference (object) with a
method called "param". You're calling that method with two arguments,
the first one being a string ("supplist") and the right one a
reference to a list ("suppliers"). However, the method probably
expects its arguments in pairs (as in an associated array), so using
the => operator makes sense and quietly documents what the param()
function expects.
As for \@suppliers, the "\" part before "@" just makes it into a
reference which means it can be assigned to a simple variable and the
arrow operator (->) can be used to de-reference it. It also means that
the list isn't expanded right then and there, such as when you passed
it to the "param" method. Had you not used a reference and "@suppliers"
contained ("north", "south", "east", "west"), param() would have
received: ("supplist", "north", "south", "east", "west"), but as it
is, only two arguments are passed into param, "supplist" and a
reference to the array.
But you mentioned something about using the "->" operator to get back
to what is stored there. Say you have this:
#!/usr/bin/perl -w
@suppliers = ("north", "south", "east", "west");
$x = \@suppliers;
print $x->[0] . "\n";
Then this will print "north\n". After $x is de-referenced, it becomes
a normal list, on which we use the [] operators.
hth,
Elizabeth
More information about the Programming
mailing list