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.
Given a module and a corresponding script…
Module: ./lib/My/Module.pm
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;
}
Script: ./test.pl
#!/usr/bin/perl
use strict;
use warnings;
use My::Module qw(trim);
print $My::Module->trim( " \t hello world\t \t" );
I get back the error message
Can’t call method “trim” on an undefined value at ./text.pl line 7.
Infact, if I call $My::Module->notamethod( "hello world" ); it gives a similar error.
What’s wrong with the above script/module?
What is that error Can't call method “X” on an undefined value at ${SOMEFILE} line ${SOMELINE} really saying?
Does this refer to the context of the method call (passed here to print), or the context of the arguments?
That syntax is looking for an object or classname in the variable
$My::Moduleand calling its trim method, but that variable is undefined.Instead, you want to just say
print My::Module::trim( " \t hello world\t \t" );to call the My::Module::trim() function.From the use line, it looks like you are trying to import trim() into the local package so you can just call it without the
My::Module::qualification, but your module doesn’t look like it is set up to support exporting.In your regexes, the /s and /m flags don’t have any effect – they only change what ., ^, and $ match, and you don’t use any of those.