Perl has several of what i tend to think of as 'convenience operators'.

Perhaps the best example of this is  $var ||= 100; which is shorthand for $var =  100 unless $var; or even if( not $var ) { $var = 100; }

In Perl 6, this has been further improved to //= which (I think) distinguishes between undef and 0 or ''.

The are also all the op= shorthands += , ^=, .= etc.

We also have the ternary operator ? : ; which is often used in expressions like

my $smallest = 1e308; $smallest = $smallest < $_ ? $smallest : $_ for @array;

which is great, but suffers from requiring both terms to be evaluated twice, which besides going against Laziness, also means that if one of the terms is a function, then the function is called twice. And if the value of the function can change between calls: eg.

# Not a good example, but demonstrates the point $latest = $latest > time() ? $latest : time();

probably isn't quite right. Or if the function has side effects, then you are forced to use a temp variable

# This would be the same as a straight assignment, but '!=' is just a +placeholder for a.n. other comparison operation. my $temp = function_with_side_effects(); $state = $state != $temp ? $temp : $state;

I wonder if there would be any utility in having a short-cut operator for the general cases where the ternary is a conditional assignment with only two terms involved?

my $smallest = 1e308; $smallest ?<:= $_ for @array; $latest ?>:= time;

These conditional assignment operators would read as

"If the current value of the lvalue is <op>* than the rvalue, then assign the rvalue to the lvalue"

* <op> is one of these infix binary comparison operators:  <, <=, >, >=, lt, le, gt, ge.

Eg.

$lvalue ?<:= $rvalue; $lvalue ?>:= $rvalue; $lvalue ?<=:= $rvalue; $lvalue ?>=:= $rvalue; $lvalue ?lt:= $rvalue; $lvalue ?gt:= $rvalue; $lvalue ?ge:= $rvalue; $lvalue ?le:= $rvalue;

There are likely some reasons why my suggested syntax wouldn't be feasable to implement in the the perl 5 parser, but maybe in perl 6?

Does this idea have any merit? Would anyone but me use it?

Finally, is it possible to invent new operators using overload? Or would you have to resort to source filters to implement this outside of the core?


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

In reply to A set of new operators. In keeping with the design of Perl? by BrowserUk

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.