use strict; use warnings; use Getopt::Long; my $usage_msg = <<'EOHELP'; greeter is a sample program for Argdom.pm. --help | -h displays this help message and quits. -o outfile is the file to which the output is written. Can be many, as in -o f1 -o f2 -o f3. `-' means the STDOUT (default). -xclaim means the output should be exclamatory. -4 X;Y;Z means the greeting should be for X, Y, and Z. Default is ``World'' and yourself!. any other args are the greeting. Default is "Hello"./; EOHELP my $xclaim = 0; my $usage = 0; my $greeting = ''; my $forWhom = 'World;to you'; my @outs; GetOptions ("h|help" => \$usage, "xclaim!" => \$xclaim, "4=s" => \$forWhom, "o=s" => \@outs, ) or die $usage_msg; if ($usage) { print $usage_msg; exit; } $greeting .= $_ foreach @ARGV; $greeting = 'Hello' unless $greeting; @outs = qw/-/ unless @outs; my $output = ''; $output .= "$greeting, $_" . ($xclaim ? '!' : '.') . "\n" for (split /;/, $forWhom); foreach my $outfile (@outs) { if ($outfile eq '-') { print $output; } else { open my $outfh, '>', $outfile or warn "Could not open $outfile for writing: $!"; print $outfh $output; close $outfh; } }