Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: Use method/function signatures with Perl

by Ovid (Cardinal)
on Dec 06, 2004 at 15:36 UTC ( [id://412665]=note: print w/replies, xml ) Need Help??


in reply to Re: Use method/function signatures with Perl
in thread Use method/function signatures with Perl

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.

Replies are listed 'Best First'.
Re^3: Use method/function signatures with Perl
by dragonchild (Archbishop) on Dec 06, 2004 at 15:50 UTC
    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.

        Let's say I have a subroutine defined as
        sub foo (ARRAY $arr) { ... }

        And, I have a class Bar. Today, it is defined as

        package Bar; sub new { bless [], shift }

        This means that

        my $bar = Bar->new; foo( $bar );

        will work just fine. But, what happens if the implementation of Bar changes from arrayref to hashref? This is a completely internal change, but one that will break the code listed above.

        Personally, I would define it to be

        if ($signature) { if (blessed $x) { return TRUE if $x->isa( $signature ); } elsif (ref $x) { return TRUE if ref $x eq $signature; } else { die "$x doesn't match $signature\n"; } }

        In other words, if it's been blessed, you only consider the class - not the underlying implementation.

        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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-23 09:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found