Ok. So, I'm stupid about using the extra map. I'm also stupid about using the wrong invocation of the function. I'm so used to method calls which don't need the & or the () that I forgot which is supposed to be used when calling a 'normal' function. :-)
I find that nesting logical assertions helps the reader understand what a string of maps and greps and sorts is doing. Yes, in theory, the programmer is supposed to use intermediate variables, but no other language has the same piping that Perl has, so Code Complete never addressed the issue. I've seen merlyn and theDamian both use a similar form to do the Orcish manauver or Schwartzian transforms. *shrugs*
As for mandatory args ... *sighs* This is similar to another problem I have that I posted somewhere, but don't have the motivation to find.
*ponders* Wouldn't something like the following work?
sub printUsage {
my ($mandatory, $optional) = @_;
print "Usage: $0\n";
print "-$_ ", uc $_, " " for @$mandatory;
print $/;
print "[-$_ ", uc $_, "]" for @$optional;
print $/;
}
Of course, the formatting isn't as good. I'll leave that as an exercise for the reader. *grins*
Update: I changed how I did the calls into Getopt::Long. Please critique.
my @mandatory = qw(user to_db to_sid to_site);
my @optional = qw(password vendor type model);
my @default = qw(from_db from_sid);
our $from_db = "some_db";
our $from_sid = "some_sid";
my $args = {};
my $rc;
{
no strict 'refs';
$rc = GetOptions($args,
(map { "${_}=s" } @mandatory),
(map { "${_}:s" } @optional),
(map { ("${_}=s", \${"$_"}) } @default),
);
}
printUsage(\@mandatory, \@optional, \@default)
unless $rc;
printUsage(\@mandatory, \@optional, \@default)
if grep !exists $args->{$_}, @mandatory;
------ We are the carpenters and bricklayers of the Information Age. Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement. |