Perl6::Parameters – Perl 6-style prototypes with named parameters


        use Perl6::Parameters;

        sub mysub($foo, ARRAY $bar, *%rest) {


Perl6::Parameters is a Perl module which simulates Perl 6's named parameters. (When I talk about "named parameters" I mean something like the parameters you're used to from C, Java and many other languages--not pass-a-hash-with-the-parameters-in-it things.)

Like most other programming languages, Perl 6 will support subroutines with pre-declared variables the parameters are put into. (Using this will be optional, however.) This goes far beyond the "line-noise prototypes" available in Perl 5, which only allow you to control context and automatically take references to some parameters--lines like my($first, $second)=(@_) will no longer be necessary.

Although Perl 6 will have this, Perl 5 doesn't; this module makes it so that Perl 5 does. It uses some other Perl 6-isms too, notably the names for builtin types and the unary-asterisk notation for flattening a list.

Crafting Parameter Lists

Crafting parameter lists is simple; just declare your subroutine and put the parameters separated by commas or semicolons, in parenthesis. (Using a semicolon signifies that all remaining parameters are optional; this may not be available this way in Perl 6, but I'm assuming it is until I hear otherwise.)

Most parameters are just variable names like $foo; however, more sophisticated behavior is possible. There are three ways to achieve this.

The first way is by specifying a type for the variable. Certain types make the actual parameters turn into references to themselves:

  • ARRAY $foo

    This turns an array into a reference to itself and stores the reference into $foo.

  • HASH $foo

    This turns a hash into a reference to itself and stores the reference into $foo.

  • CODE $foo

    This turns a subroutine into a reference to itself and stores the reference into $foo.

  • SCALAR $foo

    This turns a scalar into a reference to itself and stores the reference into $foo.

  • GLOB $foo

    This turns a typeglob into a reference to itself and stores the reference into $foo. Typeglobs will be going away in Perl 6; this type exists in this module so that it's useful for general use in Perl 5.

  • REF $foo

    This turns any parameter into a reference to itself and stores it into $foo.

    This only works in Perl 5.8. Otherwise, it's treated the same as any other unrecognized type name.

  • AnythingElse $foo

    This has no effect in this module; it's treated as though you'd typed $foo without the AnythingElse.

For example, if a subroutine had the parameters ($foo, HASH $bar, CODE $baz) and was called with ($scalar, %hash, &mysub) the subroutine would get the contents of $scalar, a reference to %hash and a reference to &mysub.

The second way is by supplying an actual array or hash as a parameter name. This requires an array or hash to be passed in for that parameter; it preserves the length of the array or hash.

The final way is only available for the last parameter: if an array or hash is prefixed with an asterisk, that array or hash will be filled with any additional parameters.


  • In Perl 6, parameters will be passed by constant reference; in this module parameters are passed by value.

  • In Perl 6, putting an is rw at the end of a parameter will make it read-write; trying to use is rw with this module will cause an error.

  • @_ and %_ may only be used for the last parameter, and then only when prefixed by an asterisk; any other use causes undefined behavior.

  • In Perl 6 a definition like HASH $foo will take either a literal hash (with a % sign in front of it) or a reference to a hash; this module requires a % sign. (Similar limitations apply for arrays.)


None known--but if you find any, send them to <> and CC <>.


Brent Dax <>


Copyright (C) 2001 Brent Dax.

This module is free software and may be used, redistributed and modified under the same terms as Perl itself.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 173:

Non-ASCII character seen before =encoding in '–'. Assuming ISO8859-1