blib/lib/EOL.pm

filter: Statementstmt 85.9% 61 / 71 Branchbran 59.6% 31 / 52 Conditioncond 80.0% 12 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 100% 7 / 7 Podpod 100% 3 / 3 totaltotal 73.0% 114 / 156 CC 35 SCAR 26.7
CC
35
Cov
75%
eol_new_file
17.5
eol_same_file
15.8
eol_return_array
14.2
CRAP 14.5
↑ Summary
1 package EOL;
2  
3 # | PACKAGE | EOL
4 # | AUTHOR | Todd Wylie
5 # | EMAIL | perldev@monkeybytes.org
6 # | ID | $Id: EOL.pm 238 2006-10-31 19:08:15Z twylie $
7  
8 # ---------------------------------------------------------------------------
9 # PURPOSE:
10 # This module aids in the conversion of text file newline characters within
11 # the context of other perl code. There are several choices a user can
12 # specifiy; see POD for details on usage.
13 # ---------------------------------------------------------------------------
14  
15 2 ▶use version; $VERSION = qv('0.0.2');
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # END OF LINE DECLARATIONS
21 2 my $CR = "\015"; # Apple II family, Mac OS thru version 9
22 2 my $CRLF = "\015\012"; # CP/M, MP/M, DOS, Microsoft Windows
23 2 my $LF = "\012"; # Unix, Linux, Xenix, Mac OS X, BeOS, Amiga
24 2 my $FF = "\014"; # printer form feed
25  
26 # --------------------------------------------------------------------------
27 # E O L N E W F I L E (subroutine)
28 # ==========================================================================
29 # USAGE : eol_new_file( in => $in_fh, out => $out_fh, eol => 'LF' );
30 # PURPOSE : Incoming file's newlines converted & saved to new file.
31 # RETURNS : none
32 # PARAMETERS : in => '' # file handle
33 # : out => '' # file handle
34 # : eol => '' # [CR; CRLF; LF]
35 # THROWS : Croaks if required arguments missing/null or unknown.
36 # COMMENTS : By default, all newlines will be converted to standard
37 # : Unix-style line feeds (LF or octal \012). A user may change
38 # : the end-of-file tag manually by supplying the "eol" argument.
39 # : Acceptable eol tags are: CR; CRLF; LF.
40 # SEE ALSO : n/a
41 # --------------------------------------------------------------------------
42 4 ▶sub eol_new_file {
Subroutine eol_new_file: called CC=14 SCAR=28.6
Pod documented
43 4 my %args = @_;
44 4 my $EOL;
45  
46 # Check arguments:
47 4 foreach my $arg (keys %args) {
48 12 ▶ if ( ($arg ne "in") && ($arg ne "out") && ($arg ne "eol") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg ne "in" and $arg ne "out" and $arg ne "eol") 0 12 12
Condition83%5/6
$arg ne "in" and $arg ne "out" and $arg ne "eol"
!ll&&!rl&&r
840
$arg ne "in" and $arg ne "out"
!ll&&!rl&&r
444
MC/DC0%0/3
$arg ne "in" and $arg ne "out" and $arg ne "eol"
$arg ne "in" $arg ne "out" $arg ne "eol"
Truth table75%3/4
A and B and C
A= $arg ne "in"B= $arg ne "out"C= $arg ne "eol"
ABCresult
0XX0
10X0
1100
1111
49 0 croak "EOL reports: not legal arg \"$arg\".";
50 }
51 }
52  
53 # Check for text file input:
54 4 ▶ if (!-T $args{in}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-T $args{"in"}) 0 4 4
55 0 croak "EOL reports: $args{in} is not a text file.";
56 }
57  
58 # Conversion steps:
59 4 ▶ open(IN, "$args{in}") or croak "EOL reports: cannot open $args{in}.";
Branch: 1 branch
BranchTrueFalseTotal
open IN, qq[$args{"in"}] or croak(qq[EOL reports: cannot open $args{"in"}.]) 4 0 4
60 0 ▶ if (-f $args{out}) { unlink($args{out}) }
Branch: 1 branch
BranchTrueFalseTotal
if (-f $args{"out"}) 0 4 4
61 4 ▶ open(OUT, ">$args{out}") or croak "EOL reports: cannot open $args{in}.";
Branch: 1 branch
BranchTrueFalseTotal
open OUT, qq[>$args{"out"}] or croak(qq[EOL reports: cannot open $args{"in"}.]) 4 0 4
62 4 ▶ if ($args{eol}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'eol'}) { } 4 0 4
63 ($args{eol} eq "CR") ? ($EOL = $CR )
64 : ($args{eol} eq "CRLF") ? ($EOL = $CRLF)
65 4 ▶ : ($args{eol} eq "LF" ) ? ($EOL = $LF )
Branch: 3 branches
BranchTrueFalseTotal
$args{'eol'} eq 'CR' ? : 1 3 4
$args{'eol'} eq 'CRLF' ? : 2 1 3
$args{'eol'} eq 'LF' ? : 1 0 1
66 : croak "EOL reports: not legal EOL tag! Use only: CR; CRLF; LF.";
67 }
68 else {
69 0 $EOL = $LF; # Default is Unix line feed.
70 }
71 4 ▶ while(<IN>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline IN)) 49672 4 49676
72 49672 s/$CRLF$|$CR|$FF$|$LF$/$EOL/g;
73 49672 print OUT $_;
74 }
75 4 close(IN);
76 4 close(OUT);
77  
78 4 return(1);
79 }
80  
81 # --------------------------------------------------------------------------
82 # E O L S A M E F I L E (subroutine)
83 # ==========================================================================
84 # USAGE : eol_same_file( in => $in_fh, eol => 'LF', backup => '.bak' );
85 # PURPOSE : Incoming file's newlines converted & overwrites self.
86 # RETURNS : none
87 # PARAMETERS : in => '' # file handle
88 # : eol => '' # [CR; CRLF; LF]
89 # : backup => '' # scalar
90 # THROWS : Croaks if required arguments missing/null or unknown.
91 # COMMENTS : By default, all newlines will be converted to standard
92 # : Unix-style line feeds (LF or octal \012). A user may change
93 # : the end-of-file tag manually by supplying the "eol" argument.
94 # : Acceptable eol tags are: CR; CRLF; LF. As already mentioned,
95 # : this routine will clobber the in-file. Sending the "backup"
96 # : argument with a file suffix will produce a one-time backup of
97 # : the original file. !!! USE AT OWN RISK !!!
98 # SEE ALSO : n/a
99 # --------------------------------------------------------------------------
100 1 ▶sub eol_same_file {
Subroutine eol_same_file: called CC=12 SCAR=27.6
Pod documented
101 1 my %args = @_;
102 1 my $EOL;
103  
104 # Check arguments:
105 1 foreach my $arg (keys %args) {
106 3 ▶ if ( ($arg ne "in") && ($arg ne "backup") && ($arg ne "eol") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg ne "in" and $arg ne "backup" and $arg ne "eol") 0 3 3
Condition83%5/6
$arg ne "in" and $arg ne "backup" and $arg ne "eol"
!ll&&!rl&&r
210
$arg ne "in" and $arg ne "backup"
!ll&&!rl&&r
111
MC/DC0%0/3
$arg ne "in" and $arg ne "backup" and $arg ne "eol"
$arg ne "in" $arg ne "backup" $arg ne "eol"
Truth table75%3/4
A and B and C
A= $arg ne "in"B= $arg ne "backup"C= $arg ne "eol"
ABCresult
0XX0
10X0
1100
1111
107 0 croak "EOL reports: not legal arg \"$arg\".";
108 }
109 }
110  
111 # Check for text file input:
112 1 ▶ if (!-T $args{in}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-T $args{"in"}) 0 1 1
113 0 croak "EOL reports: $args{in} is not a text file.";
114 }
115  
116 # Conversion steps:
117 1 ▶ if ($args{eol}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'eol'}) { } 1 0 1
118 ($args{eol} eq "CR") ? ($EOL = $CR )
119 : ($args{eol} eq "CRLF") ? ($EOL = $CRLF)
120 1 ▶ : ($args{eol} eq "LF" ) ? ($EOL = $LF )
Branch: 3 branches
BranchTrueFalseTotal
$args{'eol'} eq 'CR' ? : 0 1 1
$args{'eol'} eq 'CRLF' ? : 0 1 1
$args{'eol'} eq 'LF' ? : 1 0 1
121 : croak "EOL reports: Not legal EOL tag! Use only: CR; CRLF; LF.";
122 }
123 else {
124 0 $EOL = $LF; # Default is Unix line feed.
125 }
126 1 ▶ (exists $args{backup}) ? ($^I = $args{backup}) : ($^I = "");
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'backup'} ? : 1 0 1
127 1 ▶ while(<>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline ARGV)) 16557 1 16558
128 16557 s/$CRLF$|$CR|$FF$|$LF$/$EOL/g;
129 16557 print $_;
130 }
131  
132 1 return(1);
133 }
134  
135 # --------------------------------------------------------------------------
136 # E O L R E T U R N A R R A Y (subroutine)
137 # ==========================================================================
138 # USAGE : eol_return_array( in => $in_fh, eol => 'LF' );
139 # PURPOSE : Incoming file's newlines converted & array of lines returned.
140 # RETURNS : array-reference
141 # PARAMETERS : in => '' # file handle
142 # : eol => '' # [CR; CRLF; LF]
143 # THROWS : Croaks if required arguments missing/null or unknown.
144 # COMMENTS : By default, all newlines will be converted to standard
145 # : Unix-style line feeds (LF or octal \012). A user may change
146 # : the end-of-file tag manually by supplying the "eol" argument.
147 # : Acceptable eol tags are: CR; CRLF; LF. An array reference to
148 # : the converted file lines will be returned. Obviously, for
149 # : larger files, this may be a considerable drain on memory.
150 # SEE ALSO : n/a
151 # --------------------------------------------------------------------------
152 1 ▶sub eol_return_array {
Subroutine eol_return_array: called CC=11 SCAR=26.5
Pod documented
153 1 my %args = @_;
154 1 my $EOL;
155  
156 # Check arguments:
157 1 foreach my $arg (keys %args) {
158 2 ▶ if ( ($arg ne "in") && ($arg ne "eol") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg ne "in" and $arg ne "eol") 0 2 2
Condition66%2/3
$arg ne "in" and $arg ne "eol"
!ll&&!rl&&r
110
MC/DC0%0/2
$arg ne "in" and $arg ne "eol"
$arg ne "in" $arg ne "eol"
Truth table66%2/3
A and B
A= $arg ne "in"B= $arg ne "eol"
ABresult
0X0
100
111
159 0 croak "EOL reports: not legal arg \"$arg\".";
160 }
161 }
162  
163 # Check for text file input:
164 1 ▶ if (!-T $args{in}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-T $args{"in"}) 0 1 1
165 0 croak "EOL reports: $args{in} is not a text file.";
166 }
167  
168 # Conversion steps:
169 1 my @lines;
170 1 ▶ open(IN, "$args{in}") or croak "EOL reports: cannot open $args{in}";
Branch: 1 branch
BranchTrueFalseTotal
open IN, qq[$args{"in"}] or croak(qq[EOL reports: cannot open $args{"in"}]) 1 0 1
171 1 ▶ if ($args{eol}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'eol'}) { } 1 0 1
172 ($args{eol} eq "CR") ? ($EOL = $CR )
173 : ($args{eol} eq "CRLF") ? ($EOL = $CRLF)
174 1 ▶ : ($args{eol} eq "LF" ) ? ($EOL = $LF )
Branch: 3 branches
BranchTrueFalseTotal
$args{'eol'} eq 'CR' ? : 0 1 1
$args{'eol'} eq 'CRLF' ? : 0 1 1
$args{'eol'} eq 'LF' ? : 1 0 1
175 : croak "EOL reports: not legal EOL tag! Use only: CR; CRLF; LF.\n\n";
176 }
177 else {
178 0 $EOL = $LF; # Default is Unix line feed.
179 }
180 1 ▶ while(<IN>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline IN)) 16557 1 16558
181 16557 my $line = $_;
182 16557 $line =~ s/$CRLF$|$CR|$FF$|$LF$/$EOL/g;
183 16557 push (@lines, split (/($EOL)/, $line));
184 }
185 1 close(IN);
186  
187 1 return(\@lines);
188 }
189  
190 2 1; # End of module.
191  
192 __END__
↑ Summary