Slash Boxes
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 ]

schwern (1528)

  (email not shown publicly)
AOL IM: MichaelSchwern (Add Buddy, Send Message)

Schwern can destroy CPAN at his whim.

Journal of schwern (1528)

Wednesday December 26, 2007
05:16 AM

Method::Signatures - No $self = shift!! No filters!!

[ #35206 ]

Gentlemen! I bring you peace in our time!

package Foo;
use Method::Signatures;
method new (%args) {
    return bless {%args}, $self;
method get ($key) {
    return $self->{$key};
method set ($key, $val) {
    return $self->{$key} = $val;

And with NO SOURCE FILTERS! Just a hairy pile of black magic that, fortunately, I didn't write!

Really the kudos goes to Matt Trout who wrote Devel::Declare and the test/demo code which I just packaged up and shipped. There are, as you can see, a few nits. That trailing semicolon for one. The other is it doesn't work on "sub" so all you get right now is methods. These are all Devel::Declare issues.

The prototype syntax is very simplistic and does no checks. Expanding on that is the easy part, or at least just a SMOP. I plan on stealing a bunch of features from Perl 6.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • Very nice! My wrists thank you :)

    You should stop by #moose and chat, I know mst has been planning on writing a Perl 6-ish Moose wrapper using Devel::Declare (tentatively called Moose::Declare), but has been without the tuits to do it. We also already have some work towards method signature checking with MooseX::Method, which can easily be converted to use a Devel::Declare based syntactic sugar. There has also been some talk of pulling the type system out of Moose so it is easier to use it in non-Moose s

  • Innovative. Thanks for sharing this.

    I wish for customizing the name of "$self", but this is a nice start, and a welcome competitor to "", which I also liked it was a little weird because it was "self", not "$self", and didn't actually allow you to "shift" off $self through the system, just reference it.

    Wow, that was a run-on sentence.

    • Mark: You just inspired me to write, now on CPAN. :-)
      • Wow, Thanks Audrey. I like it.

        I wonder if it used "Sub::Exporter", if the "self" scalar name could be easily customized, like it can for "". However, I don't see any examples in the Sub::Exporter docs of exporting scalars. (Perhaps only because it is recommended against in most cases).
        • No, it doesn't use Sub::Exporter; as of version 0.05, it manages the exporting by itself.

          You can customize the "self" scalar name with an import line:

          use selfvars -self => 'this', -args => 'opts';

          Or only import self as $this but not args:

          use selfvars -self => 'this';

          Or only import self as $self:

          use selfvars -self;

          More suggestions are welcome as well!

      • Excellent! So long Spiffy.
      • Clever, but performance is awful. Tied variables. :(

        Then again, I've found some fascinating bugs in Method::Signatures.
        • True, but although there is technically a 10-fold difference between 1191563/s vs 114688/s per lookup, in practice I wonder how it's going to matter -- even if it's put in a loop that's going to look up $self more than 100000 times, the difference will still be less than one second. :-)
          • $self gets used an awful lot. It's one of those things that adds a drag to every single aspect of your code. As much as we'd like to, you can't always ignore the constant part of big-O.
      • That's beautiful, Audrey, thank you. One thought - I always pass by reference, that is to say, $vars. If that could make it into a future as an option, it would be awesome.
        • Do you mean that you always pass a single by-name hash reference argument, in a PBP-esque fashion?

          $obj->meth({ a => 1, b => 2 });

          If so, how about making available a %opts so you can say:

          sub meth {

          If you wish you can even import selfvars like this, populating named options into %_:

          use selfvars -opts => '_', -self;
          sub meth {

          Would that resemble what you want?

          • 0.10 of selfvars very definitely resembles what I want. I dub thee Lady Leet of Awesomeland!
  • Devel::Declare strikes me as likely to be unstable magic. It messes with the yy* parts of the parser during its operation. How likely is /that/ to keep working?

    I've always looked at that part of perl and gone "OMG, this is nasty looking" so perhaps this is not as horrible as I'm imagining. I just don't think it's the kind of thing of which stable software is made. In a year from now if it continues to work on 5.6 -> 5.11 I guess I could believe it then. There'll be more than enough unanticipated delta to
    • You'd have to ask Matt Trout for details, I know he has plans to move as much magic as possible out of the C hack and into Perl. At least it's stable with any given release of Perl. It's not like those exactly SNEAK up on us.