chen li am Sonntag, 28. Mai 2006 00.56:
[...]
> Thank you all for the reply.
>
>
> Based on what I learn the regular method to defer a
> hash reference to get specific value takes this
> format:
>
> $ref_hash->{key1}
yes, if $ref_hash is a hash reference, you get the value associated to the
key 'key1'.
Try the following script, it shows you the structure of $ref_hash. If you are
ever unclear of the structure of a variable, use Data::Dumper to inspect it.
As you can note in the output, the definition is exactly the same as in line
[1] (apart from the order and the variable name).
#!/usr/bin/perl
use strict; use warnings;
my $ref_hash={key1=>'value1', key2=>'value2'}; # [1]
use Data::Dumper;
print Data::Dumper::Dumper $ref_hash;
> but in this line
> $_[0]->{_name}= $_[1] if defined $_[1]
>
> the format is
> array element->{_name}
yes, because the array element $_[0] (the first element in the array @_)
consists of a hash reference, and the -> operator accesses this hash
reference.
At the point the -> operator does it's work, the fact that the hash reference
is part of an array is no more relevant.
Compare the following script with the above one - the output is exactly the
same.
#!/usr/bin/perl
use strict; use warnings;
my $ref_hash={key1=>'value1', key2=>'value2'}; # [1]
my @arr=($ref_hash); # array with one element: a hash ref
use Data::Dumper;
print Data::Dumper::Dumper $arr[0];
> Is the middle man $ref_hash is omitted in this format?
>
> Does this what Perl really sees:
>
> $_[0]=$ref_hash;
> $ref_hash->{_name};
> and put these two lines into one line to make it short:
> $_[0]->{_name}
More ore less. I'd say: You call the name() method of your object $obj (I call
it $obj, not $ref_hash, to make clear its not only a hash reference, but a
hash reference blessed into a class, that means: a perl object) with
my $name_of_the_object=$obj->name;
This means, that $obj is passed to the name method as first argument. This
first argument is passed in the @_ array, in the first position, wich is
$_[0].
The name() method uses this $obj (and its attribute _name) by the line
$_[0]->{_name}
Run the third script:
#!/usr/bin/perl
use strict; use warnings;
### Demo 1:
my $ref_hash={key1=>'value1', key2=>'value2'}; # [1]
my @arr=($ref_hash); # array with one element: a hash ref
if ($ref_hash == $arr[0]) {
print '$ref_hash and $arr[0] are the same', "\n",
"they have the addresses '$ref_hash' and '$arr[0]'\n\n\n";
}
### Demo 2;
package My;
sub new{
my $class=shift;
print "The class name is '$class'\n";
return bless [EMAIL PROTECTED], $class
};
sub name {
print "\$_[0] has address '$_[0]'\n";
my $self=shift;
print "\$self has address '$self'\n";
return $self->{_name}
}
my $obj=My->new(_name=>'myname');
print "\$obj has address '$obj'\n";
print 'The name is `', $obj->name, "`\n";
>
Have a look into the perl documentation. You get an overview of it by typing
perldoc perl
at the command line. Look for "objects", "data structure" etc.
Dani
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>