Mechanical

Geek Alert: Perl

Another geek entry you can disregard.

I've fundamentally altered the way Perl functions and, if my code works, it's a strong change for the better.

Before, if you wanted to overload a method as both an accessor and mutator and ensure that your method would be called correctly, you had to do something like this:

sub name {
    my $self = shift;
    if (1 == @_ && ! ref $_[0]) {
        $self->{name} = shift;
        return $self;
    }
    elsif (! @_) {
        return $self->{name};
    }
    else {
        croak "Unknown arguments to name()";
    }
}

What the hell is that? Isn't Perl supposed to be more concise than other languages? In fact, here's how something similar would look in Java (it's impossible to duplicate this exact functionality in Java because return types may not change):

public String name () {
    return name;
}

public String name(String name) {
    this.name = name;
    return name;
}

Well, now it looks like Java is more concise than Perl. That's awful! So I fixed it by writing a CPAN module named Sub::Signatures.

use Sub::Signatures qw/strict methods/;

# ...

sub name($self) {
    return $self->{name};
}

sub name($self, $name) {
    $self->{name} = $name;
    return $self;
}

This makes life much, much simpler.

  • Current Mood: accomplished accomplished
i know it's useful -- i'm glad to use it, but -- it has its issues, and it masks problems.

how well does Sub::Signatures play with others, like Class::MethodMaker?

and what's the compile-time hit? ReasonableParams has a pretty big compile time hit, iirc.
Sub::Signatures does not interfere with how Class::MethodMaker works. If you read the docs on the latest version, you can get an idea of what's going on. In fact, I'm hoping to make a "debug" mode that will write out what the new module will look like so people can see what's happening under the hood. I might even see if I can add caching to that.</p>

As for the compile-time hit, I'm not too worried about it. In a persistent environment, it's it really doesn't matter. In a non-persistent environment, whether it matters depends upon your personal needs.



When I run the tests, Sub::Signatures is loaded and executed probably 10 times. All of the tests run in under a second on my box, though, so I'm not overly concerned. If you really need that extra tenth of a second of performance, though, you would either be doing some serious profiling work or you wouldn't be using Perl :)

I miss you too :(

Sean and I will be having dinner tomorrow night and we should buy our tickets then. We'll see you on the 23rd :) (or was it Christmas eve? Now I forget)
Ok, so while this sounds really cool, I just don't have the time to dig into anything new with this queue running over my head (bad pun?) So keep the conversation going (here or perlmonks) and I'll catch up later. Btw, how does it do with nasty inheritance? (ya know, like the way we do it here?)
It doesn't have the weird inheritance issues that we had with ReasonableParams because it does not use prototypes. However, it's vitally important that you actually use the 0.11 version which should be on the CPAN shortly. The previous version didn't allow colons in class names. Whoops :)