Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Use method/function signatures with Perl

by dragonchild (Archbishop)
on Dec 06, 2004 at 13:57 UTC ( [id://412648]=note: print w/replies, xml ) Need Help??


in reply to Use method/function signatures with Perl

I'd be interested to see
  • what kind of speed penalty your dispatching has
  • what kind of impact it has on caller() and other introspective code
  • if it correctly handles
    sub foo(CGI $cgi) { ... } sub foo(CGI $cgi, ARRAY $arr) { ... }
A few questions:
  • Why do you use ref() (or potentially isa() ...) over Scalar::Util's blessed()? I've had serious issues when HTML::Template used isa() to determine ARRAY when I had overloaded stringification on my object. Using blessed() or can() would have solved that problem ...
  • Is the problem with more than one package in a file your problem or Filter::Simple's problem? If it's F::S's problem, you should state that as a limitation of the engine you're using.
  • Why don't you allow sub foo(@arr) { ... } instead of sub foo(ARRAY $arr) { ... }? Wouldn't that give me the prototype sub foo(@);, allowing me to rewrite push?

And, finally ... why did you use a source filter yourself instead of using Attributes? I would have thought that Attributes would have provided a tested source filter so you could have focused on your core requirements instead of worrying about the source filter yourself.

Being right, does not endow the right to be rude; politeness costs nothing.
Being unknowing, is not the same as being stupid.
Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Replies are listed 'Best First'.
Re^2: Use method/function signatures with Perl
by Ovid (Cardinal) on Dec 06, 2004 at 15:36 UTC

    There's a lot of stuff here and I can't answer it all as I get ready for work so I'll rush through this.

    The speed penalty is most at compile time. There's actually not much of a runtime speed penalty if code without this module attempts the same level of validation. If you don't mind the compile time hit, you're probably OK. In a persistent environment (such as mod_perl) you may never notice. Caveat: I haven't benchmarked this, so take what I say with a grain of salt. I was looking for programmer efficiency rather than CPU efficiency.

    It should have no impact on caller because internally it uses goto to subvert the call stack. In this respect, it's even better than some hand-rolled code. However, I didn't write tests for this. I should do that. And can you give other introspective examples you'd like to see tested?

    As for the code snippet you tested, yes it will handle that, if you use 'strict' mode.

    use Sub::Signatures 'strict'; sub foo (CGI $cgi) {...} sub foo (CGI $cgi, ARRAY $arr) {...}

    In 'strict' mode, it considers the types of variables. That would actually work in 'loose' mode, but only because each subroutine has a different number of variables. Naturally, that would be more bug-prone. I wonder if I should have made 'strict' the default instead of 'loose'?

    I used ref instead of Scalar::Util on the "simplest thing that could possibly work" principle. If you can give me a clear code snippet showing why ref is inferior (or point me to a resource.) I'll happily change it. (Update: Of course, I seem to recall some of the issues you mention. Hmm, I don't think I have a choice but to change it.)

    The problem with more than one package per file is a combination of my code and how Filter::Simple works. It's very important that I know my calling package when setting argument lists with the subroutines so I determine this in &import. However, Filter::Simple keeps scanning through the code and cheerfully skips past package declarations, thus meaning I could alter subs in the wrong package. I thought about trying to parse out the the declarations and it didn't seem too hard, but Perl has so many odd corners that I thought there would be a good chance of missing something. As a result, I opted to keep it simple for my initial release.

    I don't use prototypes because they're useless with methods and I wanted to limit the differences between using functions and methods. Right now they behave almost identically. What I didn't want was having to constantly respond to the following bug report:

    sub foo(@bar) {...} # works fine sub foo($self, @bar) {...} # how would this work?

    And I didn't use attributes because even though I knew I could get something like signatures working, the real problem I wanted to transparently solve was signature-based multi-method dispatch. I don't know if that's possible with using attributes and a relatively straightforward syntax.

    Cheers,
    Ovid

    New address of my CGI Course.

      I used ref instead of Scalar::Util on the "simplest thing that could possibly work" principle. If you can give me a clear code snippet showing why ref is inferior (or point me to a resource.) I'll happily change it. (Update: Of course, I seem to recall some of the issues you mention. Hmm, I don't think I have a choice but to change it.)

      Another question here - are you planning on allowing objects that happen to be implemented as ARRAY's in for ARRAY parameters? This will mean that your code is going to be ... complex, to say the least.

      Being right, does not endow the right to be rude; politeness costs nothing.
      Being unknowing, is not the same as being stupid.
      Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
      Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

        I'm not sure I understand your question. My code doesn't actually care about the specific implementation. Can you give me an example of what you're thinking?

        Cheers,
        Ovid

        New address of my CGI Course.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://412648]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-24 02:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found