LanX has asked for the wisdom of the Perl Monks concerning the following question:

perlop says In scalar context, ".." returns a boolean value.

(it's the flip-flop not the range operator!)

IIRC (returned) boolean values in perl are either 1 or undef.

so why does this

perl -e 'print scalar (a..d),"\n"' 1E0

return a float?

Here the op-codes

perl -MO=Terse -e 'print scalar (a..d)' LISTOP (0x8982558) leave [1] OP (0x8982390) enter COP (0x89828c0) nextstate LISTOP (0x89824d0) print OP (0x897a7f8) pushmark UNOP (0x8982608) scalar UNOP (0x89825d0) null UNOP (0x8982668) flop UNOP (0x8982688) flip [2] LOGOP (0x89826f0) range [1] SVOP (0x89828a0) const [4] PV (0x8960920) +"a" SVOP (0x89827e0) const [3] PV (0x897ee88) +"d" -e syntax OK

Cheers Rolf

Replies are listed 'Best First'.
Re: Booleans from flip-flops
by ikegami (Patriarch) on Jan 20, 2010 at 18:20 UTC

    There are two boolean values: true and false. Perl doesn't have a single way of representing either. 1E0 is true.

    Many ops return dualvar(1,'1') for true and dualvar(0,'') for false, but the flip-flop goes beyond that to increase usefulness.

    While it is a number, 1E0 is stored as a string (not a float) to make it distinguishable from 1.

    $ perl -MScalar::Util=looks_like_number \ -le'print looks_like_number("1E0")?1:0' 1 $ perl -le'print for 1E0, "1E0"' 1 1E0

    (By the way, looks_like_number used 4 for true in my test.)

    The reason for returning ...E0 is documented.

    Update: Added code example

Re: Booleans from flip-flops
by JavaFan (Canon) on Jan 20, 2010 at 18:06 UTC
    IIRC (returned) boolean values in perl are either 1 or undef
    Perl doesn't have "booleans". Function may return true or false values - it's not always defined which true or false value they return. The empty string is also often returned as a false value, and sometimes "0 but true" is returned. Or "0E0".

    The manual page says:

    The right operand is not evaluated while the operator is in the "false" state, and the left operand is not evaluated while the operator is in the "true" state. The precedence is a little lower than || and &&. The value returned is either the empty string for false, or a sequence number (beginning with 1) for true. The sequence number is reset for each range encountered. The final sequence number in a range has the string "E0" appended to it, which doesn’t affect its numeric value, but gives you something to search for if you want to exclude the endpoint. You can exclude the beginning point by waiting for the sequence number to be greater than 1.
    Which explains it.
      Right it's normally empty not undef ...

      Thanks, I've mysteriously overlooked the second paragraph in perlop only seeking for 1E0...

      Anyway the wording "it returns a boolean value" is somehow misleading ...

      Cheers Rolf

        It's not an empty string, but a dualvar that has zero is the numeric slot and an empty string in the string slot.
        $ perl -wle'print ""' $ perl -wle'print 0+""' Argument "" isn't numeric in addition (+) at -e line 1. 0 $ perl -wle'print !1' $ perl -wle'print 0+!1' 0

        Anyway the wording "it returns a boolean value" is somehow misleading ...

        It does return something that's a boolean value (true when there's a match, false when there's no match). It also returns ...