Note that there are some explanatory texts on larger screens.

plurals
  1. POPerl booleans, negation (and how to explain it)?
    text
    copied!<p>I'm new here. After reading through how to ask and format, I hope this will be an OK question. I'm not very skilled in perl, but it is the programming language what I known most.</p> <p>I trying apply Perl to real life but I didn't get an great understanding - especially not from my wife. I tell her that:</p> <blockquote> <p>if she didn't bring to me <strong>3</strong> beers in the evening, that means I got <em>zero (or nothing)</em> beers. </p> </blockquote> <p>As you probably guessed, without much success. :(</p> <p>Now factually. From <a href="http://perldoc.perl.org/perlop.html#Symbolic-Unary-Operators">perlop</a>:</p> <blockquote> <p>Unary "!" performs logical negation, that is, "not".</p> </blockquote> <p>Languages, what have <code>boolean</code> types (what can have only two "values") is OK:</p> <blockquote> <p>if it is not the one value -> <em>must be</em> the another one.</p> </blockquote> <p>so naturally:</p> <pre><code>!true -&gt; false !false -&gt; true </code></pre> <p>But perl doesn't have <code>boolean</code> variables - have only a <code>truth system</code>, whrere everything is not <code>0</code>, <code>'0'</code> <code>undef</code>, <code>''</code> is TRUE. Problem comes, when applying <em>logical negation</em> to an <em>not logical value</em> e.g. numbers.</p> <p>E.g. If some number <em>IS NOT 3</em>, thats mean it <em>IS ZERO</em> or empty, instead of the <em>real life</em> meaning, where if something is NOT 3, mean it can be <em>anything but 3</em> (e.g. zero too).</p> <p>So the next code:</p> <pre><code>use 5.014; use Strictures; my $not_3beers = !3; say defined($not_3beers) ? "defined, value&gt;$not_3beers&lt;" : "undefined"; say $not_3beers ? "TRUE" : "FALSE"; my $not_4beers = !4; printf qq{What is not 3 nor 4 mean: They're same value: %d!\n}, $not_3beers if( $not_3beers == $not_4beers ); say qq(What is not 3 nor 4 mean: @{[ $not_3beers ? "some bears" : "no bears" ]}!) if( $not_3beers eq $not_4beers ); say ' $not_3beers&gt;', $not_3beers, "&lt;"; say '-$not_3beers&gt;', -$not_3beers, "&lt;"; say '+$not_3beers&gt;', -$not_3beers, "&lt;"; </code></pre> <p>prints:</p> <pre><code>defined, value&gt;&lt; FALSE What is not 3 nor 4 mean: They're same value: 0! What is not 3 nor 4 mean: no bears! $not_3beers&gt;&lt; -$not_3beers&gt;0&lt; +$not_3beers&gt;0&lt; </code></pre> <p>Moreover:</p> <pre><code>perl -E 'say !!4' </code></pre> <p>what is <em>not not 4</em> IS <strong>1</strong>, instead of 4!</p> <p>The above statements with wife are "false" (mean 0) :), but <strong>really trying teach my son</strong> Perl and he, after a while, asked my wife: <em>why, if something is not 3 mean it is 0 ?</em> .</p> <p>So the questions are:</p> <ul> <li>how to explain this to my son</li> <li>why perl has this design, so why <code>!0</code> is everytime 1 </li> <li>Is here something "behind" what requires than <code>!0</code> is not any <em>random number</em>, but 0.</li> <li>as I already said, I don't know well other languages - in every language is <code>!3 == 0</code>?</li> </ul>
 

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