line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
|
|
1
|
|
1059
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
58
|
|
11
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
30
|
|
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
6
|
use BW::Constants; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
65
|
|
14
|
1
|
|
|
1
|
|
5
|
use IO::File; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
300
|
|
15
|
1
|
|
|
1
|
|
7
|
use base qw( BW::Base ); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
7279
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
our $VERSION = "0.5"; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub _init |
20
|
|
|
|
|
|
|
{ |
21
|
0
|
|
|
0
|
|
|
my $self = shift; |
22
|
0
|
|
|
|
|
|
$self->SUPER::_init(@_); |
23
|
|
|
|
|
|
|
|
24
|
0
|
|
|
|
|
|
return SUCCESS; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# _setter_getter entry points |
28
|
0
|
|
|
0
|
0
|
|
sub stub { BW::Base::_setter_getter(@_); } |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# read a file up to length bytes (defaults to 1MB) |
31
|
|
|
|
|
|
|
# binmode defaults to ":raw" |
32
|
|
|
|
|
|
|
sub readfile |
33
|
|
|
|
|
|
|
{ |
34
|
0
|
|
|
0
|
1
|
|
my $fn = shift; |
35
|
0
|
0
|
|
|
|
|
$fn = shift if ref($fn); # accept object or non-object calls |
36
|
0
|
|
0
|
|
|
|
my $length = shift || ( 1024 * 1024 ); |
37
|
0
|
|
0
|
|
|
|
my $layer = shift || ":raw"; |
38
|
0
|
|
|
|
|
|
my $buf; |
39
|
|
|
|
|
|
|
|
40
|
0
|
0
|
|
|
|
|
my $fh = IO::File->new( $fn, 'r' ) or return FAILURE; |
41
|
0
|
|
|
|
|
|
$fh->binmode($layer); |
42
|
0
|
|
|
|
|
|
my $rc = $fh->read( $buf, $length ); |
43
|
0
|
0
|
|
|
|
|
return FAILURE unless defined $rc; |
44
|
0
|
|
|
|
|
|
$fh->close; |
45
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
|
return $buf; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub comma |
50
|
|
|
|
|
|
|
{ |
51
|
0
|
|
|
0
|
1
|
|
my $n = shift; |
52
|
0
|
0
|
|
|
|
|
$n = shift if ref($n); # accept object or non-object calls |
53
|
|
|
|
|
|
|
|
54
|
0
|
|
|
|
|
|
while ( $n =~ s/^(-?\d+)(\d{3})/$1,$2/ ) {} |
55
|
0
|
|
|
|
|
|
return $n; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub trim |
59
|
|
|
|
|
|
|
{ |
60
|
0
|
|
|
0
|
1
|
|
my $s = shift; |
61
|
0
|
0
|
|
|
|
|
$s = shift if ref($s); # accept object or non-object calls |
62
|
|
|
|
|
|
|
|
63
|
0
|
|
|
|
|
|
$s =~ /^\s*(.*?)\s*$/; |
64
|
0
|
|
|
|
|
|
return $1; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub basename |
68
|
|
|
|
|
|
|
{ |
69
|
0
|
|
|
0
|
1
|
|
my $s = shift; |
70
|
0
|
0
|
|
|
|
|
$s = shift if ref($s); # accept object or non-object calls |
71
|
|
|
|
|
|
|
|
72
|
0
|
|
|
|
|
|
$s = ( split( m|[/\\]|, $s ) )[-1]; |
73
|
0
|
|
|
|
|
|
return $s; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub normalize_newlines |
77
|
|
|
|
|
|
|
{ |
78
|
0
|
|
|
0
|
1
|
|
my $s = shift; |
79
|
0
|
0
|
|
|
|
|
$s = shift if ref($s); # accept object or non-object calls |
80
|
|
|
|
|
|
|
|
81
|
0
|
|
|
|
|
|
$s =~ s/\x0d\x0a|\x0d/\n/gsm; |
82
|
0
|
|
|
|
|
|
return $s; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
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 |
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 number |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
Returns a comma-fied string from number. |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=item B string |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
Returns the string with leaading and trailing spaces removed |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=item B string |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
Returns the string with basename of a filename. |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=item B string |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
Returns the string with CRs and CRLF pairs normalized to NLs. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=item B 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 Ehttp://bw.org/E. |
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
|
|
|
|
|
|
|
|