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


Pre-invert semantics (save the old value into another variable) can be obtained with multiple assignment, protected within parentheses, so that xor-equals operates on the correct lvalue:

    ($var = $old = $var) ^= 1


Note that the following will make a copy and then invert the copy. leaving the original untouched:

    ($varn = $var) ^= 1


HTH,

David



2013-09-10 14:27:36 dpchrist@desktop ~/sandbox/perl
$ cat xor-equal.pl
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my (   $t,    $f,    $u,    $e,    $z,    $o,    $i,    $n,    $s);
my ($newt, $newf, $newu, $newe, $newz, $newo, $newi, $newn, $news);
my ($oldt, $oldf, $oldu, $olde, $oldz, $oldo, $oldi, $oldn, $olds);

sub dump_new {
    Data::Dumper->Dump(
        [$newt, $newf, $newu, $newe, $newz, $newo, $newi, $newn, $news],
      [qw(newt   newf   newu   newe   newz   newo   newi   newn   news)]);
}

sub dump_old {
    Data::Dumper->Dump(
        [$oldt, $oldf, $oldu, $olde, $oldz, $oldo, $oldi, $oldn, $olds],
      [qw(oldt   oldf   oldu   olde   oldz   oldo   oldi   oldn   olds)]);
}

sub dump_var {
    Data::Dumper->Dump([$t, $f, $u, $e, $z, $o, $i, $n, $s],
                     [qw(t   f   u   e   z   o   i   n   s)]);
}

sub reset_var {
    $t = (1 == 1);                      # true
    $f = (1 != 1);                      # false
    $u = undef;                         # undefined value
    $e = '';                            # empty string
    $z = 0;                             # zero
    $o = 1;                             # one
    $i = 255;                           # integer
    $n = 3.141592653589793238462;       # double
    $s = "Just another Perl hacker";  # string
}

sub save_new {
    $newt = $t ^= 1;
    $newf = $f ^= 1;
    $newu = $u ^= 1;
    $newe = $e ^= 1;
    $newz = $z ^= 1;
    $newo = $o ^= 1;
    $newi = $i ^= 1;
    $newn = $n ^= 1;
    $news = $s ^= 1;
}

sub save_old {
    ($t = $oldt = $t) ^= 1;
    ($f = $oldf = $f) ^= 1;
    ($u = $oldu = $u) ^= 1;
    ($e = $olde = $e) ^= 1;
    ($z = $oldz = $z) ^= 1;
    ($o = $oldo = $o) ^= 1;
    ($i = $oldi = $i) ^= 1;
    ($n = $oldn = $n) ^= 1;
    ($s = $olds = $s) ^= 1;
}

reset_var();
print "### reset values ###\n", dump_var();

save_new();
print "### xor-equals, save new values ###\n", dump_var(), dump_new();

save_new();
print "### again ###\n", dump_var(), dump_new();

reset_var();
print "### reset values ###\n", dump_var();

save_old();
print "### xor-equals, save old values ###\n", dump_var(), dump_old();

save_old();
print "### again ###\n", dump_var(), dump_old();



2013-09-10 14:27:38 dpchrist@desktop ~/sandbox/perl
$ perl xor-equal.pl
### reset values ###
$t = 1;
$f = '';
$u = undef;
$e = '';
$z = 0;
$o = 1;
$i = 255;
$n = '3.14159265358979';
$s = 'Just another Perl hacker';
Argument "" isn't numeric in bitwise xor (^) at xor-equal.pl line 43.
Argument "Just another Perl hacker" isn't numeric in bitwise xor (^) at xor-equal.pl line 48.
### xor-equals, save new values ###
$t = 0;
$f = 1;
$u = 1;
$e = 1;
$z = 1;
$o = 0;
$i = 254;
$n = 2;
$s = 1;
$newt = 0;
$newf = 1;
$newu = 1;
$newe = 1;
$newz = 1;
$newo = 0;
$newi = 254;
$newn = 2;
$news = 1;
### again ###
$t = 1;
$f = 0;
$u = 0;
$e = 0;
$z = 0;
$o = 1;
$i = 255;
$n = 3;
$s = 0;
$newt = 1;
$newf = 0;
$newu = 0;
$newe = 0;
$newz = 0;
$newo = 1;
$newi = 255;
$newn = 3;
$news = 0;
### reset values ###
$t = 1;
$f = '';
$u = undef;
$e = '';
$z = 0;
$o = 1;
$i = 255;
$n = '3.14159265358979';
$s = 'Just another Perl hacker';
Argument "" isn't numeric in bitwise xor (^) at xor-equal.pl line 55.
Argument "Just another Perl hacker" isn't numeric in bitwise xor (^) at xor-equal.pl line 60.
### xor-equals, save old values ###
$t = 0;
$f = 1;
$u = 1;
$e = 1;
$z = 1;
$o = 0;
$i = 254;
$n = 2;
$s = 1;
$oldt = 1;
$oldf = '';
$oldu = undef;
$olde = '';
$oldz = 0;
$oldo = 1;
$oldi = 255;
$oldn = '3.14159265358979';
$olds = 'Just another Perl hacker';
### again ###
$t = 1;
$f = 0;
$u = 0;
$e = 0;
$z = 0;
$o = 1;
$i = 255;
$n = 3;
$s = 0;
$oldt = 0;
$oldf = 1;
$oldu = 1;
$olde = 1;
$oldz = 1;
$oldo = 0;
$oldi = 254;
$oldn = 2;
$olds = 1;

--
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