-
Notifications
You must be signed in to change notification settings - Fork 10
/
template.perl
executable file
·88 lines (72 loc) · 1.66 KB
/
template.perl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/perl
use strict;
use warnings;
use English qw( -no_match_vars );
use File::Basename;
use Getopt::Long;
# use Carp;
# use File::Temp qw( tempfile );
# use File::Spec;
# use POSIX qw ( strftime );
# use Pod::Usage;
my $PROG = basename $PROGRAM_NAME;
my $Error;
sub main {
my $fh;
my $line;
# Parse the command line
(? ? ?) = assertParseArgs();
if (! open($fh, "<", ...)) {
printf STDERR "%s: unable to open file '%s': %s\n",
$PROG, ..., lcfirst($OS_ERROR);
exit 1;
}
while ($line = <$fh>) {
chomp $line;
}
close $fh;
} # main
sub assertParseArgs {
my %options;
my $result;
my $save;
# Trap warnings so we can handle them ourselves
$save = $SIG{__WARN__};
$SIG{__WARN__} = \&trap;
# Let GetOptions() do the actual parsing
$result = GetOptions(
\%options,
"int|i=i",
"float|f=f",
"string|s=s",
"optional|o:_",
#
"man" => sub { pod2usage(-exitstatus => 0, -verbose => 2) },
"help" => sub { pod2usage(-exitstatus => 0, -verbose => 2) },
);
if (! $result) {
printf STDERR "%s: %s\n\n", $PROG, lcfirst($Error);
pod2usage(-exitstatus => 2, -verbose => 0);
}
# Restore warnings
$SIG{__WARN__} = $save;
###
### Validation goes here
###
if (! defined($options{"string"})) {
printf STDERR "%s: no string specified (-s | --string)\n\n", $PROG;
pod2usage(-exitstatus => 2, -verbose => 0);
}
return (
$options{'int'},
$options{'float'},
$options{'string'},
$options{'optional'},
);
} # assertParseArgs
sub trap {
($Error) = @_; # *NOT* my $Error -- this is the global variable!
chomp $Error;
return;
} # trap
main();