#!/usr/bin/perl
use strict;
use warnings;
unless ($#ARGV == 1) {
Huh?!? Not that this is technically incorrect, but is there any good
reason for not using '@ARGV == 2' instead? (Which IMHO conveys more
naturally the idea that the number of required parameters is two.)
usage();
exit;
} [...]
sub usage {
print <<EOUSAGE
Usage:
$0 <pattern> <text>
EOUSAGE
}
Also, I understand that you're being more verbose than would seem
natural for this simple because you're probably used to write "bigger"
applications, but since it would be better, still IMHO, to die()
anyway, then I'd write:
die <<"EOUSAGE" unless @ARGV == 2;
Usage:
$0 <pattern> <text>
EOUSAGE
Also, you can still adopt a "hybrid" approach writing a usage() sub
to *return* the usage string or "save" that to a $usage scalar in case
you need it in multiple places of your script, e.g.:
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Std;
sub usage(); # Or (maybe better!) 'use constant'...
my %opts;
getopts 'h', \%opts;
print usage and exit if $opts{h};
die usage unless @ARGV == 2;
# ACTUAL PROGRAM...
sub usage () {
<<"EOUSAGE"
Usage: $0 [options] <pattern> <text>
Currently the only option is -h, that
will print this help and exit immediately.
EOUSAGE
}
__END__
BTW: I didn't use it here, but Getopt::Std has its own --help managing
facility, that often come handy and can complement an approach like
that above...
Michele