--- "Martin A. Hansen" <[EMAIL PROTECTED]> wrote:
> hi
> 
> i wonder how i can list all the methods availible from a given object?
> 
> martin

Hi Martin,

The simple answer:  You can't.  Welcome to Perl.

The long answer:  there are a variety of strategies you can use to try and figure this 
out, but
all of them fall short.  For example, here's a snippet that prints out defined 
subroutines (or
methods) in a given package:

  {
    no strict 'refs';
    my $namespace = sprintf "%s::", $CLASS;
    foreach (keys %{$namespace}) {
      my $test_sub = sprintf "%s%s", $namespace, $_;
      print "$test_sub\n" unless defined &$test_sub;
    }
  }

The problem here is that it will not print inherited or AUTOLOADed methods.  It might 
be
sufficient for your needs now, but it's fragile.  You can also test whether or not a 
particular
method *is* implemented:

  if ($object->can('method_name_to_test')) {
    # this works if $object can have methods called on it.  It will find
    # inherited methods.  It can also find AUTOLOADed methods if they've
    # already been called and installed in the symbol table
  }

A slightly more robust version of that syntax:

  if (UNIVERSAL::can($object, $method_name) {
    # almost the same thing, but doesn't die a horrible death if, for example,
    # $object is undef
  }

If you explain the problem you're trying to solve, we might be able to come up with a 
better
solution.

Cheers,
Ovid

=====
Silence is Evil            http://users.easystreet.com/ovid/philosophy/indexdecency.htm
Ovid                       http://www.perlmonks.org/index.pl?node_id=17000
Web Programming with Perl  http://users.easystreet.com/ovid/cgi_course/

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to