Common.pm

filter: Statementstmt 35.4% 17 / 48 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 41.6% 5 / 12 Podpod 83.3% 5 / 6 totaltotal 30.6% 27 / 88 CC 11 SCAR 27.2
CC
11
Cov
25%
readfile
42.0
comma
12.0
trim
6.0
CRAP 15.1
« Config.pm ↑ Summary Jumptable.pm »
1 # BW::Common.pm
2 # Common methods for BW::* modules
3 #
4 # by Bill Weinman - http://bw.org/
5 # Copyright (c) 1995-2010 The BearHeart Group, LLC
6 #
7 # See POD for History
8  
9 package BW::Common;
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use BW::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use base qw( BW::Base );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 our $VERSION = "0.5";
18  
19 sub _init
20 0 ▶{
Subroutine _init: not called CC=1 SCAR=6.9
21 0 my $self = shift;
22 0 $self->SUPER::_init(@_);
23  
24 0 return SUCCESS;
25 }
26  
27 # _setter_getter entry points
28 0 ▶sub stub { BW::Base::_setter_getter(@_); }
Subroutine stub: not called CC=1 SCAR=6.9
Pod undocumented
29  
30 # read a file up to length bytes (defaults to 1MB)
31 # binmode defaults to ":raw"
32 sub readfile
33 0 ▶{
Subroutine readfile: not called CC=6 SCAR=37.4
Pod documented
34 0 my $fn = shift;
35 0 ▶ $fn = shift if ref($fn); # accept object or non-object calls
Branch: 1 branch
BranchTrueFalseTotal
if ref $fn 0 0 0
36 0 ▶ my $length = shift || ( 1024 * 1024 );
Condition0%0/2
shift() || 1048576
l!l
00
MC/DC0%0/1
shift() || 1048576
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
37 0 ▶ my $layer = shift || ":raw";
Condition0%0/2
shift() || ':raw'
l!l
00
MC/DC0%0/1
shift() || ':raw'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
38 0 my $buf;
39  
40 0 ▶ my $fh = IO::File->new( $fn, 'r' ) or return FAILURE;
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($fn, "r") or return "" 0 0 0
41 0 $fh->binmode($layer);
42 0 my $rc = $fh->read( $buf, $length );
43 0 ▶ return FAILURE unless defined $rc;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rc 0 0 0
44 0 $fh->close;
45  
46 0 return $buf;
47 }
48  
49 sub comma
50 0 ▶{
Subroutine comma: not called CC=3 SCAR=24.8
Pod documented
51 0 my $n = shift;
52 0 ▶ $n = shift if ref($n); # accept object or non-object calls
Branch: 1 branch
BranchTrueFalseTotal
if ref $n 0 0 0
53  
54 0 ▶ while ( $n =~ s/^(-?\d+)(\d{3})/$1,$2/ ) {}
Branch: 1 branch
BranchTrueFalseTotal
if ($n =~ s/^(-?\d+)(\d{3})/$1,$2/) 0 0 0
55 0 return $n;
56 }
57  
58 sub trim
59 0 ▶{
Subroutine trim: not called CC=2 SCAR=17.9
Pod documented
60 0 my $s = shift;
61 0 ▶ $s = shift if ref($s); # accept object or non-object calls
Branch: 1 branch
BranchTrueFalseTotal
if ref $s 0 0 0
62  
63 0 $s =~ /^\s*(.*?)\s*$/;
64 0 return $1;
65 }
66  
67 sub basename
68 0 ▶{
Subroutine basename: not called CC=2 SCAR=17.9
Pod documented
69 0 my $s = shift;
70 0 ▶ $s = shift if ref($s); # accept object or non-object calls
Branch: 1 branch
BranchTrueFalseTotal
if ref $s 0 0 0
71  
72 0 $s = ( split( m|[/\\]|, $s ) )[-1];
73 0 return $s;
74 }
75  
76 sub normalize_newlines
77 0 ▶{
Subroutine normalize_newlines: not called CC=2 SCAR=17.9
Pod documented
78 0 my $s = shift;
79 0 ▶ $s = shift if ref($s); # accept object or non-object calls
Branch: 1 branch
BranchTrueFalseTotal
if ref $s 0 0 0
80  
81 0 $s =~ s/\x0d\x0a|\x0d/\n/gsm;
82 0 return $s;
83 }
84  
85 1 1;
86  
87 =head1 NAME
88  
89 BW::Common - Some common utility functions
90  
91 =head1 SYNOPSIS
92  
93 use BW::Common
94 my $o = BW::Common->new;
95  
96 =head1 METHODS
97  
98 =over 4
99  
100 =item B<new>
101  
102 Constructs a new BW::Common object.
103  
104 Returns a blessed BW::Common object reference.
105 Returns undef (VOID) if the object cannot be created.
106  
107 =item B<comma> number
108  
109 Returns a comma-fied string from number.
110  
111 =item B<trim> string
112  
113 Returns the string with leaading and trailing spaces removed
114  
115 =item B<basename> string
116  
117 Returns the string with basename of a filename.
118  
119 =item B<normalize_newlines> string
120  
121 Returns the string with CRs and CRLF pairs normalized to NLs.
122  
123 =item B<readfile> filename, [max_length], [layer]
124  
125 Read a file and return as a string.
126  
127 max_length is the maximum length to read. It defaults to 1MB (1024 * 1024).
128  
129 layer is the LAYER string passed to binmode. It defaults to ":raw" (for binary files).
130  
131 =back
132  
133 =head1 AUTHOR
134  
135 Written by Bill Weinman E<lt>http://bw.org/E<gt>.
136  
137 =head1 COPYRIGHT
138  
139 Copyright (c) 1995-2010 The BearHeart Group, LLC
140  
141 =head1 HISTORY
142  
143 2010-03-08 bw -- updated basename to work with backslash for MS Win
144 2010-02-02 bw -- first CPAN release
145 2009-12-22 bw -- added readfile
146 2009-11-25 bw -- initial version.
147  
148 =cut
149  
« Config.pm ↑ Summary Jumptable.pm »