Checking if a method is defined in perl 5.00503

A

Aaron Brice

I need to check that a method whose name is given in $methodName
exists. In perl 5.8.0, "if (defined($myobj->$methodName))" seems to
work, but not in 5.00503, so I'm wondering what the 5.00503 equivalent
would be if it exists (upgrading is not an option right now). The
following test code works in 5.8.0 but gives me a syntax error in
5.00503:

#!/usr/bin/perl
use strict;

my $myobj = new MyTest;
my $var = "testMethod";

if (defined($myobj->$var)) {
my $ret = $myobj->$var();
print "Defined: $ret\n";
} else {
print "Not defined\n";
}

package MyTest;

sub new {
my ($proto) = @_;

my $self = {};
bless($self, "MyTest");
return $self;
}

sub testMethod {
return 52;
}
 
T

Tad McClellan

Aaron Brice said:
I need to check that a method whose name is given in $methodName
exists.


if ( $myobj->can($methodName) ) {
print "object has a $methodName() method\n";
}


See the docs for the universal class for more info:

perldoc UNIVERSAL
 
N

Nicholas Dronen

AB> I need to check that a method whose name is given in $methodName
AB> exists. In perl 5.8.0, "if (defined($myobj->$methodName))" seems to
AB> work, but not in 5.00503, so I'm wondering what the 5.00503 equivalent
AB> would be if it exists (upgrading is not an option right now). The
AB> following test code works in 5.8.0 but gives me a syntax error in
AB> 5.00503:

AB> #!/usr/bin/perl
AB> use strict;

AB> my $myobj = new MyTest;
AB> my $var = "testMethod";

AB> if (defined($myobj->$var)) {
AB> my $ret = $myobj->$var();
AB> print "Defined: $ret\n";
AB> } else {
AB> print "Not defined\n";
AB> }

my $obj = new MyTest;
my $meth = "testMethod";

if ($obj->can($meth)) {
print "Can: ", $obj->$meth(), "\n";
} else {
print "Cannot\n";
}

AB> package MyTest;

AB> sub new {
AB> my ($proto) = @_;
AB> my $self = {};
AB> bless($self, "MyTest");
AB> return $self;
AB> }

AB> sub testMethod { 52 }

Regards,

Nicholas
 
M

Martien Verbruggen

I need to check that a method whose name is given in $methodName
exists. In perl 5.8.0, "if (defined($myobj->$methodName))" seems to
work, but not in 5.00503, so I'm wondering what the 5.00503 equivalent

That will only give the right answer if $methodName returns a defined
value. It will also actually be executed, and if that has side
effects, you might not want to do that. Besides that, It'll throw a
fatal error if $methodName doesn't exist, even in 5.8.0, so I'm not
sure how you can say that it works.. you'd need an eval{} around it.
would be if it exists (upgrading is not an option right now). The
following test code works in 5.8.0 but gives me a syntax error in
5.00503:

In 5.8.0 $object->$varname is interpreted as a call to method $varname
on $object. In 5.00503 you need to make that more clear by adding some
parentheses: $object->$varname(); That should also make clear that it
is not what you think it is.

Observe what the following does (and note that it doesn't compile on
Perls before 5.6.0 for the abovestated reason):


#!/usr/local/bin/perl
use strict;
use warnings;

package Grub;

sub new { bless {}, $_[0] }
sub false { print "false called\n"; undef }
sub true { print "true called\n" ; 1 }

package main;

my $grub = Grub->new;

my $method = "grub";
print "grub defined\n" if eval { defined $grub->$method };
print "grub can()\n" if defined $grub->can($method);
$method = "false";
print "false defined\n" if defined $grub->$method;
print "false can()\n" if defined $grub->can($method);
$method = "true";
print "true defined\n" if defined $grub->$method;
print "true can()\n" if defined $grub->can($method);

Martien
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top