Perl6 modules in Rakudo baby-steps, part 1

Here is an example of what a module can look like. In this case it only has a simple method that greets the user:

class Greeter;

method greet($name = 'world') {
say "hello $name";

Now to use the module we can write something like:

BEGIN { @*INC.push('Greeter/lib') }

use Greeter;
my $x =;

The only tricky part is actually the push in the BEGIN block, to add your lib directory. Of course running this is as simple as:

$ ./perl6 greeter.p6 
hello rakudo


For the record, you can also use the PERL6LIB environment variable on the outside of the program, just like PERL5LIB in p5.

I know this might be a bit far fetching - but for translating my WebNano web framework to Perl6 I would need a way to do the following:

  1. Check if a module is loaded
  2. If yes - call a class method on it
  3. If no try to load it
  4. If it loads correctly go to 2.
  5. If not check the error
  6. If the error was no such file - then emit 404
  7. Else emit 500

I know I am confusing here module and class - and that both nouns have different meaning in Perl6 - but I hope my intent is clear. How would you translate that to Perl6?

Hmm - sorry. I now see that my question comes out as a rude 'do it for me' demand. Please don't look at it like that - it was just the first thing I was thinking about when reading your post. Now I think it could be better formulated as following questions:

  1. Is there a way to check if a class is defined?
  2. What is really the difference between module and class in Perl6? Is it just a convention that a module contains the Greeter class?
  3. Is there a 'require' (i.e. dynamic module loader) function in Perl6 and if yes how do I check it return codes/error conditions? Are there different error conditions for the cases of 'no file found' and 'file does not compile'?


Leave a comment

About smash

user-pic I blog about Perl.