Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Perl doesn't have a native boolean type, but you can use comparison of integers or strings in order to get the same behavior. Alan's example is a nice way of doing that using comparison of integers. Here's an example</p> <pre><code>my $boolean = 0; if ( $boolean ) { print "$boolean evaluates to true\n"; } else { print "$boolean evaluates to false\n"; } </code></pre> <p>One thing that I've done in some of my programs is added the same behavior using a constant:</p> <pre><code>#!/usr/bin/perl use strict; use warnings; use constant false =&gt; 0; use constant true =&gt; 1; my $val1 = true; my $val2 = false; print $val1, " &amp;&amp; ", $val2; if ( $val1 &amp;&amp; $val2 ) { print " evaluates to true.\n"; } else { print " evaluates to false.\n"; } print $val1, " || ", $val2; if ( $val1 || $val2 ) { print " evaluates to true.\n"; } else { print " evaluates to false.\n"; } </code></pre> <p>The lines marked in "use constant" define a constant named true that always evaluates to 1, and a constant named false that always evaluates by 0. Because of the way that constants are defined in Perl, the following lines of code fails as well:</p> <pre><code>true = 0; true = false; </code></pre> <p>The error message should say something like "Can't modify constant in scalar assignment."</p> <p>I saw that in one of the comments you asked about comparing strings. You should know that because Perl combines strings and numeric types in scalar variables, you have different syntax for comparing strings and numbers:</p> <pre><code>my $var1 = "5.0"; my $var2 = "5"; print "using operator eq\n"; if ( $var1 eq $var2 ) { print "$var1 and $var2 are equal!\n"; } else { print "$var1 and $var2 are not equal!\n"; } print "using operator ==\n"; if ( $var1 == $var2 ) { print "$var1 and $var2 are equal!\n"; } else { print "$var1 and $var2 are not equal!\n"; } </code></pre> <p>The difference between these operators is a very common source of confusion in Perl.</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. 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