File Coverage

bin/rev
Criterion Covered Total %
statement 38 49 77.5
branch 8 12 66.6
condition n/a
subroutine 8 8 100.0
pod n/a
total 54 69 78.2


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             =begin metadata
4              
5             Name: rev
6             Description: reverse lines of a file
7             Author: Andy Murren, andy@murren.org
8             License: gpl
9              
10             =end metadata
11              
12             =cut
13              
14 5     5   23824 use strict;
  5         9  
  5         238  
15              
16 5     5   24 use File::Basename qw(basename);
  5         6  
  5         535  
17 5     5   2336 use Getopt::Std qw(getopts);
  5         11697  
  5         370  
18              
19 5     5   33 use constant EX_SUCCESS => 0;
  5         9  
  5         501  
20 5     5   62 use constant EX_FAILURE => 1;
  5         10  
  5         8745  
21              
22 5         886235 my $Program = basename($0);
23              
24             # unbuffer output to make it look speedier
25 5         38 $|++;
26              
27 5         15 our $VERSION = '1.5';
28              
29 5 100       36 getopts('') or usage();
30 2         132 my $rc = EX_SUCCESS;
31 2         6 foreach my $file (@ARGV) {
32 1 50       63 if (-d $file) {
33 0         0 warn "$Program: '$file' is a directory\n";
34 0         0 $rc = EX_FAILURE;
35 0         0 next;
36             }
37 1         2 my $fh;
38 1 50       74 unless (open $fh, '<', $file) {
39 0         0 warn "$Program: cannot open '$file': $!\n";
40 0         0 $rc = EX_FAILURE;
41 0         0 next;
42             }
43 1         7 rev($fh);
44 1 50       5 if ($!) {
45 0         0 warn "$Program: '$file': $!\n";
46 0         0 $rc = EX_FAILURE;
47             }
48 1 50       22 unless (close $fh) {
49 0         0 warn "$Program: cannot close '$file': $!\n";
50 0         0 $rc = EX_FAILURE;
51 0         0 next;
52             }
53             }
54 2 100       14 rev(*STDIN) unless @ARGV;
55 2         0 exit $rc;
56              
57             sub rev {
58 2     2   8 my $fh = shift;
59 2         90 while (<$fh>) {
60 7         18 chomp;
61 7         16 my $r = reverse;
62 7         285 print $r, $/;
63             }
64             }
65              
66             sub usage {
67 2     2   382 print "usage: $Program [file ...]\n";
68 2         0 exit EX_FAILURE;
69             }
70              
71             sub VERSION_MESSAGE {
72 1     1   120 print "$Program version $VERSION\n";
73 1           exit EX_SUCCESS;
74             }
75              
76             __END__