Front page | perl.perl5.porters |
Postings from August 2013
Re: postfix dereference syntax
Thread Previous
|
Thread Next
From:
Darin McBride
Date:
August 15, 2013 16:13
Subject:
Re: postfix dereference syntax
Message ID:
2105675.Rnf9yx2FJo@naboo
On Wednesday August 14 2013 4:44:31 PM Brad Baxter wrote:
> FWIW, I would never use (this incarnation of) postfix dereference. I'm
> sorry, but to my eye $foo->@* is not prettier than @{$foo}. And I
> understand the latter.
>
> That is to say, with the latter, I know exactly where the entity being
> dereferenced begins and ends. With the former--phew--you got me.
I think this says more about your comfort level than the usefulness, or lack
thereof, of this functionality. I know a lot of people who would never use
perl 5 syntax because it's no prettier to them than perl 4 syntax, and they
understand the latter.
To me, the proposed new syntax will take a bit of learning, but will be hugely
beneficial and make my code both easier to write as well as clearer in meaning
as the flow will be more natural.
On Thursday August 15 2013 9:44:26 AM Johan Vromans wrote:
> David Nicol <davidnicol@gmail.com> writes:
> > it's not about $foo->@*
> > it's about $foo->{bar}->baz(27, flurg => gnar_now())->[4]->@*
> > at the end of a long chain of stuff eventually yielding a reference,
> > sometimes you want to get the whole array, or a slice of it, and currently
> > you need to back up and enclose the whole thing.
>
> Yes, that is quite a burden, assuming you still use butterflies :)
>
> Seriously, this is a strong indication of strictly left-to-right code
> writing, not knowing beforehand what you want to do with that big
> expression. Makes me frown.
I know what I want to do with that big expression. However, I have concocted
it in order of what is going to happen.
I'm taking the %$foo hash. Looking up the key bar. I expect that to be an
object. I'll call its baz method. It will return an array ref, which I will
take the fifth element of, and THAT will be an array ref, which I will
completely dereference.
There's nothing unique about this new syntax other than the syntax itself.
What it's doing is exactly what we did with everything else - take one value
and extract something from it. From left to right. The @{...} syntax forces
me to mentally go back to the beginning (or mentally note what's at the
beginning) just to figure out what's going on.
Generally, I've avoided that syntax. I find it too unwieldy. I end up using
more temporary variables just to avoid the braces.
> > What we've just set up is the filling of the holes left in the current
> > incomplete postfix dereference syntax, which allows only scalar element
> > accesses.
>
> Unbiased opinion: I do not doubt it fits in somewhere, my question was
> whether it is worth the efforts given that it seems to add to
> implementation and maintenance complexity, and potential user confusion.
That's a fair question. I think it's worth a lot, but I'm not the one eating
the complexity, I'm consuming its sugar.
> Personal opinion: Since we have $aref->[1], $aref->[] makes sense to
> mean the whole array. Likewise, $href->{foo}, $href->{}. But all other
> examples I've seen of postfix dereference syntax seem rather contrived
> to me. Why would you ever write $$foo->$* instead of $$$foo or ${$$foo}?
Again, when you simplify the LHS to a single variable, none of this makes any
difference. It's only when the LHS is something complex that this makes
sense. And it makes even more sense when we have conditional dereferencing:
$foo->?{bar}->?baz(27, flurg => gnar_now())->?[4]->?@*
If $foo is not a hash ref, return undef. Otherwise grab the value associated
with its bar key. If that's not an object, return undef. Otherwise call its
baz method. If that doesn't return an array ref, return undef. Otherwise
grab its fifth element. If that's not an array ref, return undef. Otherwise
dereference it.
Compare to:
@{$foo->?{bar}->?baz(27, flurg => gnar_now())->?[4]}
If anything inside the braces fail, it returns undef, and gets derefenced.
Instant death. Maybe there'd have to be:
@?{$foo->?{bar}->?baz(27, flurg => gnar_now())->?[4]}
just to get the same end.
> How often would you need $foo->\*? blah->&*?
Personally, I almost never use the prefix form of these, so I'm not sure I'd
use the postfix form, either. However, again, when you have a hash of objects
whose baz methods return array refs of code refs, then having the ->&* bit at
the end may come in handy.
Thread Previous
|
Thread Next