Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: RFC: "assignary" operator ?= :

by kcott (Archbishop)
on Dec 09, 2019 at 05:53 UTC ( [id://11109867]=note: print w/replies, xml ) Need Help??


in reply to RFC: "assignary" operator ?= :

G'day richard.sharpe,

$var ?= "something" : "something else";

I suspect something along these lines would do what you want using existing syntax:

$var = ("something else", "something")[!!$var];

Some examples:

$ perl -E 'my $x; $x = (0,1)[!!$x]; say $x' 0 $ perl -E 'my $x = ""; $x = (0,1)[!!$x]; say $x' 0 $ perl -E 'my $x = "X"; $x = (0,1)[!!$x]; say $x' 1

From "Re^2: RFC: "assignary" operator ?= :":

$var1 ?= die "ERROR: \$var1 already set." : $var2;

You could do something rather exotic like:

$ perl -E 'my $x = ""; $x = (42, $x && die "Set: $x")[!!$x]; say $x' 42 $ perl -E 'my $x = "X"; $x = (42, $x && die "Set: $x")[!!$x]; say $x' Set: X at -e line 1.

But ask yourself:

  • Do you fully understand what's happening with that code?
  • Will the next maintainer fully understand what's happening with that code?
  • Wouldn't a simple if/else block be easier to write, read, understand and maintain?

— Ken

Replies are listed 'Best First'.
Re^2: RFC: "assignary" operator ?= :
by Anonymous Monk on Dec 09, 2019 at 07:01 UTC
    $var is repeated thus missing the point

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-25 06:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found