@@ -14,6 +14,7 @@ License: gpl
14
14
use strict;
15
15
16
16
use File::Basename qw( basename) ;
17
+ use Getopt::Std qw( getopts) ;
17
18
18
19
use constant EX_SUCCESS => 0;
19
20
use constant EX_FAILURE => 1;
@@ -25,48 +26,33 @@ $|++;
25
26
26
27
my ($VERSION ) = ' 1.4' ;
27
28
28
- my $rc = EX_SUCCESS;
29
- if (scalar (@ARGV ) == 0 || $ARGV [0] !~ m / ^-./ ) {
30
- if (@ARGV ) {
31
- foreach my $file (@ARGV ) {
32
- if (-d $file ) {
33
- warn " $Program : '$file ' is a directory\n " ;
34
- $rc = EX_FAILURE;
35
- next ;
36
- }
37
- my $fh ;
38
- unless (open $fh , ' <' , $file ) {
39
- warn " $Program : cannot open '$file ': $! \n " ;
40
- $rc = EX_FAILURE;
41
- next ;
42
- }
43
- rev($fh );
44
- if ($! ) {
45
- warn " $Program : '$file ': $! \n " ;
46
- $rc = EX_FAILURE;
47
- }
48
- }
49
- } else {
50
- rev(*STDIN );
51
- }
29
+ my %opt ;
30
+ getopts(' v' , \%opt ) or usage();
31
+ if ($opt {' v' }) {
32
+ print " $Program $VERSION \n " ;
33
+ exit EX_SUCCESS;
52
34
}
53
- elsif ($ARGV [0] eq ' --version' ) {
54
- print " $Program $VERSION \n " ;
55
- }
56
- else {
57
- print <<EOF ;
58
- Usage: $Program [OPTION] [FILE]
59
-
60
- Reverses lines of the named file or the text input on STDIN
61
-
62
- Options:
63
- --version: Print version number, then exit.
64
- --help || -h: Print usage, then exit;
65
35
66
- EOF
67
- exit EX_FAILURE;
36
+ my $rc = EX_SUCCESS;
37
+ foreach my $file (@ARGV ) {
38
+ if (-d $file ) {
39
+ warn " $Program : '$file ' is a directory\n " ;
40
+ $rc = EX_FAILURE;
41
+ next ;
42
+ }
43
+ my $fh ;
44
+ unless (open $fh , ' <' , $file ) {
45
+ warn " $Program : cannot open '$file ': $! \n " ;
46
+ $rc = EX_FAILURE;
47
+ next ;
48
+ }
49
+ rev($fh );
50
+ if ($! ) {
51
+ warn " $Program : '$file ': $! \n " ;
52
+ $rc = EX_FAILURE;
53
+ }
68
54
}
69
-
55
+ rev(* STDIN ) unless @ARGV ;
70
56
exit $rc ;
71
57
72
58
sub rev {
@@ -78,6 +64,19 @@ sub rev {
78
64
}
79
65
}
80
66
67
+ sub usage {
68
+ print <<EOF ;
69
+ Usage: $Program [OPTION] [FILE]...
70
+
71
+ Reverse lines of the named file(s) or the text input on STDIN
72
+
73
+ Options:
74
+ -v Print version number, then exit.
75
+
76
+ EOF
77
+ exit EX_FAILURE;
78
+ }
79
+
81
80
__END__
82
81
83
82
=pod
@@ -88,7 +87,7 @@ rev - reverse lines of a file
88
87
89
88
=head1 SYNOPSIS
90
89
91
- rev [options ] [file]
90
+ rev [-v ] [file]...
92
91
93
92
=head1 DESCRIPTION
94
93
@@ -102,13 +101,9 @@ I<rev> accepts the following options:
102
101
103
102
=over 4
104
103
105
- =item --help || -h
106
-
107
- Print a short help message, then exits.
108
-
109
- =item --version
104
+ =item -v
110
105
111
- Prints out its version number, then exits.
106
+ Print version number then exit
112
107
113
108
=back
114
109
0 commit comments