AlienResidents has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I've been looking through some code, and I can't for the life of me find out where this is documented, and least of all it's fairly difficult to search for '*;********' etc.

Anyway, I'm trying to find out where I can read more about this use of sub:

sub name(*;*********************************************************** +***********************) { ... }

Specifically around the use of the semicolon (wtf does it do/represent?), and then the multiple asterisks afterwards.

Thanks.

Replies are listed 'Best First'.
Re: sub many asterisk semicolon separated
by GrandFather (Saint) on Jan 13, 2014 at 00:30 UTC

    As LanX suggests the ; separates mandatory arguments from optional arguments. The * is pretty much a "match any argument type". The probable intent of the prototype is to ensure that the sub is normally called with at least one argument and may be called with somewhat over 80 arguments.

    That looks pretty nasty to my eye. I'd be inclined to get rid of the prototype and add a runtime check that does a decent job of validating the arguments. Along with a decent set of unit tests that would provide easier to understand code, better documentation (the unit tests show expected usage for the sub) and much better regression testing over the long haul.

    True laziness is hard work

      Thank you for the additional insight into a probable why there are many asterisks after the semicolon.

Re: sub many asterisk semicolon separated
by LanX (Saint) on Jan 13, 2014 at 00:19 UTC
    my first guess is perlsub#Prototypes

    * means only type globs allowed (# hmm ... only partly correct, see below)

    ; means here everything following the first argument is optional.

    edit

    A * allows the subroutine to accept a bareword, constant, scalar expression, typeglob, or a reference to a typeglob in that slot. The value will be available to the subroutine either as a simple scalar, or (in the latter two cases) as a reference to the typeglob.

    ...

    A semicolon (; ) separates mandatory arguments from optional arguments. It is redundant before @ or % , which gobble up everything else.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      Thanks for this. I should have read a bit more first before submitting this.

Re: sub many asterisk semicolon separated
by Athanasius (Archbishop) on Jan 13, 2014 at 06:23 UTC

    Hello AlienResidents, and welcome to the Monastery!

    As LanX and GrandFather have explained, the given prototype has the effect of requiring one argument and allowing up to 82 additional arguments.

    But it has another effect: it coerces each argument into scalar context. To demonstrate this, here is a script which takes advantage of the fact that calling a subroutine with an initial ampersand — &name(...) — disables the prototype for that function call. So does calling the subroutine via a reference:

    #! perl use strict; use warnings; sub name(*;**) { printf "First argument: %s\n" . "Second argument: %s\n" . "Third argument: %s\n\n", @_; } printf "\nname() has prototype (%s)\n\n", prototype('name'); my @array = ('foo', 'bar'); &name('1a', 'u', 'v'); name('1b', 'u', 'v'); &name('2a', ('x', 'y')); name('2b', ('x', 'y')); &name('3a', @array, 'extra_1'); name('3b', @array, 'extra_2'); my $fp = \&name; $fp->('3c', @array, 'extra_3');

    Output:

    See Far More than Everything You've Ever Wanted to Know about Prototypes in Perl -- by Tom Christiansen.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: sub many asterisk semicolon separated
by AlienResidents (Novice) on Jan 13, 2014 at 00:29 UTC

    So uhh, moments after I submitted this, I found the prototypes section in perlsub. Everything after a semicolon is optional.

    Sorry for a question that is clearly explained in the perldocs.