On Thu, Jul 15, 2010 at 09:52, Dr.Ruud <[email protected]> wrote:
> Bryan R Harris wrote:
>
>> Out of curiosity, is there a unary not operator in perl?
>>
>>  i.e.  "$a = $a+1"   is the same as   "$a++"
>>
>> Is there a similarly short form of "$a = !$a"?  Like "$a!!"?  (tried it
>> and
>> it didn't work.)
>
> perl -wle '
>  print --$| for 1..4;
> '
> 1
> 0
> 1
> 0
snip

Interesting behavior.  That isn't documented in perlvar.  I assume
what is happening is that $| sets itself to 1 whenever a non-zero
value is assigned to it:

perl -le '$| = 20; print $|'
perl -le '$| = -1; print $|'

The first --$| is $| = $| - 1 or $| = 0 - 1 or $| = -1.  Since -1 is
nonzero $| becomes 1, the second --$| is $| = $| = $| - 1 or $| = 1 -
1 or $| = 0.  Since 0 is zero, $| becomes 0.  You can generated this
same behavior with a tied scalar:

#!/usr/bin/perl

use strict;
use warnings;

#Don't do this, it is silly.
{ package Silly;

        sub TIESCALAR { bless \my $self, shift }
        sub FETCH { ${+shift} }
        sub STORE {
                my ($self, $val) = @_;
                $$self = 0+!!$val;
        }
}

tie my $silly, "Silly";

print --$silly, "\n" for 1 .. 4;



-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/


Reply via email to