in reply to Re: RFC: "assignary" operator ?= :
in thread RFC: "assignary" operator ?= :
I can hardly imagine a reason to map non-Boolean values to a previously Boolean $var without confusing any maintainer.
Using a new variable is always better then.
Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery
FootballPerl is like chess, only without the dice
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: RFC: "assignary" operator ?= :
by shmem (Chancellor) on Dec 08, 2019 at 17:37 UTC | |
What's the use case which justifies extending Perl's codebase for such an exotic feature? Concise expressiveness. The ternary ? : is a shortcut for a more convoluted simple if/else
to be written as
which is one line compared to 5, 6 or 7, depending on indentation style.
We have more of such subtle operators, eg. ||= vs. //=, where the first checks the LHS truthiness, and the second checks the LHS definedness. Writing
just looks and feels as silly as
instead of
For the same reasons, I like the compound operator x!! so much, because it lets me set up a parameter list based on truthiness of variables:
Otherwise, I'd had to say:
While the second variant is one line less, I regard the first variant as much more readable, and I don't have to introduce a temporary hash just for the sake of building function arguments. Note that in the second variant, the hash name is misspelt as materiasl at the 'nails' case, small bug caught by strict, but annoying. So, instead of
I'd rather like to see
because it binds the condition to the LHS in the same way as ||= and //= do. I can hardly imagine a reason to map non-Boolean values to a previously Boolean $var without confusing any maintainer. Who said that $var was boolean? It could hold any value, and this value is checked for truthiness. Anyways - de gustibus non est disputandem.
perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
| [reply] [d/l] [select] |
by choroba (Cardinal) on Dec 08, 2019 at 20:49 UTC | |
It's a bit different than the if/else or ternary it's used to replace, though. Unlike them, the x!! evaluates the left hand side even if the condition is zero. Cf. versus
Now %h is the same, but $t is different.
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
| [reply] [d/l] [select] |
by shmem (Chancellor) on Dec 08, 2019 at 21:03 UTC | |
It's a bit different than the if/else or ternary it's used to replace, though Ah no, this construct doesn't replace any ternary, it is a wholly different thing - but an example for replacement of a convoluted construct with a succinct idiom. Once you grok what x!! is about, you'll never forget what it does.
perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
| [reply] [d/l] |
by LanX (Saint) on Dec 08, 2019 at 18:42 UTC | |
Edit: When and why does this ever happen??? > $var = $var ? 'this' : 'that';
Cheers Rolf
| [reply] [d/l] |
by shmem (Chancellor) on Dec 08, 2019 at 20:31 UTC | |
When and why does this ever happen??? As for why, I don't know. But it does. On this old debian box:
So, it looks like there are plenty of use cases ;)
perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
| [reply] [d/l] |
by dave_the_m (Monsignor) on Dec 09, 2019 at 22:32 UTC | |
by LanX (Saint) on Dec 09, 2019 at 23:49 UTC | |
by shmem (Chancellor) on Dec 10, 2019 at 11:19 UTC | |
| |
by Anonymous Monk on Dec 09, 2019 at 01:53 UTC | |
by tobyink (Canon) on Dec 09, 2019 at 13:49 UTC | |