[Purdue-pm] postfix dereference

Mark Senn mark at ecn.purdue.edu
Fri Jul 18 22:17:08 PDT 2014


> did purdue-pm discuss this new feature of perl 5.20 already?  See
> http://perltricks.com/article/68/2014/2/13/Cool-new-Perl-feature--postfix-dereferencing

Here is some code from the article:

    use experimental 'postderef';

    my $array_ref = [1, 2, 3];
    # Using circumflex dereferencing.
    push @{$array_ref}, 4;
    # Using postfix dereferencing.
    push $array_ref->@*, 4;

    my $deep_array_ref = [[[[[1,2,3]]]]];
    # Using circumflex dereferencing.
    push @{$deep_array_ref->[0]->[0]->[0]->[0]}, 4;
    # Using postfix dereferencing.
    push $deep_array_ref->[0]->[0]->[0]->[0]->@*, 4;

I like postfix dereferencing much better---it is much easier to read
and uses only one more character than circumflex dereferencing.

I won't use it until it is no longer experimental though, and then
only for new code.

I like the noun followed by transformations style of
    $deep_array_ref->[0]->[0]->[0]->[0]->@*
that is easy to read left to right.  It's nice not to have to worry
about the scope of @{ ... } when using circumflex dereferencing.
Braces, brakets, and parentheses act like fences to my eyes---it's nice
to not have to come to one, hesitate, and then jump over it.

Here is some Perl 6 code.
    my $str = 'TESTING 1, 2, 3';
    $str.subst(/<-[A..M]>/,'',:g).lc.say;
Similar to postfix dereferencing, the last line of this code
is easy to read left to right:
    operate on $str
    # I wanted to demonstrate Perl 6's new subst method.
    substitute character than is not A--M with nothing---do that globally
    make the result lowercase
    and print it on standard output followed by a newline character

-mark
 


    



More information about the Purdue-pm mailing list