What I've been thinking about doing is hijacking the pseudo-hash syntax and using it for this. So if you did
my Animal $spot = Dog->new;
$spot->Reproduce; # fine
$spot->DriveToWork # not OK
some_other_function_that_returns_an_object()->method # ignore this
I was thinking of using the
B framework to get access to the parsed code. In a very simple implementation you would wander around the parsed code, looking for methods called directly on a variable, find out that the variable is a lexical declared as
my Animal and end up checking
Animal->can("Reproduce") and
Animal->can("DriveToWork"). In a more sophisticated implementation, instead of using
can, the method information would be held in say $Animal::INTERFACE and could also include info about method arguments or other useful stuff.
Objections:
- AUTOLOAD methods. These are OK as long as you can supply the list of possible valid methods. If you can't provide that list then static checking is probably not suitable for that class anyway. Even if the list of methods is large/infinite you could always provide a function that says yes or no to a particular method name.
- Things like some_function()->Reproduce. To which I say, if you want real static checking, don't use Perl (5).
I had a quick look at B last weekend and it looks like it should be possible by navigating through a B tree of opcodes looking for the method calls but it could be a lot of work to write the code because you need to cater for each opcode (athough most of the time you're just ignoring them I think).
If anyone else fancies a challenge...
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.