perl6 是否有等同于 MAIN sub 的 class 方法?
Does perl6 have a class method equivalent to the MAIN sub?
或者类似于java的main()方法?换句话说,一种首先执行的方法,有可能从终端读取一个或多个参数。
是的,它被称为MAIN and it has autoparsing for terminal parameters. Futhermore, it can even be a multi sub(支持不同的签名),有默认值,标记为必需并进行类型验证,例如:
#|(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;
}
或者类似于java的main()方法?换句话说,一种首先执行的方法,有可能从终端读取一个或多个参数。
是的,它被称为MAIN and it has autoparsing for terminal parameters. Futhermore, it can even be a multi sub(支持不同的签名),有默认值,标记为必需并进行类型验证,例如:
#|(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;
}