RE: SOLVED: how do i list the methods connected to a object?

2003-08-14 Thread Jeff 'japhy' Pinyan
On Aug 7, Dan Muey said:

So would this return the methods that the $obj sent to dump_functions
then, only shorter?

sub dump_functions {
use Class::Inspector;
my $r = ref $_[0];
return [ grep /^$r/, @{ Class::Inspector-methods($r,'full','public')} ]
}

Yeah.  The 'return' keyword can even be left out.

-- 
Jeff japhy Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
stu what does y/// stand for?  tenderpuss why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]


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



RE: SOLVED: how do i list the methods connected to a object?

2003-08-14 Thread Dan Muey

Cool! 1 quick question why do you have @methods?
If it's because you use @{ $methods } then $methods would have to be the string 
'methods' to be a reference to @methods, which doesn't have any data as far as I can 
tell.

Maybe I'm missing something?

Dan

 problem solved:
 
 given an arbitrary object, i can now get a dump of all the 
 functions connected to the object.
 
 print dump_functions( $obj );
 
 sub dump_functions
 {
 use Data::Dumper;
 use Class::Inspector;
 
 my ( $obj ) = @_;
 
 my ( $ref, $methods, @methods );
 
 $ref = ref $obj;
 $methods = Class::Inspector-methods( $ref, 'full', 'public' );
 
 @{ $methods } = grep /$ref/, @{ $methods };
 
 return Dumper( $methods );
 }
 
 
 
 On Tue, Aug 05, 2003 at 12:19:57PM +0200, Martin A. Hansen wrote:
  hi
  
  i wonder how i can list all the methods availible from a 
 given object?
  
  martin
  
  --
  To unsubscribe, e-mail: [EMAIL PROTECTED]
  For additional commands, e-mail: [EMAIL PROTECTED]
 
 -- 
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 
 

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



Re: how do i list the methods connected to a object?

2003-08-14 Thread Martin A. Hansen
On Wed, Aug 06, 2003 at 10:24:09PM +0100, Rob Dixon wrote:
 
 Martin A. Hansen [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED]
  On Tue, Aug 05, 2003 at 08:18:24PM +0100, Rob Dixon wrote:
  
   Ovid [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED]
--- Peter Scott [EMAIL PROTECTED] wrote:
 The problem here is that it will not print inherited or AUTOLOADed methods.
 [snip]

 So traverse the inheritance hierarchy and do the same thing with each class:

 http://search.cpan.org/author/SBURKE/Class-ISA-0.32/ISA.pm
   
That's certainly a decent 95% solution and if you *really*
need it, it's a good idea.  The original problem, though,
is not one that I want to encourage most people to solve
because for most programmers, when things get that complicated,
it turns out that there's an underlying design flaw that needs
to be addressed.
   
Of course, people seem to get mad when I say that :)
  
   I'll second that. Absolutely.
  
   If you've created an object and are asking, 'Now what can I do
   with this object?' then you're not designing software, you're
   laying bricks. Find a module that, from the documentation,
   looks like it will do what you want. Then commit to using that
   module with the documented interface alone. After that, if
   you find you've made the wrong decision, be prepared to backtrack
   as far as necessary to redeem yourself.
 
  first of all. objects and modules are for software reuse as i understand it. so
  installing and using modules from CPAN leaves me as a bricklayer!
 
 Being a bricklayer is a fine thing. But I thought this group was
 for beginner Perl programmers? If you take your module, poke it
 around a little, and say 'I wonder what this will do if...' then
 you will probably end up with Carhenge. Read the Owners' Manual
 and you'll find that you could have driven countless times to visit
 the magnificent results of a bricklayer at work across the country.
 
hah! so this is the sad story of OO perl. i have been warned off OO perl
repeatedly, but using Bio:: (which i believe is as extensive as Net:: and
HTTP::) is a major source of software reuse, especially because Bio:: stuff has
a lot of parsers of text flatfiles that keep altering format.

i was really hoping that someone more clever than i, had this problem solved
...

:oP

martin

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

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



Re: how do i list the methods connected to a object?

2003-08-14 Thread Ovid
--- Peter Scott [EMAIL PROTECTED] wrote:
 The problem here is that it will not print inherited or AUTOLOADed methods. 
 [snip]
 
 So traverse the inheritance hierarchy and do the same thing with each class:
 
 http://search.cpan.org/author/SBURKE/Class-ISA-0.32/ISA.pm

That's certainly a decent 95% solution and if you *really* need it, it's a good idea.  
The
original problem, though, is not one that I want to encourage most people to solve 
because for
most programmers, when things get that complicated, it turns out that there's an 
underlying design
flaw that needs to be addressed.

Of course, people seem to get mad when I say that :)

Cheers,
Ovid

=
Silence is Evilhttp://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]



RE: SOLVED: how do i list the methods connected to a object?

2003-08-14 Thread Dan Muey


 sub dump_functions {
 use Class::Inspector;
 my $r = ref $_[0];
 return [ grep /^$r/, @{ 
 Class::Inspector-methods($r,'full','public')} ] }
 
 Yeah.  The 'return' keyword can even be left out.

Nice! I'll leave it in for readability I think.

Wouldn't it need a ';' at the end?
[ grep .. ];

Or am I once again missing something I should know?

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



Re: SOLVED: how do i list the methods connected to a object?

2003-08-14 Thread Jeff 'japhy' Pinyan
On Aug 7, Martin A. Hansen said:

print dump_functions( $obj );

I'd think it's better for dump_functions() to return an array ref, and let
the end-user decide how to use its contents.

sub dump_functions {
use Data::Dumper;
use Class::Inspector;

my ( $obj ) = @_;

my ( $ref, $methods, @methods );

You never use @methods.

$ref = ref $obj;
$methods = Class::Inspector-methods( $ref, 'full', 'public' );

@{ $methods } = grep /$ref/, @{ $methods };
return Dumper( $methods );

That should probably be /^$ref/, for safety.  And I'd probably just do

  return [ grep /^$ref/, @{ Class::Inspector-methods(...) } ]

}

-- 
Jeff japhy Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
stu what does y/// stand for?  tenderpuss why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]


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



Re: how do i list the methods connected to a object?

2003-08-14 Thread Rob Dixon

Martin A. Hansen [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED]
 hi

 i wonder how i can list all the methods availible from a given object?

If you only need this for debugging, then the debugger will
do it for you. Stop at point where the object is still in
scope and type

  DB1m $object

(The DB1 bit being the debugger prompt). If you need it
at run time, then I suggest you change your mind! It is
possible, but there isn't a clean use for it that I can
think of :)

Rob




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



Re: how do i list the methods connected to a object?

2003-08-14 Thread Martin A. Hansen
aargh :o) i was hoping there was a simple answer to this. the object im
investigating do apparantly inherets stuff :/

im not into OO perl at all, and i guess this is the reason why.

ok, so the problem is rather complex, but thats just another perl problem then.
and this one must have been solved by others. i have been looking at the
Class::Inspector module on CPAN, which can track the inheritance i think.

ill add my code snippet here to elaborate on the specific problem. and attach a
test file for the parser.

the problem is that the objects $hit and $hsp gives access to a lot of usefull
functions, but what are they???



#!/usr/bin/perl -w

use strict;
use Data::Dumper;
use Bio::SearchIO;

my ( $script, $usage, @files, $file );

my ( $searchio, $result, $blast_report, $algorithm_type, $hit, $hit_name, $hsp, 
$bit_score,
 $query_beg, $query_end, $query_name, $subject_beg, $subject_end, $strand, $eval, 
$frame );

$script = ( split /, $0 )[ -1 ];

$usage = qq(
$script by Martin A. Hansen, July 2003.

$script parses blast repport files and writes GFF to STDOUT

Usage: $script blast blast blast
blast - blast report files

);

print $usage and exit if not @ARGV;

@files = @ARGV;

foreach $file ( @files )
{
$searchio   = new Bio::SearchIO ( -format = 'blast', -file = $file );
$result = $searchio-next_result;
$query_name = $result-query_name;

$result-database_name;
$algorithm_type = $result-algorithm;

while ( $hit = $result-next_hit )
{
$hit_name= $hit-name;
$hsp = $hit-next_hsp;

$eval= $hsp-evalue;
$bit_score   = $hsp-bits;
$strand  = $hsp-strand( subject );
$frame   = $hsp-frame;
$query_beg   = $hsp-query-start;
$query_end   = $hsp-query-end;
$subject_beg = $hsp-subject-start;
$subject_end = $hsp-subject-end;

if ( $strand eq 1 ) {
$strand = +;
} elsif ( $strand eq -1 ) {
$strand = -;
} else {
$strand = .;
}

print join( \t,
$hit_name,
$algorithm_type,
similarity,
$subject_beg,
$subject_end,
$bit_score,
$strand,
$frame,
Target \$query_name\ $query_beg $query_end ; E_value $eval
  );

print \n;
}
}



On Tue, Aug 05, 2003 at 05:48:04AM -0700, Ovid wrote:
 --- 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
BLASTN 2.2.5 [Nov-16-2002]


Reference: Altschul, Stephen F., Thomas L. Madden, Alejandro A. Schaffer, 
Jinghui Zhang, Zheng Zhang, Webb Miller, and David J. Lipman (1997), 
Gapped BLAST and PSI-BLAST: a new generation of protein database search
programs,  Nucleic Acids Res. 25:3389-3402.

Query= mt0001-av25gc10k167_AV25_01
 (110 letters)

Database: gbgene 
   2,966,325 sequences; 8,818,290,589 total letters


SOLVED: how do i list the methods connected to a object?

2003-08-14 Thread Martin A. Hansen
problem solved:

given an arbitrary object, i can now get a dump of all the functions connected to the 
object.

print dump_functions( $obj );

sub dump_functions
{
use Data::Dumper;
use Class::Inspector;

my ( $obj ) = @_;

my ( $ref, $methods, @methods );

$ref = ref $obj;
$methods = Class::Inspector-methods( $ref, 'full', 'public' );

@{ $methods } = grep /$ref/, @{ $methods };

return Dumper( $methods );
}



On Tue, Aug 05, 2003 at 12:19:57PM +0200, Martin A. Hansen wrote:
 hi
 
 i wonder how i can list all the methods availible from a given object?
 
 martin
 
 -- 
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]

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



RE: SOLVED: how do i list the methods connected to a object?

2003-08-14 Thread Dan Muey
 On Thu, Aug 07, 2003 at 11:28:10AM -0500, Dan Muey wrote:
  Yeah.  The 'return' keyword can even be left out.
  
  Nice! I'll leave it in for readability I think.
  
  Wouldn't it need a ';' at the end?
  [ grep .. ];
  
  Or am I once again missing something I should know?
 
 The last statement in a block doesn't need a semicolon.
 
Ooooh thanks Steve for the info!

 -- 
 Steve
 

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



how do i list the methods connected to a object?

2003-08-14 Thread Martin A. Hansen
hi

i wonder how i can list all the methods availible from a given object?

martin

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



Re: how do i list the methods connected to a object?

2003-08-11 Thread Rob Dixon

Martin A. Hansen [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED]
 On Tue, Aug 05, 2003 at 08:18:24PM +0100, Rob Dixon wrote:
 
  Ovid [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED]
   --- Peter Scott [EMAIL PROTECTED] wrote:
The problem here is that it will not print inherited or AUTOLOADed methods.
[snip]
   
So traverse the inheritance hierarchy and do the same thing with each class:
   
http://search.cpan.org/author/SBURKE/Class-ISA-0.32/ISA.pm
  
   That's certainly a decent 95% solution and if you *really*
   need it, it's a good idea.  The original problem, though,
   is not one that I want to encourage most people to solve
   because for most programmers, when things get that complicated,
   it turns out that there's an underlying design flaw that needs
   to be addressed.
  
   Of course, people seem to get mad when I say that :)
 
  I'll second that. Absolutely.
 
  If you've created an object and are asking, 'Now what can I do
  with this object?' then you're not designing software, you're
  laying bricks. Find a module that, from the documentation,
  looks like it will do what you want. Then commit to using that
  module with the documented interface alone. After that, if
  you find you've made the wrong decision, be prepared to backtrack
  as far as necessary to redeem yourself.

 first of all. objects and modules are for software reuse as i understand it. so
 installing and using modules from CPAN leaves me as a bricklayer!

Being a bricklayer is a fine thing. But I thought this group was
for beginner Perl programmers? If you take your module, poke it
around a little, and say 'I wonder what this will do if...' then
you will probably end up with Carhenge. Read the Owners' Manual
and you'll find that you could have driven countless times to visit
the magnificent results of a bricklayer at work across the country.

Cheers,

Rob



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



Re: SOLVED: how do i list the methods connected to a object?

2003-08-11 Thread Martin A. Hansen
On Thu, Aug 07, 2003 at 09:01:57AM -0500, Dan Muey wrote:
 
 Cool! 1 quick question why do you have @methods?

sorry old remnant. just delete.

martin

 If it's because you use @{ $methods } then $methods would have to be the string 
 'methods' to be a reference to @methods, which doesn't have any data as far as I can 
 tell.
 
 Maybe I'm missing something?
 
 Dan
 
  problem solved:
  
  given an arbitrary object, i can now get a dump of all the 
  functions connected to the object.
  
  print dump_functions( $obj );
  
  sub dump_functions
  {
  use Data::Dumper;
  use Class::Inspector;
  
  my ( $obj ) = @_;
  
  my ( $ref, $methods, @methods );
  
  $ref = ref $obj;
  $methods = Class::Inspector-methods( $ref, 'full', 'public' );
  
  @{ $methods } = grep /$ref/, @{ $methods };
  
  return Dumper( $methods );
  }
  
  
  
  On Tue, Aug 05, 2003 at 12:19:57PM +0200, Martin A. Hansen wrote:
   hi
   
   i wonder how i can list all the methods availible from a 
  given object?
   
   martin
   
   --
   To unsubscribe, e-mail: [EMAIL PROTECTED]
   For additional commands, e-mail: [EMAIL PROTECTED]
  
  -- 
  To unsubscribe, e-mail: [EMAIL PROTECTED]
  For additional commands, e-mail: [EMAIL PROTECTED]
  
  
 
 --
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 

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



Re: how do i list the methods connected to a object?

2003-08-11 Thread Steve Grazzini
On Wed, Aug 06, 2003 at 09:37:52AM +0200, Martin A. Hansen wrote:
 i find it very annoying that i cannot simple dump all the functions
 connected to a certain object.
 
 but enough nagging. even if this is a tricky problem, cant it be 
 solved?

 if using the ISA.pm as suggested, you are able to track the 
 inheritance solving 95% of the problem, then what is the remaining
 5% ?

Autoloaded subs and (maybe) lazily-loaded superclasses.

 i guess i wish for a widget to go
 
 print DumpFunctions( $obj );
 
 does it exist?

Here's a simple one (pay no attention to the symrefs).

package UNIVERSAL;

sub methods {
my $class = ref($_[0]) || $_[0];
if ($class eq 'GLOB') {
$class = ref *{$_[0]}{IO};
}
my @stack = ('UNIVERSAL', $class);
my (@meth, %seen);
while ($class = pop @stack) {
next if $seen{$class}++;
push @stack, @{$class.::ISA};
push @meth, grep exists $_, 
 map $class.::$_, keys %{$class.::};
}
@meth;
}

And then:

print $_\n for $obj-methods;

But as a substitute for reading the documentation, this is
practically worthless.  It can't distinguish between constants
and methods and imported subroutines, and it tells you nothing
about how to use them.

It does, however, give you the full name of whatever it finds,
so you can translate $fh-tell into 'perldoc IO::Seekable'.

HTH
-- 
Steve

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



Re: how do i list the methods connected to a object?

2003-08-10 Thread Rob Dixon

Ovid [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED]
 --- Peter Scott [EMAIL PROTECTED] wrote:
  The problem here is that it will not print inherited or AUTOLOADed methods.
  [snip]
 
  So traverse the inheritance hierarchy and do the same thing with each class:
 
  http://search.cpan.org/author/SBURKE/Class-ISA-0.32/ISA.pm

 That's certainly a decent 95% solution and if you *really*
 need it, it's a good idea.  The original problem, though,
 is not one that I want to encourage most people to solve
 because for most programmers, when things get that complicated,
 it turns out that there's an underlying design flaw that needs
 to be addressed.

 Of course, people seem to get mad when I say that :)

I'll second that. Absolutely.

If you've created an object and are asking, 'Now what can I do
with this object?' then you're not designing software, you're
laying bricks. Find a module that, from the documentation,
looks like it will do what you want. Then commit to using that
module with the documented interface alone. After that, if
you find you've made the wrong decision, be prepared to backtrack
as far as necessary to redeem yourself.

Software built on what a module /can/ do instead of what it's
/supposed/ to do isn't a useful thing in any way.

Rob



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



RE: SOLVED: how do i list the methods connected to a object?

2003-08-08 Thread Perry, Alan
On Thursday, August 07, 2003 11:28, Dan Muey wrote:

sub dump_functions {
use Class::Inspector;
my $r = ref $_[0];
return [ grep /^$r/, @{ 
Class::Inspector-methods($r,'full','public')} ] }
 
 Yeah.  The 'return' keyword can even be left out.

Nice! I'll leave it in for readability I think.

Wouldn't it need a ';' at the end?
[ grep .. ];

Or am I once again missing something I should know?

You can leave out the last semicolon in a block, but I never do.  It doesn't
hurt to have it there, and may save you if later you were to add another
statement after it but forget to add the semicolon to what *was* the last
statement.

I also always put in the return keyword.  It enhances readability as you
say, and you don't have to remember that Perl returns the last result.

There are a lot of shortcuts that Perl allows for that I avoid, to enhance
readability and preserve my sanity.  Heck, I even put use English; at the
top of my programs and use $PREMATCH, $MATCH, and $POSTMATCH instead of $`,
$, and $' because otherwise, I always have to look them up to remember what
the shortcut variables are.  Although, I still use $_, $1, $2, etc.  Go
figure...

Alan

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



RE: SOLVED: how do i list the methods connected to a object?

2003-08-08 Thread Dan Muey
 On Aug 7, Martin A. Hansen said:
 
 print dump_functions( $obj );
 
 I'd think it's better for dump_functions() to return an array 
 ref, and let the end-user decide how to use its contents.
 
 sub dump_functions {
 use Data::Dumper;
 use Class::Inspector;
 
 my ( $obj ) = @_;
 
 my ( $ref, $methods, @methods );
 
 You never use @methods.
 
 $ref = ref $obj;
 $methods = Class::Inspector-methods( $ref, 'full', 'public' );
 
 @{ $methods } = grep /$ref/, @{ $methods };
 return Dumper( $methods );
 
 That should probably be /^$ref/, for safety.  And I'd 
 probably just do
 
   return [ grep /^$ref/, @{ Class::Inspector-methods(...) } ]
 
 }

So would this return the methods that the $obj sent to dump_functions then, only 
shorter?

sub dump_functions {
use Class::Inspector;
my $r = ref $_[0];
return [ grep /^$r/, @{ Class::Inspector-methods($r,'full','public')} ]
}

Dan

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



Re: how do i list the methods connected to a object?

2003-08-08 Thread Ovid
--- 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 Evilhttp://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]



Re: how do i list the methods connected to a object?

2003-08-07 Thread Rob Dixon

Jenda Krynicky wrote:

 Guess what methods does this object support ;-)

 package Len;

 sub new {
 my $self;
 bless \$self, 'Len';
 }

 sub AUTOLOAD {
 $AUTOLOAD =~ s/^.*:://;
 return length($AUTOLOAD);
 }

Nice one Jenda! But I couldn't find it on CPAN :-/

Rob



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



Re: how do i list the methods connected to a object?

2003-08-06 Thread Martin A. Hansen
On Tue, Aug 05, 2003 at 08:18:24PM +0100, Rob Dixon wrote:
 
 Ovid [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED]
  --- Peter Scott [EMAIL PROTECTED] wrote:
   The problem here is that it will not print inherited or AUTOLOADed methods.
   [snip]
  
   So traverse the inheritance hierarchy and do the same thing with each class:
  
   http://search.cpan.org/author/SBURKE/Class-ISA-0.32/ISA.pm
 
  That's certainly a decent 95% solution and if you *really*
  need it, it's a good idea.  The original problem, though,
  is not one that I want to encourage most people to solve
  because for most programmers, when things get that complicated,
  it turns out that there's an underlying design flaw that needs
  to be addressed.
 
  Of course, people seem to get mad when I say that :)
 
 I'll second that. Absolutely.
 
 If you've created an object and are asking, 'Now what can I do
 with this object?' then you're not designing software, you're
 laying bricks. Find a module that, from the documentation,
 looks like it will do what you want. Then commit to using that
 module with the documented interface alone. After that, if
 you find you've made the wrong decision, be prepared to backtrack
 as far as necessary to redeem yourself.

first of all. objects and modules are for software reuse as i understand it. so
installing and using modules from CPAN leaves me as a bricklayer! so i have
found a module which is fulfilling my needs, but i find it very annoying that i
cannot simple dump all the functions connected to a certain object. now im
advised to read the module documentation (which can be poor) of this object,
AND the module documentation of ALL inherited objects! i find it rediculous to
go the documentation path, where you clearly should be able to understand the
code based on the code itself! i am aware that OO perl is about abstraction of
the code, but this is no good.

but enough nagging. even if this is a tricky problem, cant it be solved? if
using the ISA.pm as suggested, you are able to track the inheritance solving
95% of the problem, then what is the remaining 5% ?

i guess i wish for a widget to go

print DumpFunctions( $obj );

does it exist?

martin

 
 Software built on what a module /can/ do instead of what it's
 /supposed/ to do isn't a useful thing in any way.
 
 Rob
 
 
 
 -- 
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]

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



Re: how do i list the methods connected to a object?

2003-08-06 Thread Peter Scott
In article [EMAIL PROTECTED],
 [EMAIL PROTECTED] (Ovid) writes:
--- Martin A. Hansen [EMAIL PROTECTED] wrote:
 i wonder how i can list all the methods availible from a given object?

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. 
[snip]

So traverse the inheritance hierarchy and do the same thing with each class:

http://search.cpan.org/author/SBURKE/Class-ISA-0.32/ISA.pm

-- 
Peter Scott
http://www.perldebugged.com

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



Re: how do i list the methods connected to a object?

2003-08-06 Thread Jenda Krynicky
From: [EMAIL PROTECTED] (Martin A. Hansen)
 a bricklayer! so i have found a module which is fulfilling my needs,
 but i find it very annoying that i cannot simple dump all the
 functions connected to a certain object. now im advised to read the
 module documentation (which can be poor) of this object, AND the
 module documentation of ALL inherited objects!

Apart from the Net::... and HTTP:: messes deep hierarchies are pretty 
uncommon in the Perl world. And if the documentation of a module is 
poor, most likely the code will be as well.

 but enough nagging. even if this is a tricky problem, cant it be
 solved? if using the ISA.pm as suggested, you are able to track the
 inheritance solving 95% of the problem, then what is the remaining 5%
 ?

No. I mean the more you try to nearer to 100% you'll get, but it'll 
never be 100%. You can't prevent the module authors to be creative 
with autoloading and autodefinig.

Guess what methods does this object support ;-)

package Len;

sub new {
my $self;
bless \$self, 'Len';
}

sub AUTOLOAD {
$AUTOLOAD =~ s/^.*:://;
return length($AUTOLOAD);
}

Jenda
= [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery


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