Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

static method checker for perl?

by perl5ever (Pilgrim)
on Apr 07, 2009 at 19:41 UTC ( #756123=perlquestion: print w/replies, xml ) Need Help??
perl5ever has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way to statically check method calls?

This would entail annotating variable declarations with the name of a class or protocol so that any method calls on that variable could be validated (to make sure it's spelled correctly, has a legal number of arguments, etc.) It may not be able to check as much as a strongly typed language like Java could, but just checking that the method name is spelled correctly would be a big help.

I realize that perl can be used as a very dynamic language, but being able to do any amount static checking would be extremely helpful.

Replies are listed 'Best First'.
Re: static method checker for perl?
by moritz (Cardinal) on Apr 07, 2009 at 20:57 UTC
    The simplest answer is "no".

    First of all methods don't have signatures in plain Perl 5, so there's no way a code checker could actually know arguments a sub expects. It could guess, though.

    Second, variables don't have type annotations, so you'd have to introduce these first.

    Thirdly, methods can be added at run time. In the simplest case it's something like *methodname = sub { ... }, in the most general (and thus hardest) case something like that would happen in a string eval, completely undetectable at compile time.

    All of those can be ignored to some extend, and you can still try to write some static checker, which would be more heuristically than correct.

    So you want a Perl with explicit signatures, type annotations and no eval. Luckily there's something that provides the first two of these criteria - Perl 6. It can't do complete, reliable checking at compile time, but at least it allows for much better introspection, and much better compile time detection if you ignore that an eval() might add methods.

Re: static method checker for perl?
by DStaal (Chaplain) on Apr 07, 2009 at 20:01 UTC

    In general no. Perl is a little bit too dynamic for that: many modules create methods at runtime (usually using AUTOLOAD, but there are other ways...), and all methods and functions in Perl take exactly one argument: A list, which will be put in the @_ array. (Even prototyped functions take a list. They just define what's in the list.)

    So the problem is that none of the above is known until runtime, when a specific method call is made. It may be possible to answer those questions for a fair number of simple cases, but default arguments and AUTOLOADed methods are both fairly common practice.

      Even prototyped functions take a list. They just define what's in the list.

      While that may be technically true (Perl uses a stack), I'm not sure that's helpful. Prototyped functions parse differently from regular Perl functions, so the apparent behavior of these functions may not make it apparent that they take lists of arguments.

        Agreed. I just wanted to indicate I was aware of prototypes. (I didn't feel a more in-depth discussion on them was needed to answer the question given.)

        But yeah, prototypes cause the function to apply different contexts to their arguments (except when they don't) and so they can appear to take something other than a list. Unless you ignore the prototype. Or work around it some way. Or...

        End result, for this discussion: Prototypes in Perl don't actually help all that much with static code analysis. They can help a bit, but not enough to make code analysis really useful.

Re: static method checker for perl?
by kyle (Abbot) on Apr 07, 2009 at 20:08 UTC

    Using Params::Validate, you can check a sub's parameters at run time. After that, a test suite with good code coverage should ensure pretty well that you didn't fat finger a method name.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://756123]
Approved by almut
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2018-02-19 23:39 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (266 votes). Check out past polls.