develooper Front page | perl.beginners | Postings from June 2007

Re: Passing arguments to subroutine

Thread Previous | Thread Next
From:
Chas Owens
Date:
June 5, 2007 11:35
Subject:
Re: Passing arguments to subroutine
Message ID:
58ce48dc0706051134w66ed0a50s101b93c47f594429@mail.gmail.com
On 6/5/07, Brad Baxter <baxter.brad@gmail.com> wrote:
snip
> Apparently, unlike with named subs, both &$anon()
> and $anon->() ignore prototypes.  However, like named
> subs &$anon gets the caller's @_.  But that is almost
> never mentioned afaict at the places in the docs where
> the &$anon style call is used.
>
> This isn't intended to be a criticism; I just wonder if some
> small number of newcomers reading the docs might
> be picking up calling habits from this.
snip

Yet another reason to avoid Perl 5 prototypes (they don't survive the
sub being turned into a reference).  Happily Perl 6 fixes this

pugs> sub foo ($arg) { $arg }
undef
pugs> foo()
*** No compatible subroutine found: "&foo"
    at <interactive> line 1, column 1 - line 2, column 1
pugs> foo("arg")
"arg"
pugs> my $coderef = \&foo;
sub {Syn "block" {Var "$arg"}}
pugs> $coderef()
*** No compatible subroutine found: "$coderef"
    at <interactive> line 1, column 1 - line 2, column 1
pugs> $coderef("arg")
"arg"

Admittedly, Pugs isn't doing a good job with the error message yet,
but at least it is working the way one would expect.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About