Re: [Parseperl-discuss] determining object types w/o executing???
Brought to you by:
adamkennedy
From: Adam K. <ad...@ph...> - 2005-08-31 19:09:19
|
Mark Ethan Trostler wrote: > Howdy All, > A very strange conglomeration events has led me here.... > I'm very interested in attempting to determine what the type of an > object would be at runtime - without actually having to run the program > - say to determine available methods from a debugger (as mentioned in > the recent article on perl.com). Because Perl is loosely types, you can't really do this. A variable could contain just about anything. > Clearly the code must be run to the point where the object is in the > code to then 'ref' it to find out it's type - then something like PPI > can be used to walk up the inheiritance tree collecting possible > subroutines. If you already have the the code loaded, then something like Class::Inspector does a pretty good job of working out the available methods by walking the ISA tree. > Actually executing the program/script everytime just to determine > the type of the object is a bummer - as who knows what the side-effects > are of running & re-running the code over & over. Right. It's generally considered dangerous. > Have you guys thought about how to tackle this? It does seem the > code has to be re-run everytime you would want to know the type of an > object. > I have a bizarro solution involving attributed-'my' variables to > determine object types (replete w/code instrumentation via PPI) BUT > re-running the code everytime sucks. > Other then detecting whether the code has been modified before > having to re-run it again have you guys come up with something to > determine varaible types? > It can't be possible... right??? Technically no, it's impossible. But then PPI is about dealing with the impossible :) To repeat something you're going to be repeating quite a bit when people ask "can I" questions, if YOU (as a person) can look at a piece of code in a document and know what it is, then you should be able to write code with PPI to work it out programmatically. But take something like package Foo; sub foo { my $self = shift; my $param = shift; ... } Can you tell me what those are? You can probably guess that $self is going to be an object of type 'Foo' (or a subclass). But what is $param? You can't tell. There's a range of situations in which you can guess reasonably well, for example... my $foo = My::Class->new; But in general, Perl vars are untyped, so no you can't tell. Adam K |