Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
It seems to me that if it were pursued, you'd want a general conditional assignment operator. I chose ?= for notation because I don't think it conflicts with any existing syntax, and it has some intuitive appeal to me, because of the resemblance to, e.g., ||=. Follow it with a comparison operator and the value to compare. If the comparison is true, do the assignment:
# syntax lvalue ?= op value; # ex: $lowest = $_ if $lowest > $_; $lowest ?= > $_; # ex: $highest = substr($_, 10, 5) if $highest lt substr($_, 10, 5) $highest ?= lt substr($_, 10, 5); # ex: $foo ||= $bar, using the generalized ?= operator $foo ?= || $bar;
The last isn't quite equivalent to $foo ||= $bar, for cases where $foo and $bar are different values of false. ||= would do the assignment; ?= would not.

Unary tests could work, if Perl understands that unary operators operate on the lvalue, and I think in that case that ! should be combinable with other ops, since we already have $a ||= $b to denote $a = $b if !$a:

# ex: $foo = $default if !defined($foo) $foo ?= !defined $default; # ex: $foo{'bar'} = $default if exists($foo{'bar'}) # That is, we're resetting the value, but not creating it $foo{'bar'} ?= exists $default;
That's not going to win any converts from //= notation, and there's not much clamor for exists-based-defaulting, but it did seem a logical extension.

Update: extending a little further, we could make unary operators into mutators by making the value optional. If no value is given, the result of the (unary) operation is assigned back to the lvalue:

# $foo = uc($foo) $foo ?= uc; # even do arrays! @arr ?= reverse;
Operators that can be unary or binary present a syntactic problem. What is meant by
# $foo = 5 if -$foo? # $foo = $foo - 5 if $foo - 5? $foo ?= - 5;
? I think assuming binary operators in those cases make sense, while in the unambiguous case of no argument, you could negate a value with minimal typing:
$foo{$bar}{$baz} ?= -;

The PerlMonk tr/// Advocate

In reply to Re: Thoughts on some new operators for perl (6 or 7) by Roy Johnson
in thread Thoughts on some new operators for perl (6 or 7) by BigLug

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-03-28 14:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found