Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

{ __syntax } question

Status
Not open for further replies.

capitano

Programmer
Jul 30, 2001
88
0
0
US
I'm learing about XML::parser and looking at an example snippet of code in the Wrox book "Professional Perl development".

There is a snippet of code which does a funny syntax thing with curly-brackets and I don't understand it:

sub Init {
my $self = shift;

$self->{__myData} = {
invoice_number => "",
customer_id => "",
address => "",
name => "",
#.... and more code
};
#...and more code
}

Specifically, this is the part I'm not understanding:

$self->{__myData}

??? What is {__myData} ??

Thanks for any help here.

Bryan
 
[tt]$self[/tt] is a reference to a hash (actually $self is a reference to an object in this case, but objects are usually stored as hashes; although they need not be). Just as you would access a hash as
Code:
$hash{'akey'} = 'myvalue';
print "Value of $key is $hash{$key}\n";
You can also define a reference to a hash like
Code:
my $hashref = \%hash;
And you would access the hash value as
[/code]
$hashref->{'akey'} = 'myvalue';
#or equivalently
$$hashref{'akey'} = 'myvalue';
print "Value of $key is $hashref->{$key}\n";
[/code]
Just as you can define hashes like
Code:
%hash = ( key1 => 'value1',
          key2 => 'value2',
        );
You can also define hash refs like
(note the braces instead of parentheses)
Code:
$hashref = { key1 => 'value1',
             key2 => 'value2',
           };
In your case $self is a reference to an object, a hash reference that has been blessed into a class. The class being whatever package the Init() sub is contained in.

It is common in packages and modules put an underscore before a hash key that is internal to the module. Meaning it isn't meant to be accessed outside the package, only through methods contained in the module. You can, of course access them in you code by using the key, it's just not part of the 'published' interface of the module.

See the perlref perldoc for more.

jaa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top