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

Re: Passing arguments to subroutine

Thread Previous | Thread Next
Brad Baxter
June 5, 2007 10:55
Re: Passing arguments to subroutine
Message ID:
On Jun 1, 9:58 am, (Chas Owens) wrote:
> On 31 May 2007 10:58:54 -0700, Paul Lalli <> wrote:
> > On May 31, 10:15 am, (Yitzle) wrote:
> > > I suspect one of the tutorials that Google or points to has
> > > something in it that needs correcting.
> > Actually, it's an unfortunate truth that up until Edition 3, the Llama
> > itself recommended that you use the & to call subroutines...
> > Paul Lalli
> Alright, I am a pedantic jerk, but this struck as wrong.  I learned on
> 2nd edition Llama and Camel, so I dug up my old copies.  Learning Perl
> 2nd Edition says on page 92
>     The subname is th name of the subroutine, which is any name like
>     the names we've has for scalar variables, arrays, and hashes.  Once
>     again, these come from a different namespace, so you can have a
>     scalar variable $fred, an array @fred, a hash %fred, and now a
>     subroutine fred*.
>     * Technically, the subroutine's name is &fred, but you seldom need
> to call it that.
> Also, on page 93 it says
>     Invoking a User Function
>     You invoke a subroutine from within any expression by following
> the subroutine
>     name with parentheses, as in:
>         say_hello();          # a simple expression
>         $a = 3 + say_hello(); # part of a larger expression
>         for ($x = start_value(); $x < end_value(); $x += increment()) {
>             ...
>         }                     # invoke three subroutines to define values
> I checked Programming Perl (2nd Edition), just in case you meant the
> Camel instead of the Llama, and it appears* to talk about subroutines
> in the same was as perlsub currently does with no specific
> recommendations about whether to use & or not (it just explains all of
> the options and their side effects).
> The Llama (2nd edition) was published in 1997.  That was ten years
> ago.  You can see why I want to know where these people who are new to
> Perl are being told to use & as part of the subroutine name.  I assume
> there are some old tutorials out there (things live forever on the
> Internet) and they are reading bad, old code at work.
> * there may be a recommendation somewhere, but I couldn't find one in
> my cursory glance through it.

One thing I wonder about is that I see anonymous subs called
as &$anon or &$anon() in various places in the docs, e.g., perlipc,
perlmod, perlmodlib, perlref, -q What's a closure, -q How can I
pass/return a Function.  perl5004delta says:

New and changed syntax

    A subroutine reference may now be suffixed with an
    arrow and a (possibly empty) parameter list.  This
    syntax denotes a call of the referenced subroutine,
    with the given parameters (if any).

    This new syntax follows the pattern of
    "$hashref->{FOO}" and "$aryref->[$foo]": You may now
    write "&$subref($foo)" as "$subref->($foo)".  All
    these arrow terms may be chained; thus,
    "&{$table->{FOO}}($bar)" may now be written

so the $anon->() syntax as been around since then.

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.


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About