Skip to content

Commit 39ef5bb

Browse files
committed
Remove old MAIN documentation from functions
And replace it by a stub referring to the new CLI page
1 parent 085dc97 commit 39ef5bb

File tree

1 file changed

+2
-122
lines changed

1 file changed

+2
-122
lines changed

doc/Language/functions.pod6

Lines changed: 2 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,130 +1113,10 @@ for (2,4) X (1,2) -> ($a,$b) {
11131113
In this case, we are coercing an C<Int> to a C<Bool>, which is then printed (put
11141114
into a string context) in the C<for> loop that calls the function.
11151115
1116-
X<|MAIN>X<|command line arguments>
11171116
=head1 sub MAIN
11181117
1119-
The sub with the special name C<MAIN> is executed after all relevant phasers.
1120-
Its signature is the means by which command line arguments can be parsed. Multi
1121-
methods are supported and a usage method is automatically generated and
1122-
displayed if no command line arguments are provided. All command line arguments
1123-
are also available in L<C<@*ARGS>|/language/variables#Dynamic_variables>, which
1124-
can be mutated before being processed by C<MAIN>.
1125-
1126-
Unlike other ordinary functions, any return value provided by C<MAIN> will be
1127-
ignored by the invoker, even if explicitly set by means of a C<return> statement
1128-
(that will terminate the C<MAIN> function). The default return code of C<MAIN> is always
1129-
zero (C<0>, success).
1130-
In order to provide any return value different from zero, a call
1131-
to L<exit|https://docs.perl6.org/routine/exit> has to be performed.
1132-
1133-
#|(optional description for USAGE message)
1134-
sub MAIN( Int :$length = 24,
1135-
:file($data) where { .IO.f // die "file not found in $*CWD" } = 'file.dat',
1136-
Bool :v(:$verbose) #`( -verbose, --verbose, -v or --v ) )
1137-
{
1138-
say $length if $length.defined;
1139-
say $data if $data.defined;
1140-
say 'Verbosity ', ($verbose ?? 'on' !! 'off');
1141-
1142-
exit 1;
1143-
}
1144-
1145-
This C<MAIN> is defining two kind of aliases, as explained in
1146-
L<Signatures|/type/Signature#Positional_vs._named_arguments>: C<:file($data)> aliases the
1147-
content passed to the command-line parameter C<--file=> to the variable
1148-
C<$data>; C<:v(:$verbose)> not only aliases C<v> to C<verbose>, but also creates
1149-
a new command line parameter C<verbose> thanks to the specification of the C<:>.
1150-
In fact, since this is an alias, both C<verbose> and C<v> can use single or
1151-
double dashes (C<-> or C<-->).
1152-
1153-
With C<file.dat> present, this will work this way
1154-
=begin code :lang<shell>
1155-
$ perl6 Main.p6
1156-
24
1157-
file.dat
1158-
Verbosity off
1159-
=end code
1160-
1161-
Or this way with C<-v> or C<--verbose>
1162-
1163-
=begin code :lang<shell>
1164-
$ perl6 Main.p6 -v
1165-
24
1166-
file.dat
1167-
Verbosity on
1168-
=end code
1169-
1170-
1171-
=head2 C<%*SUB-MAIN-OPTS>
1172-
1173-
It's possible to alter how arguments are processed before they're passed
1174-
to C<sub MAIN {}> by setting options in C<%*SUB-MAIN-OPTS> hash. Due to the
1175-
nature of dynamic variables, it is required to set up C<%*SUB-MAIN-OPTS>
1176-
hash and fill it with the appropriate settings. For instance:
1177-
1178-
my %*SUB-MAIN-OPTS =
1179-
:named-anywhere, # allow named variables at any location
1180-
:!foo, # don't allow foo
1181-
;
1182-
sub MAIN ($a, $b, :$c, :$d) {
1183-
say "Accepted!"
1184-
}
1185-
1186-
Available options are:
1187-
1188-
=head3 C<named-anywhere>
1189-
1190-
By default, named arguments passed to the program (i.e., C<MAIN>)
1191-
cannot appear after any positional argument.
1192-
However, if C«%*SUB-MAIN-OPTS<named-anywhere>» is
1193-
set to a true value, named arguments can be specified anywhere, even after
1194-
positional parameter. For example, the above program can be called with:
1195-
1196-
=begin code :lang<shell>
1197-
perl6 example.p6 1 --c=2 3 --d=4
1198-
=end code
1199-
1200-
=head2 X<Unit-scoped definition of MAIN|declarator,unit (MAIN)>
1201-
1202-
If the entire program body resides within C<MAIN>, you can use the C<unit>
1203-
declarator as follows:
1204-
1205-
=begin code :skip-test<unit>
1206-
unit sub MAIN( Int :$length = 24,
1207-
:file($data) where { .IO.f // die "file not found in $*CWD" } = 'file.dat',
1208-
Bool :v(:$verbose) #`( -verbose, --verbose, -v or --v ) );
1209-
1210-
# rest of script is part of MAIN
1211-
=end code
1212-
1213-
Note that this is only appropriate if you do not need a C<proto> or C<multi> definition.
1214-
1215-
X<|USAGE>X<|$*USAGE>
1216-
=head1 sub C<USAGE>
1217-
1218-
If no multi candidate of C<MAIN> is found for the given command line
1219-
parameters, the sub C<USAGE> is called. If no such method is found,
1220-
the compiler will output a default generated usage message.
1221-
1222-
#|(is it the answer)
1223-
multi MAIN(Int $i) { say $i == 42 ?? 'answer' !! 'dunno' }
1224-
#|(divide two numbers)
1225-
multi MAIN($a, $b){ say $a/$b }
1226-
1227-
sub USAGE() {
1228-
print Q:c:to/EOH/;
1229-
Usage: {$*PROGRAM-NAME} [number]
1230-
1231-
Prints the answer or 'dunno'.
1232-
EOH
1233-
}
1234-
1235-
The default usage message is available inside C<sub USAGE> via read-only
1236-
C<$*USAGE> variable. It will be generated based on available C<sub MAIN>
1237-
candidates and their parameters. You can specify additional extended
1238-
description for each candidate using C<#|(...)> Pod block to set
1239-
L«C<WHY>|/routine/WHY».
1118+
There is no C<sub MAIN> in Perl 6, but you can provide one to create a
1119+
L<command line interface|create-cli> for your script.
12401120
12411121
=end pod
12421122

0 commit comments

Comments
 (0)