How to make sure class/instance methods are called in the right way
I was recently attending NPW09 and there I met several prominent members of the Perl community. It was a really exciting event for me. I feel like I learned more in two days then I could've learned from studying documentation for two months. One of the guys I met there was Jonathan Rockway (jrockway). I asked him how I could make sure that my subs where called in the right way. That is, I don't want a class (static) method to be called as an instance method and vice-versa. In a few minutes he cooked up the example below. use MooseX::Declare; class Test { method my_static_method (ClassName $class: Str $str) { print "Hello, $str.\n"; } method my_instance_method (Str $str) { print "Hello, $str.\n"; } } # These work Test->my_static_method("abc"); Test->new->my_instance_method("abc"); # These fail #Test->new->my_static_method("abc"); #Test->my_instance_method("abc");
I was amazed to see that it worked so well. Then only thing left now is to figure out how to make sure functions are actually called as functions and not as class/instance methods. If you've got a simple example, please comment. Side-note: Unfortunately I couldn't test this out at NPW09 on my Strawberry Perl installation on my laptop, because Devel::Declare wouldn't install on it. After a lot of talking (at NPW09) with Florian Ragwitz (rafl) and later with Ash Berlin (ash) at irc we were able to fix the problem. A patch to ExtUtils::Depends that fixes the problem has been submitted, and hopefully it will be available on CPAN soon. If you have any influence to speed things up, please apply some to the maintainers of ExtUtils::Depends. It would be wonderful if Devel::Declare was available to Windows users aswell.