Jacob Chapa wrote:

> what is the difference between
>
> ->

That is the Perl dereferencing operator.  You use it to access hash or array
elements, or package [class] methods, from a reference.

my $keywords_ref = {};
$keywords_ref->{Animal}   = 'Movement';
$keywords_ref->{Vegetable} = 'Photosynthesis';
$keywords_ref->{Mineral} = 'Chrystallization';

> and
>
> =>

... is the key-value pairing operator.  It is used in static initiaors for Perl
hashes, and can be very helpful in aligning keys and values to make group
assgnments visually clear:


my $keywords_ref = {
      Animal           => 'Movement',
      Vegetable        => 'Photosynthesis',
      Mineral          => 'Chrystallization',
      'Sentient being' => 'Don\'t look at me.  I\'m just a human'
};

Which offers a reference to an anonymous hash initiated with the pairs
indicated.

Hashes declared directly can also be populated using this operator:

my %keywords= (
      Animal           => 'Movement',
      Vegetable        => 'Photosynthesis',
      Mineral          => 'Chrystallization',
      'Sentient being' => 'Don\'t look at me.  I\'m just a human'
);

Note that parenthese are used in this context

and the arrow operator can be used with references to hashes declared either way

my $keywords_ref = \%keywords;
$keywords_ref->{'Protistan'} = 'One-celled';

which can be very useful when using the hash or other struture in a function.
In fact it is necessary if you wish to midify the values of the original
structure from within the function:

print_key_words($keywords_ref );
print "$_: $keywords{$_}\n" foreach keys %keywords;

sub print_key_words {
   my $keywords_ref = shift;

   print "$_: $keywords_ref->{$_}\n" foreach keys %$keywords_ref;
   $keywords_ref->{$_} .= '--has been printed once' foreach keys %$keywords_ref;

}

If you put that all together, you can get an idea of how each of these operators
does its work:

Greetings! C:\Documents and Settings\rjnewton>perl -w
my %keywords = (
      Animal           => 'Movement',
      Vegetable        => 'Photosynthesis',
      Mineral          => 'Chrystallization',
      'Sentient being' => 'Don\'t look at me.  I\'m just a human'
);

my $keywords_ref = \%keywords;
$keywords_ref->{'Protistan'} = 'One-celled';

print_key_words($keywords_ref );
print "$_: $keywords{$_}\n" foreach keys %keywords;

sub print_key_words {
   my $keywords_ref = shift;

   print "$_: $keywords_ref->{$_}\n" foreach keys %$keywords_ref;
   $keywords_ref->{$_} .= '--has been printed once' foreach keys %$keywords_ref;

}

Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to