I want to make a modulino (a file that can run as either a module or a script) in Perl6.
The following code "processes" file names from the command line:
sub MAIN ( *@filenames )
{
for @filenames -> $filename
{
say "Processing $filename";
}
}
Saving this as main.pm6
I can run it and it works:
perl6 main.pm6 hello.txt world.txt
Processing 'hello.txt'
Processing 'world.txt'
So, I want this to be a module so that I can add functions and make testing it easier. However, when I add a module
declaration to it, it no longer outputs anything:
module main;
sub MAIN ( *@filenames )
{
for @filenames -> $filename
{
say "Processing '$filename'";
}
}
Which results in nothing output:
perl6 main.pm6 hello.txt world.txt
So, how can I build a modulino in Perl6?
I'm using Perl6 running on MoarVM from the January 2015 release of Rakudo Star.
UPDATE:
When I try wrapping the module in braces:
module main
{
sub process (@filenames) is export
{
for @filenames -> $filename
{
say "Processing '$filename'";
}
}
};
sub MAIN ( *@filenames )
{
process(@filenames)
}
I also get errors:
===SORRY!=== Error while compiling main.pm6
Undeclared routine:
process used at line 14. Did you mean 'proceed'?
module main { ... }; sub MAIN { ... }
– Lemniscateprocess
outside the module, make itour
-scoped and call is asmain::process
or export it and add animport
statement to the body of your subMAIN
– Lemniscate