Re: accessor problem in OO



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 {@_}, $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
.



Relevant Pages

  • Problem using Perl web service with C# client.
    ... I have a perl web service with a method called ... "Detail" that returns a strucure (hash reference to be exact). ... works fine when consumed by a Perl client, but when I try to consume ... public class TestDetailService: ...
    (microsoft.public.dotnet.framework.webservices)
  • Re: Best place to learn perl?
    ... > # do perl stuff here ... reference to an array, and we're dereferencing it. ... Each element of THAT array points to another hash reference. ...
    (comp.lang.perl.misc)
  • Re: XML::Simple Problem
    ... I am somewhat of a newbie at perl so I do nto know how to check if the ... convert the hash reference into useable perl variables you can ... If foo turns out to be a list reference and you want element 2 of the list, ... If foo turns out to be a hash reference and you want element bar, ...
    (comp.lang.perl.modules)
  • Cant build SNMP...
    ... I get the following when trying to install the SNMP-4.2.0 into perl ... MAN3PODS takes a hash reference not a string/number. ... Can't use string as a HASH ref while "strict refs" in use at ...
    (comp.lang.perl.misc)
  • Re: XML::Simple Problem
    ... I am somewhat of a newbie at perl so I do nto know how to check if the ... convert the hash reference into useable perl variables you can ...
    (comp.lang.perl.modules)