Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Don't ask to ask, just ask
 
PerlMonks  

Mini-Tutorial: Dereferencing Syntax

by ikegami (Pope)
on Jun 20, 2012 at 16:54 UTC ( #977408=perlmeditation: print w/ replies, xml ) Need Help??

Direct Using References
Syntax 1* Syntax 2
scalar itself $s ${$sr}
array itself @a @{$ar}
element $a[0] ${$ar}[0] $ar->[0]
slice @a[0,1,2] @{$ar}[0,1,2]
last index $#a $#{$ar}
hash itself %h %{$hr}
element $h{'a'} ${$hr}{'a'} $hr->{'a'}
slice @h{'a','b','c'} @{$hr}{'a','b','c'}
code call func(@args) &{$cr}(@args) $cr->(@args)

* – The curly brackets around $sr, $ar, $hr and $cr are optional.

See also:

Comment on Mini-Tutorial: Dereferencing Syntax
Select or Download Code
Re: Mini-Tutorial: Dereferencing Syntax
by tobyink (Prior) on Jun 20, 2012 at 19:50 UTC

    It's also worth noting that the following are two completely different things.

    @hash{'key1', 'key2'} $hash{'key1', 'key2'}

    The first one is basically a shorthand for:

    ($hash{'key1'}, $hash{'key2'})

    The second one is:

    $hash{join($;, 'key1', 'key2')}

    ... where $; is the subscript separator, which defaults to \034.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      And similarly (though not identically, certainly) for arrays.

      The difference between hash lookups and hash slices has nothing to do with references or how to use them. I shall not add such a note.
Re: Mini-Tutorial: Dereferencing Syntax
by Anonymous Monk on Jun 21, 2012 at 13:06 UTC
    Not mentioned:   $$foo as shorthand for $foo->.

      $$foo is not a shorthand for $foo->.

      >perl -E"$foo=\'foo'; say $$foo;" foo >perl -E"$foo=\'foo'; say $foo->;" syntax error at -e line 1, near "->;" Execution of -e aborted due to compilation errors.

      $foo-> is not a shorthand for $$foo.

      >perl -e"$foo=sub{}; $foo->();" >perl -e"$foo=sub{}; $$foo();" syntax error at -e line 1, near "$foo(" Execution of -e aborted due to compilation errors.

      You're probably thinking of the equivalency of $$foo[...] and $foo->[...] and of $$foo{...} and $foo->{...}, but that's already mentioned.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://977408]
Approved by ww
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2013-05-19 18:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best material for plates (tableware) is:









    Results (397 votes), past polls