Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Checking if a method is defined in perl 5.00503

Reply
Thread Tools

Checking if a method is defined in perl 5.00503

 
 
Aaron Brice
Guest
Posts: n/a
 
      06-25-2003
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;
}
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      06-25-2003
Aaron Brice <(E-Mail Removed)> wrote:

> 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


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
 
Nicholas Dronen
Guest
Posts: n/a
 
      06-25-2003
Aaron Brice <(E-Mail Removed)> wrote:
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

--
"Why shouldn't I top-post?" http://www.aglami.com/tpfaq.html
"Meanings are another story." http://www.ifas.org/wa/glossolalia.html
 
Reply With Quote
 
Martien Verbruggen
Guest
Posts: n/a
 
      06-26-2003
On 25 Jun 2003 13:51:42 -0700,
Aaron Brice <(E-Mail Removed)> wrote:
> 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
--
|
Martien Verbruggen | +++ Out of Cheese Error +++ Reinstall
Trading Post Australia | Universe and Reboot +++
|
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
User-defined exception: "global name 'TestRunError' is not defined" jmike@alum.mit.edu Python 1 07-10-2008 12:37 PM
can call a method, but not a method within a user-defined class aidy Ruby 5 06-04-2006 08:10 PM
Using parenthesis with defined (#if defined(...)) Angel Tsankov C++ 1 04-05-2006 10:00 PM
object as a argument of the method in which the method is defined ? mike C++ 5 02-22-2006 01:24 PM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 PM



Advertisments