David Christensen wrote:
September 10, 2013 06:15 Hans Ginzel wrote:
 > Is there a shorter way to write $a = ! $a, please?
 > Something analogous to ++ and -- operators like $a !! or !! $a would
 > negate the variable $a and return its previous or new value
 > respectively.

I don't believe Perl has boolean pre-invert or post-invert unary
operators. You might be able to build something with objects and
operator overloading, but that would mean overloading existing
operators. Adding entirely new operators could require hacking the Perl
source code (?).


On 09/10/13 04:14, Dr.Ruud wrote:
$a^=1

The bitwise xor-equals binary operator is an interesting suggestion.
Yes, it inverts the boolean sense of variables containing canonical
boolean values (undef, empty string, numerical zero, and numerical one).
But, thinking in boolean and applying this operator to other kinds of
values may be confusing (see script and output, below).


Assuming canonical boolean values, post-invert semantics (save the new
value into another variable) can be can be obtained with assignment:

$new = ($var ^= 1)

It appears that xor-equals has higher precedence than assignment, so the
parentheses are not required:

$new = $var ^= 1

xor-equals IS assignment and has the same precedence as assignment:

perldoc perlop
[SNIP]
   Assignment Operators
       "=" is the ordinary assignment operator.

       Assignment operators work as in C.  That is,

           $a += 2;

       is equivalent to

           $a = $a + 2;

       although without duplicating any side effects that dereferencing
       the lvalue might trigger, such as from tie().  Other assignment
       operators work similarly.  The following are recognized:

           **=    +=    *=    &=    <<=    &&=
                  -=    /=    |=    >>=    ||=
                  .=    %=    ^=           //=
                        x=

       Although these are grouped by family, they all have the
       precedence of assignment.




John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to