title: Compare two (potentially) undef variables in perl
body: <p>I am trying to compare two variables in which are usually strings. These variables are generated from a database, <code>$var1</code> from one db and <code>$var2</code> from another.
  When I compare them in a loop I use the <code>ne</code> operator. However there are times when I these variables are <code>null</code> or <code>undef</code>. The comparison is done as follows:</p>
  
  <pre><code>foreach my $var1 (@$varlist)
  {
    if ($var1 ne $var2)
    {
      print "vars are not equal";
    }
  }
  </code></pre>
  
  <p>The issue is that if <code>$var1</code> or <code>$var2</code> are <code>undef</code> then I get an error. However, I need to be able to compare then values as undef b/c I will have to write them. I considered converting the variables to a string 'NULL' and then back but that seemed inefficient.</p>
  
  <p>Any way to fix this?
  Thanks!</p>