Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Ovid (2709)

Ovid
  (email not shown publicly)
http://publius-ovidius.livejournal.com/
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Monday February 04, 2008
02:03 PM

Things I Can't Do In Perl :(

[ #35576 ]

Update: Has anyone else been getting a lot of "can't connect" or similar errors. Lately I've been getting them constantly here, regardless of whether I'm posting from home or work.

I'd love to have a proper IDE. So tell me, how I figure out all of the method names in a class? Well, many are built up in BEGIN blocks:

BEGIN {
    no strict 'refs';
    foreach my $token (qw( plan comment test bailout version unknown yaml )) {
        my $method = "is_$token";
        *$method = sub { return $token eq shift->type };
    }
}

Or at runtime:

package Employee;
use base qw(Class::Accessor);
Employee->mk_accessors(qw(name role salary));

... via Moose:

package Point;
use Moose;

has 'x' => (is => 'rw', isa => 'Int');
has 'y' => (is => 'rw', isa => 'Int');

... maybe Spiffy:

package Keen;
use Spiffy -Base;
field 'mirth';
const mood => ':-)';

sub happy {
    if ($self->mood eq ':-(') {
        $self->mirth(-1);
        print "Cheer up!";
    }
    super;
}

Or via AUTOLOAD, defined in XS files, etc.

So how do I figure out those methods short of running the code? I can't. Hell, even if I could, this could easily trip me up:

package Foo;
use HTML::Entities 'encode_entities';

# later
if ( Foo->can('encode_entities') ) { ... } # danger!

I could check to see if the method was defined in the class hierarchy:

use Sub::Information;
if ( $class->isa( inspect( $class->can($method) )->package ) ) { ... }

But that's a pain and breaks in all sorts of annoying scenarios. But maybe it really does mean that this is the method I want? Well, it can't tell me that.

if ( $object->can('name') ) {
    $object->name($new_name);
}

Well, does that really take an argument, instead of &get_name? And is is really a void method?

These things are frustrating to me because what I would really love to do with Bermuda is include deserialization. I'm not sure how to do that. Perl is too random and has no real introspective capabilities. Perl 6 solves a lot of this, but I'm writing Perl 5. Moose might help, but having a generic serialization tool and a custom deserialization tool? That's no good (though I wouldn't mind custom tools to be built on a generic system).

If I want deserialization, I'll have to figure out how to add that to the Bermuda "islands".

Of course, this problem is wider than just Perl. Even being able to deterministically list all methods and know which ones are actually for "fields" (or slots, or properties, or whatever your term of choice is), doesn't tell me much about the intent of those methods. This, I think, is THE big hurdle that programming needs to solve. Behavior is in the code and intent is in our heads.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • This, I think, is THE big hurdle that programming needs to solve. Behavior is in the code and intent is in our heads.

    This is also why syntax is such a small part of maintainability. It's the difference between phonics and vocabulary.

  • So how do I figure out those methods short of running the code? I can't.

    Without some kind of static analysis, you are just not going to be able to do that, but even that most likely won't get you what you want.

    Think for a moment about introspecting classes without executing code. If you look at a language like Perl 6, where classes can be created, destroyed and modified at runtime. You need to have a highly dynamic meta-model and a meta-model is a living thing which means you have to execute some c

    • While Perl 6 will surely provide a better API to this kind of stuff, I actually suspect that we will actually lose some of this introspection capabilities in the end (unless someone write B::PIR or something).

      I can think of someone who has experience with Parrot, PIR, the Perl 6 object system, and the B:: modules.... I'm waiting for a couple of other pieces of Parrot to be ready before that will work.