Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does perl6 have a class method equivalent to the MAIN sub?

Tags:

raku

Or similar to java's main() method? In other words a method that executes first, with the possibility of reading one or more parameters from the terminal.

like image 259
Mikkel Avatar asked May 11 '18 12:05

Mikkel


1 Answers

Yes, and it's called MAIN and it has autoparsing for terminal parameters. Futhermore, it can even be a multi sub (supporting different signatures), have defaults, mark as required and do type validation, e.g.:

#|(optional description for USAGE message) 
sub MAIN( Int :$length = 24,
           :file($data) where { .IO.f // die "file not found in $*CWD" } = 'file.dat',
           Bool :v(:$verbose) #`( -verbose, --verbose, -v or --v ) )
{
    say $length if $length.defined;
    say $data   if $data.defined;
    say 'Verbosity ', ($verbose ?? 'on' !! 'off');

    exit 1;
}
like image 183
nxadm Avatar answered Oct 24 '22 03:10

nxadm