title: Perl Module Method Calls: Can't call method "X" on an undefined value at ${SOMEFILE} line ${SOMELINE}
body: <p>All over the place, especially in DBI, I see this message come up all the time. It's confusing, because the first thing that comes to mind is that the arguments I'm passing the function are set to undef (or something similar), but it's clearly not the case.</p>
  
  <p>Given a module and a corresponding script...</p>
  
  <p>Module: <code>./lib/My/Module.pm</code></p>
  
  <pre><code>package My::Module;
  
  use strict;
  use warnings;
  
  sub trim {
      my $str = shift;
      $str =~ s{ \A \s+ }{}xms; # remove space from front of string
      $str =~ s{ \s+ \z }{}xms; # remove space from end of string
      return $str;
  }
  </code></pre>
  
  <p>Script: <code>./test.pl</code></p>
  
  <pre><code>#!/usr/bin/perl
  
  use strict;
  use warnings;
  use My::Module qw(trim);
  
  print $My::Module-&gt;trim( " \t hello world\t \t" );
  </code></pre>
  
  <p>I get back the error message</p>
  
  <blockquote>
    <p>Can't call method "trim" on an undefined value at ./text.pl line 7.</p>
  </blockquote>
  
  <p>Infact, if I call <code>$My::Module-&gt;notamethod( "hello world" );</code> it gives a similar error.</p>
  
  <p>What's wrong with the above script/module?</p>
  
  <p>What is that error <code>Can't call method “X” on an undefined value at ${SOMEFILE} line ${SOMELINE}</code> really saying?
  Does this refer to the context of the method call (passed here to print), or the context of the arguments?</p>