Sub::Signatures now works with methods

Ovid on 2004-12-05T01:49:38

This now works:

package Some::Package;
use Sub::Signatures 'strict';

sub new { bless {} => shift }

sub foo($self, ARRAY $bar) {
    return sprintf "arrayref with %d elements" => scalar @$bar;
}

sub foo($self, HASH $bar) {
    $bar->{this} = 1;
    $bar;
}

It doesn't work with subclassing, though. I think we'll have to add a new items to the import list:

use Sub::Signatures qw/methods/; # 'strict' is optional

I'm unsure of how to handle the special case of methods that can be called as both classes and methods. If it's a class name, it defaults to SCALAR. Otherwise, it defaults to the class name. I think I will have to set it up to ignore the type of the first argument with methods unless the programmer explicitly states it. That seems like a nice compromise.


lvalue subs?

djberg96 on 2004-12-05T14:36:37

What about lvalue subs? Say I want to do this:
$obj->foo = $hash_ref;
I want to make sure that the foo() method only accepts a hash. Would it be defined like this?
sub foo : method(HASH $x){
   $x->{whatever} = 1;
}
I may have the syntax wrong - it's been a while since I played with lvalue subs. But, I think you see what I'm trying to do. Possible?

Re:lvalue subs?

Ovid on 2004-12-05T16:43:04

Argh! I hadn't even considered lvalue subs. I'll have to think about them. At the present time I don't see support for them being offered without someone being gracious enough to send a patch :) (after it gets to the CPAN, of course.)