title: Use of defined/undef in Perl
body: <p>I am planning to pass two variables to a perl function, one of which may be optional. I am trying to check if the second one is defined or not, but it doesn't work correctly. When I called the function as myFunction(18), it assumes that the variable $optional is defined and goes to the else statement. But in the else statement when the $optional variable is being accessed it throws an &quot;uninitialized&quot; error. This is exactly opposite of what I have expected. Any help is greatly appreciated.</p>
  <pre><code>sub myFunction {
      my ($length, $optional) = (@_);
  
      if (undef($optional) {
          more code..
      }
      else {
          more code...
      }
  }
  
  myFunction(18);
  </code></pre>