[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]
Method::Signatures and the \@foo prototype
Back in January, I put out an RFC about having a method declaration syntax.
http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2008-01/msg00459.html
I also opined about adding simple, named parameters which we could extend in
the future.
http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2008-01/msg00458.html
Now we have both (in version 0.05).
http://search.cpan.org/dist/Method-Signatures
One of the points of breakdown in the named parameters discussion was whether
the parameters should copy or alias. I prefer copying, since that's what we
already do and it prevents action at a distance, whether accidental or deliberate.
# With aliasing
my @foo = (1,2,3);
$obj->thing(@foo); # did @foo change? You don't know!
And while the above is possible now by manipulating @_ directly, it's use is
discouraged because of the yucky @_ syntax. The gun is in the cabinet if you
need it. If we alias by default the gun is in everyone's hands, at all times.
[1] [2]
Compare with this:
my @foo = (1,2,3);
$obj->thing(\@foo); # yes, @foo might change
The caller is on alert, @foo might be changed. This is good. This makes the
caller aware to check for possible side-effects. The problem is reference
syntax is yucky. What if that part could go away?
method add(\@args) {
$_++ for @args;
}
my @foo = (1,2,3);
$obj->add(\@foo);
print "@foo"; # 2 3 4
Isn't that nice? This is what Method::Signatures does. I think it's a very
Perl5-ish solution to pass-by-reference. The signature has symmetry with what
gets passed into the method. It allows easy reference manipulation and
aliasing behavior without the action-at-a-distance.
Now that Method::Signatures works we have a way to seriously prototype and
experiment with function signatures in Perl 5. Without a source filter.
Without performance loss.
[1] A friend of mine was showing me his new Glock 9mm and showing off the
safety features saying "you could hammer nails with it and it won't go off!"
Sure, but I'm not going to build a house with it.
[2] Perl 6 and other "everything is an object" languages can get away with
this because it permeates the whole language. Automatic pass-by-reference is
built in because everything is a reference and the language reminds you of that.
--
Just call me 'Moron Sugar'.
http://www.somethingpositive.net/sp05182002.shtml
[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]