fionbarr has asked for the wisdom of the Perl Monks concerning the following question:

I use this format
for my $line (@$work_ref) {
to iterate through a reference to an array but I see
@{$workref}
Is there a practical reason for the 2nd example?

Replies are listed 'Best First'.
Re: array ref notation
by BrowserUk (Patriarch) on Dec 22, 2014 at 21:26 UTC
    Is there a practical reason for the 2nd example?

    Orthogonality. Without the curly braces @$a[1] is unintuatively equivalent to @{$a}[1]:

    $a = [ [1],[2],[3] ];; print @$a[1];; ARRAY(0x1b7ae0) print @{$a}[1];; ARRAY(0x1b7ae0)

    Instead of:

    print @{ $a[1] };; 2

    You can get away with not using the curlies for a single depth dereference, but it is required for greater depths.

    And this is an error:

    print @$a->[1];; Using an array as a reference is deprecated at (eval 15) line 1, <STDI +N> line 7. ARRAY(0x1b7ae0)

    That needs to be this:

    print @{ $a->[1] };; 2

    I find it easier to always use the curlies rather than have to think about the exceptions.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: array ref notation
by LanX (Saint) on Dec 22, 2014 at 21:23 UTC
    Some people prefer extra parentheses for clarity or easier extensibility.

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

Re: array ref notation
by Anonymous Monk on Dec 22, 2014 at 21:47 UTC

    Another way to think about it is that @$foo is the shortened version of @{$foo}; the latter is needed if the expression gets more complex so you can tell Perl what the order of dereferencing is supposed to be. See Using References