Naive Signatures

Ovid on 2004-09-25T18:10:41

Naive signature processing for Perl is fairly simple:

package Sub::Signatures;
use Filter::Simple;
    
FILTER_ONLY
    code => sub {
        s <
            sub\s*(\w+)\s*\(([^)]+)\)[^{]*{
        >gx;
    };

1;

And using it:

use Sub::Signatures;

sub foo($one, $two, $three) {
    return join ',' => $one, $two, $three;
}

Of course, that doesn't allow signatures on anonymous subs, it does no type checking or validating arguments lists in the actual sub, but it works. Maybe Sub::Overload will become a reality after all.

I should add that writing tests for source filters is very, very hard. First, having source filters generate valid code is difficult. Second, if they do generate valid code, it still might not perform as expected. In your filter block, it's very helpful to add a "print $_" as the last line of the filter subroutine. This will print out the resulting code and make it easier to see what your filter is actually doing.


Source filtering

Aristotle on 2004-09-26T14:34:24

Every time I read about that kind of stuff, I involuntarily have to think “so now we've caught up to C”… :-)