Re: Object method prototypes?
by perrin (Chancellor) on Dec 14, 2007 at 00:28 UTC
|
To do this sort of argument checking in Perl, you use a module like Params-Validate. | [reply] |
Re: Object method prototypes?
by Joost (Canon) on Dec 14, 2007 at 00:36 UTC
|
Prototypes are determined at compile time and with that come several restrictions that make it a very bad fit for object-oriented constructs in perl. For one, it's terribly hard in specific cases and in general impossible for the perl compiler to determine an object's class at compile-time (i.e. by static analysis of the source code) so it won't be able to check which of any number of same-named functions will be used in a method invocation.
Also, perl's prototypes are really not about argument checking. They are about argument coercion - they don't really match what prototypes in C or Java for instance do.
See also Far More Than Everything You've Ever Wanted to Know about
Prototypes in Perl
| [reply] |
|
| [reply] |
Re: Object method prototypes?
by moritz (Cardinal) on Dec 14, 2007 at 00:07 UTC
|
Prototypes on methods are ignored because prototypes essentially change the way perl parses the code.
And since the type of a reference is not known at compile time, the parser has no way to find out if there is a prototype to a method, and if yes, which. | [reply] |
Re: Object method prototypes?
by dragonchild (Archbishop) on Dec 14, 2007 at 00:48 UTC
|
You don't miss prototypes. You only think you do. You might want to consider changing how you look at subroutine parameters and switch everything to be a hashref. This allows you to do a number of things:
- Future-proof your code by easily adding new parameters without modifying every caller
- Allow for optional parameters
- Make your parameters positionless
- Allow you to be able to have different classes handle a given invocation easily.
- Allow you to validate it more easily.
My criteria for good software:
- Does it work?
- Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
| [reply] |
|
sub my_method {
my ($self,%args) = @_;
# now do stuff with $self and %args
}
# which is called as
$object->my_method( foo => $value, bar => $bee );
Saves typing and IMHO is nicer to look at at both sides.
| [reply] [d/l] |
|
That works, as long as you don't mind creating and destroying an argument hash every time your method is called. Passing a hashref would avoid that, but unless you're writing performance-sensitive code it doesn't matter much.
| [reply] |
|
|
|
Re: Object method prototypes?
by shmem (Chancellor) on Dec 14, 2007 at 01:05 UTC
|
First argument to a method is its object, whose type is not known at compile time, so the first prototype
of a method has to be ignored anyways.
Does it make sense to test for the rest of them? I'm glad it was so decided that it doesn't, given the perl5
method call implementation.
Things would be different if a method's object wouldn't appear at all in its arguments, but be magically shoehorned
into the method's function body as, say, $self, which would mean having a special variable different from all other
special variables (of which perlvar tells) in that it is strictly localized and reserved - if and only if the
function in question is called as a method.
The current implementation is easier to implement (since it's done ;-) Have to see what Perl 6 says...
But anyways - you don't need prototypes, except maybe for the & (subroutine/block) type of prototype.
--shmem
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
| [reply] |
|
| [reply] |
Re: Object method prototypes?
by petdance (Parson) on Dec 14, 2007 at 02:58 UTC
|
| [reply] |