Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The various logical operators don't return an empty string, they return a false or true value in all three simple scalar types. It just looks like it returns an empty string because <code>print</code> forces a string context on its arguments:</p> <pre><code>#!/usr/bin/perl use strict; use warnings; use Devel::Peek; my $t = 5 &gt; 4; my $f = 5 &lt; 4; Dump $t; Dump $f; </code></pre> <p>Output:</p> <pre><code>SV = PVNV(0x100802c20) at 0x100827348 REFCNT = 1 FLAGS = (PADMY,IOK,NOK,POK,pIOK,pNOK,pPOK) IV = 1 NV = 1 PV = 0x100201e60 "1"\0 CUR = 1 LEN = 16 SV = PVNV(0x100802c40) at 0x100827360 REFCNT = 1 FLAGS = (PADMY,IOK,NOK,POK,pIOK,pNOK,pPOK) IV = 0 NV = 0 PV = 0x100208ca0 ""\0 CUR = 0 LEN = 16 </code></pre> <p>For those not familiar with the Perl 5 internals, a <code>PVNV</code> is a scalar structure that holds all three simple scalar types (integer <code>IV</code>, double precision float <code>NV</code>, and string <code>PV</code>). The flags <code>IOK</code>, <code>NOK</code>, and <code>POK</code> mean that the integer, double, and string values are all in sync (for some definition of in sync) so any one of them may be used (i.e. no conversions need to take place if you use it as an integer, double, or string).</p> <p>I assume the empty string was chosen for the false string because it <strike>is smaller and</strike> is more in keeping with the idea of a false string than <code>"0"</code>. Ignore my statement about it being smaller, both <code>""</code> and <code>"1"</code> are the same size: sixteen characters. It says so right in the dump. Perl 5 adds extra space to strings to allow them to grow quickly.</p> <p>Oh, and I hate you. In researching this I have found that I have lied in <a href="http://github.com/cowens/perlopquick" rel="nofollow noreferrer"><code>perlopquick</code></a> and will now have to find a way to fix it. If only you had been like all of the other sheep and just accepted Perl 5's surface weirdness as fact, I would have less work to do.</p> <p>Answers to the questions in the EDIT section:</p> <blockquote> <p>How would using string which is true ("false" for instance) as a string representation of false values change the meaning of existing code?</p> </blockquote> <p>The only special things about about PL_sv_yes and PL_sv_no (the canonically true and false values returned by comparison operators) are that they are read only and are created by <code>perl</code> not the program that is running. If you change them, it does not change the truthiness test, so a PL_sv_no that is set to <code>"false"</code> will be treated as true. You can even do this yourself (this code stops working at some point between Perl 5.18 and the latest Perl) using undocumented features of <code>perl</code>:</p> <pre><code>#!/usr/bin/perl use strict; use warnings; use Scalar::Util qw/dualvar/; BEGIN { # use the undocumented SvREADONLY function from Internals to # modify a reference to PL_sv_no's readonly flag # note the use of &amp; to make the compiler not use SvREADONLY's # prototype, yet another reason prototypes are bad and shouldn't # be used &amp;Internals::SvREADONLY(\!!0, 0); # set PL_sv_no to a dualvar containing 0 and "false" ${\!!0} = dualvar 0, "false"; } if (5 &lt; 4) { print "oops\n"; } </code></pre> <p>outputs</p> <pre><code>opps </code></pre> <p>This is because the truthiness test looks at strings first.</p> <blockquote> <p>Could we say that code that changes semantics after such a change is less robust/correct than it could have been?</p> </blockquote> <p>It will be straight up broken. Even if you restrict yourself to setting it to an int 0 or a string "0" (both of which are false), it will break some valid code.</p> <blockquote> <p>I guess string context is so pervasive in Perl that the only option leading to sane semantics is if boolean value preserve its value after round tripping to and from a string...</p> </blockquote> <p>Yes.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload