introspection (and Perl 6)

Jeff Anderson captvanhalen at gmail.com
Mon Jan 21 16:14:57 GMT 2008


$class.^can('foo')

That syntax is nearly as elegant as mud is not. :P


On Jan 20, 2008 3:04 PM, Ovid <publiustemp-londonpm at yahoo.com> wrote:
> --- jesse <jesse at fsck.com> wrote:
>
>  > > features as any other modern programming language.
> > >
> > > *cough*introspection*cough* :)
> >
> > You can certainly introspect Perl, if you're willing to sweet-talk
> > her enough.
>
> Introspection isn't build into Perl is, at best, heuristic in nature.
> You can take a look inside namespaces, but that's only part of it.  For
> example:
>
>   my $method = $class->can('foo');
>
> There are several problems with that (some of which the current Perl 6
> specification shares).  With the following code:
>
>   if ( my $method = $class->can('foo') ) {
>       my $hashref = $class->$method($arrayref);
>   }
>
> You have no way of knowing:
>
>   1.  If that's a method or a function.
>   2.  If that's a class or an instance method.
>   3.  If it really returns a hash reference.
>   4.  The number and type of arguments it accepts.
>
> So let's say you have this:
>
>   package My::Class;
>   use HTML::Entities 'encode_entities';
>
> With that code, My::Class->can('encode_entities') returns a reference
> to the encode_entities subroutine.  You can't call it like a method,
> nor can you determine its signature.  Perl's introspective capabilities
> are virtually non-existent.
>
> Of course, if we don't ignore AUTOLOAD, then we're faced with the
> problem of needing to override 'can' ...
>
> The fact that Perl 6's introspection model is also broken is also
> worrisome for me (http://www.perlcabal.org/syn/S12.html):
>
>   if my $method = $class.^can('foo') {
>       ...
>   }
>
> Now we at least are guaranteed that it's a method, but none of the
> other problems are solved (actually, the class versus instance one
> might, but I can't tell from the docs).  Meta-programming seems broken
> in Perl 6 :(
>
> Cheers,
> Ovid
>
> --
> Buy the book  - http://www.oreilly.com/catalog/perlhks/
> Perl and CGI  - http://users.easystreet.com/ovid/cgi_course/
> Personal blog - http://publius-ovidius.livejournal.com/
> Tech blog     - http://use.perl.org/~Ovid/journal/
>



-- 
jeffa


More information about the london.pm mailing list