By using the following method, you can handle various string arguments regardless of their order.
#!/usr/bin/perl
# sample.pl
map {
if (/^-a$/) {
print $_, "\n";
}
elsif (/^--(xyz)$/) {
print uc($1), "!!\n";
}
else {
print "[", $_, "] is ignored.\n"
}
} @ARGV;
@ARGV
is a Perl-specific array that automatically stores command-line arguments.map
is a function that can process each element of an array (list).
Since regular expressions can be used in the conditional clause of an if
statement, it enables detailed pattern matching.
# Example of execution on Linux
$ chmod +x sample.pl
$ ./sample.pl test --xyz -a -b 100
[test] is ignored.
XYZ!!
-a
[-b] is ignored.
[100] is ignored.
The intended arguments (-a
, –-xyz
) were processed correctly.
コメント