Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: I use postfix dereferencing ...

by haj (Chaplain)
on Feb 18, 2019 at 07:43 UTC ( #1230062=note: print w/replies, xml ) Need Help??

in reply to I use postfix dereferencing ...

When the poll was published I truthfully answered with "Never". However, since then I find myself checking whenever I write @{$stuff} whether it would be more readable as a postfix dereference. Eily already brought up one border case in Re^2: I use postfix dereferencing ..., but I'd agree with him that in this case an intermediate variable would be preferable.

Today I stumbled over another use case where I am now tempted to use postfix dereferencing: I'm pulling several attributes out of an object, some of which are array references... and I need copies of the arrays. In that case, scalars and arrays "line up" better with postfix dereferencing. SSCCE:

use 5.020; use feature 'postderef'; package Object { use Moose; has string => (is => 'ro', isa => 'Str'); has aref => (is => 'ro', isa => 'ArrayRef'); }; my $object = Object->new(string => 'Hello World', aref => [1,2,3] ); # Traditional dereferencing my $string = $object->string; my @list1 = @{$object->aref}; # Intermediate variable my $string = $object->string; my $aref = $object->aref; my @list2 = @$aref; # Postfix dereferencing my $string = $object->string; my @list3 = $object->aref->@*;

What needs work in my opinion is the documentation in perlref. It claims that (emphasis mine) Postfix dereference should work in all circumstances where block (circumfix) dereference worked, and should be entirely equivalent. "Should work"? Except when it doesn't? I'd prefer not to rely on syntax which should work. A bit later it says: Note especially that $cref->&* is not equivalent to $cref->(), and can serve different purposes. I admit that I'm too lazy to work out the difference and have decided not to use the ->&* construct instead.

Replies are listed 'Best First'.
Re^2: I use postfix dereferencing ...
by Eily (Monsignor) on Feb 21, 2019 at 10:27 UTC

    About that last point, $sub->&* would rather be equivalent to &$sub, meaning it passes the current value @_ to the subroutine (the comma after &$sub, is not a typo, it's to mark the fact that you can't pass parameters with the ->&* form):

    perl -MData::Dump=pp -dEbug Loading DB routines from version 1.51 Editor support available. Enter h or 'h h' for help, or 'perldoc perldebug' for more help. main::(-e:1): bug DB<1> $a = sub { say pp @_; say 'wantarray' if wantarray } DB<2> @_ = qw< Hello World > DB<3> $a->() () wantarray DB<4> $b = $a->(); () DB<5> $b = &$a,; ("Hello", "World") DB<6> $b = $a->&*; ("Hello", "World")

Re^2: I use postfix dereferencing ...
by talexb (Canon) on Feb 22, 2019 at 17:21 UTC

    The whole postfix approach was cute when it was c = *p++ in C. Get the value pointed to by the pointer, then increment the pointer. Sure.

    Turning an arrayref into an array (@{ $array_ref }, or @$array_ref), as has already been mentioned, is fine. Going to $array_rec->@* instead seems to obscure what's going on .. like you're giving directions, and saying, "Up ahead, turn right .. oh, but first, turn left.".

    That's fine, now I'm aware, and can answer if it comes up in a job interview. And I'll avoid using it like the plague.

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1230062]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2019-10-18 00:12 GMT
Find Nodes?
    Voting Booth?