[Israel.pm] function prototypes

sawyer x xsawyerx at gmail.com
Wed Jun 11 01:44:39 PDT 2008

I understand you're trying to use prototypes and I don't want to ruin
that "fun" (I don't really consider it fun, but some might).
However, I would strongly suggest against it and recommend you to read
FMTYEWTK About Prototypes in Perl, by the great Tom Christiansen.
It explains how prototypes really work in Perl, which is different
than the way we natively (and naively) think it should.

There is only one reason to use prototypes and that's for nifty and
nice looking functions that don't require parenthesis - personally I
prefer to keep the separation for me to use as clarity and cleanliness
in code (next to godliness, of course).
The whole "this is checking my input" claim (which I assume - only
assume - is what you're after) doesn't hold up because it's easy to
fool it.

I couldn't find the article online right now (it seems down both in
library.n0i.net and on two other links on Perl.com but I'm sure it's
available somewhere.

On Wed, Jun 11, 2008 at 8:19 AM, Yitzchak Scott-Thoennes
<sthoenna at efn.org> wrote:
> On Tue, June 10, 2008 1:25 pm, Shmuel Fomberg wrote:
> > I'm trying to write a function:
> > sub MetaField ($&) { ... }
> >
> > and call it like that: MetaField("data", { $_->ctx->{length} })
> > or:
> > MetaField("data", $_->ctx->{length})
> >
> > but only this works: MetaField("data", sub { $_->ctx->{length} })
> The "sub" is only optional if the prototype starts with &.  From perlsub:
>   An "&" requires an anonymous subroutine, which, if passed as the first
>   argument, does not require the "sub" keyword or a subsequent comma.
> I'm not sure how perl would know you meant the second example to be
> a sub that returns $_->ctx->{length} (based on whatever $_ is set to
> if and when MetaField ends up calling the sub), which is what the
> third does.
> If you somehow meant that $_->ctx->{length} is itself the coderef
> you want to pass, there isn't a convenient way to do that, since
> "&" explictly expects a new anonymous sub.
> I suggest reconsidering using prototypes at all.
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl

More information about the Perl mailing list