Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: ||= (poorly documented?)

by Marshall (Prior)
on Jul 09, 2012 at 07:36 UTC ( #980666=note: print w/ replies, xml ) Need Help??


in reply to ||= (poorly documented?)

Maybe this will help...

#!/usr/bin/perl -w use strict; ## The ||= operator tests for "truthfulness" ## The //= operator tests for "definedness" my $z = 25; my $x = 550; $z ||= $x; print "$z\n"; #prints 25 because z is already true my $y; # $y is undefined print "y is not defined\n" if !defined $y; $y //= 32; print "y is is defined now as $y\n" if defined $y; my $k; #$k is undefined #An undefined value evaluates to "false" #and the assignment proceeds $k ||= 3842; print "k is $k ...hey I'm defined now!\n"; __END__ 25 y is not defined y is is defined now as 32 k is 3842 ...hey I'm defined now!
Update:
There is a rather strange thing that can happen in Perl.
It is possible for Perl to return a "true", "zero" value.
It does this by returning the string "0E0": 0 * 10**1 =0 numerically, but that evaluates to "true" in a logical sense. This is used in the Database Interface for example. You might get back an answer "hey I worked, but I didn't modify any rows!". In an language like C or Java, you have to have two variables: Number of rows and did it work or not? In Perl this can be expressed in a single value. "I worked, but didn't modify any rows!". A "0" is logical False while a 0E0 is logical True- pretty cool!

Update:
I got off track, but since I'm talking about 0E0, I will show some code for your amusement.

#!/usr/bin/perl -w #use strict; $|=1; #Turn off STDOUT Buffering # This is wild but Perl has # a special string that will evaluate # to a "true", "zero" value and can be used # in a numeric computation - even with # warnings! # This is so obscure that it must be # depreciated in favor of the 0E0 notation. # I personally wouldn't use this, and I # show it just for amusement. This just # Perl trivia. my $str_zero = "0 but true"; ### special string ### $str_zero += 1; print "new str_zero is: $str_zero\n"; # No warning, this is the same as 0E0 my $bogus_zero = "0 bogus"; $bogus_zero +=1; print "bogus_zero plus one is: $bogus_zero\n"; # "works" albeit with a warning __END__ new str_zero is: 1 Argument "0 bogus" isn't numeric in addition (+) at C:\TEMP\zeroButTrue.pl line 23. bogus_zero plus one is: 1


Comment on Re: ||= (poorly documented?)
Select or Download Code
Re^2: ||= (poorly documented?)
by dsheroh (Parson) on Jul 09, 2012 at 09:23 UTC
    There is a rather strange thing that can happen in Perl.
    It is possible for Perl to return a "true", "zero" value.
    It does this by returning the string "0E0": 0 * 10**1 =0 numerically, but that evaluates to "true" in a logical sense.
    This works because there are only two strings which evaluate as false in Perl: "" and "0".

    Any other string which begins with one or more zeroes followed by a non-digit character will by logically true (since it's not one of the two false strings) while still having the value 0 when evaluated as a number. This actually will also work with strings that start with a non-numeric character (true as a boolean, 0 as a number), but, if warnings are enabled (as they generally should be), it will complain that "Argument "..." isn't numeric" if you try to use it as a number.

    Aside from "0E0", the other string I often see used for this purpose (I'm not sure which is more common) is "0 but true".

      I don't see how you and I are in any kind of disagreement.
        We're not. You seemed surprised that this works, so I provided an explanation for why it works.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://980666]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2014-12-25 20:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (163 votes), past polls