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)

Saturday December 04, 2004
02:38 PM

Subroutine Signatures

[ #22139 ]

I finally got off my rear and started serious work on subroutine signatures. This now works:

use Sub::Signatures;

sub foo($bar) {
  print "$bar\n";
}

sub foo($bar, $baz) {
  print "$bar, $baz\n";
}

foo(1);     # prints 1
foo(2,3);   # prints 2, 3
foo(2,3,4); # fatal error

I capture the information necessary to do dispatch based upon typing, but the dispatch is currently only based on the number of arguments. Note that this works for subroutines and methods. Naturally, I have a TODO list:

  • Ignore prototyped subs
  • Allow typing
  • Can we make the typing optional?
  • How do we handle variadic subs? (I have ideas here)
  • Better error messages
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.
  • I really like this idea. I haven't ever minded the standard subs in perl, but this enhanced functionality will save time AND improve readaility. Very nice.
    • Thanks. I really hope it works as well as it appears to. I think I will have an alpha for the CPAN either tonight or tomorrow. I now have it set up to handle method dispatch "loosely" based on the number of args or a "string" mode where it dispatches on the number and type (ref) or args.

      My tests for the latter:

      use Test::More 'no_plan';
      use Test::Exception;

      my $CLASS;
      BEGIN
      {
          $ENV{DEBUG} = 1;
          chdir 't' if -d 't';
          unshift @INC => '../lib';
          $CLASS = 'S

  • Please just tell me it isn't based on source filters :-) Otherwise, it's not for me.

    But if it is and you are actually willing to use a filter beast in production, take a look at reformed-perl [netalive.org], which mostly forestalls your effort (though it's mainly OO-centrically) and includes more doodads. I don't know how refined it really is, but collaboration or something is probably be better than independent developments.

    • I found myself writing enough about source filters that I made a new journal entry to deal with this topic [perl.org].

      As for reformed-perl, it looks interesting and solves some problems with Perl 5's OO, but it's doing far more than I had intended and I suspect people would be less likely to use my more modest approach if bundled with that code. Personally I'd just use something like Class::MethodMaker with Sub::Signatures and get most of the benefits. Of course, if they want to borrow my code, they can.

      The fina