Camelia
Prev: Looping over keys of a hash

Subroutines in Perl 6

  1. Simple definition with required parameters
  2. Subroutine with arbitrary number of parameters
  3. Passing arrays and hashes
  4. Multiple signatures
  5. Optional parameters
  6. Named only parameters
  7. No parameter definition - perl 5 style
  8. Fibonacci
  9. Creating Operators
  10. Create your own operator

For more details look at S06 .

Subroutines in Perl 6

I have been thinking for some time now on how to teach subroutines in Perl 6 and I don't have a good answer yet. So for now, instead of some methodological introduction I'll just try to throw together a few examples and we'll see if something comes out of that?

In Perl 6 just as in Perl 5 one can define a subroutine without specifying the list of parameters. When someone calls that subroutine the values are found in the private array called @_. There is more to even that in Perl 6 than in Perl 5 but let's look at the nicer examples of Perl 6:

In Perl 6 one can define real signature for the subroutines and let the language, or rather the implementation, check if the valid set of arguments were passed.

Actually, instead of checking if the right number of arguments were passed Perl will not even call the function if you did not give the correct list of arguments.

I can already hear the people getting worried about the flexibility of passing an arbitrary number of arguments and then letting the function figure it out. Don't worry. You can have that in Perl 6 too.


Prev: Looping over keys of a hash

Table of Contents

Perl 6 Tricks and Treats newsletter

Register to the free newsletter now, and get updates and news.
Email:
Name: