| 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 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 5 | ▶ | use File::Basename qw(basename); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 5 | ▶ | use Getopt::Std qw(getopts); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | 5 | ▶ | use constant EX_SUCCESS => 0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 5 | ▶ | use constant EX_FAILURE => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | |||||||||||
| 22 | 5 | my $Program = basename($0); | |||||||||
| 23 | |||||||||||
| 24 | # unbuffer output to make it look speedier | ||||||||||
| 25 | 5 | $|++; | |||||||||
| 26 | |||||||||||
| 27 | 5 | our $VERSION = '1.5'; | |||||||||
| 28 | |||||||||||
| 29 | 5 | ▶ | getopts('') or usage(); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 2 | my $rc = EX_SUCCESS; | |||||||||
| 31 | 2 | foreach my $file (@ARGV) { | |||||||||
| 32 | 1 | ▶ | if (-d $file) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 0 | warn "$Program: '$file' is a directory\n"; | |||||||||
| 34 | 0 | $rc = EX_FAILURE; | |||||||||
| 35 | 0 | next; | |||||||||
| 36 | } | ||||||||||
| 37 | 1 | my $fh; | |||||||||
| 38 | 1 | ▶ | unless (open $fh, '<', $file) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 0 | warn "$Program: cannot open '$file': $!\n"; | |||||||||
| 40 | 0 | $rc = EX_FAILURE; | |||||||||
| 41 | 0 | next; | |||||||||
| 42 | } | ||||||||||
| 43 | 1 | rev($fh); | |||||||||
| 44 | 1 | ▶ | if ($!) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 0 | warn "$Program: '$file': $!\n"; | |||||||||
| 46 | 0 | $rc = EX_FAILURE; | |||||||||
| 47 | } | ||||||||||
| 48 | 1 | ▶ | unless (close $fh) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 0 | warn "$Program: cannot close '$file': $!\n"; | |||||||||
| 50 | 0 | $rc = EX_FAILURE; | |||||||||
| 51 | 0 | next; | |||||||||
| 52 | } | ||||||||||
| 53 | } | ||||||||||
| 54 | 2 | ▶ | rev(*STDIN) unless @ARGV; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | 2 | exit $rc; | |||||||||
| 56 | |||||||||||
| 57 | 2 | ▶ | sub rev { | ||||||||
|
Subroutine
rev: called CC=2 SCAR=6.9
| |||||||||||
| 58 | 2 | my $fh = shift; | |||||||||
| 59 | 2 | ▶ | while (<$fh>) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 7 | chomp; | |||||||||
| 61 | 7 | my $r = reverse; | |||||||||
| 62 | 7 | print $r, $/; | |||||||||
| 63 | } | ||||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | 2 | ▶ | sub usage { | ||||||||
|
Subroutine
usage: called CC=1 SCAR=0.0
| |||||||||||
| 67 | 2 | print "usage: $Program [file ...]\n"; | |||||||||
| 68 | 2 | exit EX_FAILURE; | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | 1 | ▶ | sub VERSION_MESSAGE { | ||||||||
|
Subroutine
VERSION_MESSAGE: called CC=1 SCAR=0.0
| |||||||||||
| 72 | 1 | print "$Program version $VERSION\n"; | |||||||||
| 73 | 1 | exit EX_SUCCESS; | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | __END__ | ||||||||||