Checking if a method is defined in perl 5.00503

Discussion in 'Perl Misc' started by Aaron Brice, Jun 25, 2003.

  1. Aaron Brice

    Aaron Brice Guest

    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;
    }
    Aaron Brice, Jun 25, 2003
    #1
    1. Advertising

  2. Aaron Brice <> 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
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jun 25, 2003
    #2
    1. Advertising

  3. Aaron Brice <> 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
    Nicholas Dronen, Jun 25, 2003
    #3
  4. On 25 Jun 2003 13:51:42 -0700,
    Aaron Brice <> 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 +++
    |
    Martien Verbruggen, Jun 26, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oodini
    Replies:
    1
    Views:
    1,767
    Keith Thompson
    Sep 27, 2005
  2. mike
    Replies:
    5
    Views:
    337
    Ben Pope
    Feb 22, 2006
  3. Angel Tsankov
    Replies:
    1
    Views:
    867
    Victor Bazarov
    Apr 5, 2006
  4. Replies:
    1
    Views:
    498
    Sion Arrowsmith
    Jul 10, 2008
  5. aidy
    Replies:
    5
    Views:
    237
    Marcin Mielżyński
    Jun 4, 2006
Loading...

Share This Page