blib/lib/IDS/Utils.pm untested

filter: Statementstmt 0.0% 0 / 48 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 111 CC 7 SCAR 23.0
CC
7
Cov
0%
uniq
20.0
fh_or_stdout
6.0
to_fh
6.0
CRAP 10.0
1 package IDS::Utils;
2 $IDS::Utils::VERSION = "1.0";
3  
4 require Exporter;
5 our @ISA = ("Exporter");
6 our @EXPORT = qw ();
7 our @EXPORT_OK = qw (&fh_or_stdout &uniq &to_fh &split_value);
8  
9 use Carp qw(cluck carp confess);
10  
11 =over
12  
13 =item fh_or_stdout(filehandle)
14  
15 =item fh_or_stdout(filehandle, destination)
16  
17 =back
18  
19 A utility function that returns either the filehandle passed in (if
20 defined) or a IO::Handle to STDOUT or the specified destination (which
21 is a fileno(filehandle)).
22  
23 It would be nice to make this function more general (e.g., what about
24 stdin or stderr? a specified file?).
25  
26 =cut
27  
28 sub fh_or_stdout {
29 my $fh = shift;
30 my $dest_fh = shift; # not required; default is STDOUT
31 defined($dest_fh) or $dest_fh = fileno(STDOUT);
32  
33 unless (defined($fh)) {
34 $fh = new IO::Handle;
35 $fh->fdopen($dest_fh,"w") or
36 confess "Unable to fdopen STDOUT: $!\n";
37 }
38 return $fh;
39 }
40  
41 =over
42  
43 =item uniq(listref)
44  
45 =item uniq(list)
46  
47 Ensure no dups in the list (or list reference) we get.
48  
49 The return value is either the list or a reference to a copy of the
50 list, depending on the calling context. The list will have been sorted
51 as a side effect.
52  
53 =back
54  
55 =cut
56  
57 sub uniq {
58 my (@l, $i);
59  
60 if ($#_ == -1) {
61 carp "uniq was not passed anything to work on!\n";
62 return undef;
63 }
64  
65 if (ref($_[0])) {
66 my $lref = shift;
67 @l = sort @$lref;
68 } else {
69 @l = sort @_;
70 }
71  
72 for ($i=0; $i<$#l; $i++) {
73 $l[$i] eq $l[$i+1] && splice(@l, $i+1, 1);
74 }
75 return wantarray ? @l : \@l;
76 }
77  
78 =over
79  
80 =item to_fh(filename)
81  
82 =item to_fh(filename, method)
83  
84 =item to_fh(filehandle)
85  
86 Return a filehandle. If the argument is a file name, we will open the
87 file with the method specified, for reading if unspecified. If the
88 argument is a filehandle, it is simply returned.
89  
90 =back
91  
92 =cut
93  
94 sub to_fh {
95 my $arg = shift;
96 my $method = shift || "<";
97 my $fh;
98  
99 defined($arg) or confess "to_fh missing filename/filehandle argument";
100  
101 $fh = ref($arg) && $arg->isa("IO::Handle")
102 ? $arg
103 : (new IO::File("$method $arg") or confess "Cannot open $arg: $!\n");
104  
105 return $fh;
106 }
107  
108 =over
109  
110 =item split_value($name, $pattern, $value)
111  
112 Split the value using the pattern given. For each of the resulting
113 pieces, prepend "$name: ". This function is used when there are a
114 collection of values for a given name (e.g., q-values, accept values).
115  
116 =back
117  
118 =cut
119  
120 sub split_value {
121 my ($name, $pattern, $value) = @_;
122 my @pieces = ();
123
124 # @pieces = map {$_ ? "$name: $_" : "BUG: undef in split_value '$value' split at '$pattern'"} split /$pattern/, $value;
125 for my $part ( split /$pattern/, $value ) {
126 next unless defined($part) && $part ne "";
127 push @pieces, "$name: $part";
128 }
129 # @pieces = map {$_ ? "$name: $_" : undef} split /$pattern/, $value;
130 for (my $i=0; $i <= $#pieces; $i++) {
131 defined($pieces[$i]) && $pieces[$i] or
132 splice @pieces, $i, 1;
133 }
134 return @pieces;
135 }
136  
137 =head1 AUTHOR INFORMATION
138  
139 Copyright 2005-2007, Kenneth Ingham. All rights reserved.
140  
141 This library is free software; you can redistribute it and/or modify
142 it under the same terms as Perl itself.
143  
144 Address bug reports and comments to: ids_test at i-pi.com. When sending
145 bug reports, please provide the versions of IDS::Test.pm, IDS::Algorithm.pm,
146 IDS::DataSource.pm, the version of Perl, and the name and version of the
147 operating system you are using. Since Kenneth is a PhD student, the
148 speed of the reponse depends on how the research is proceeding.
149  
150 =head1 BUGS
151  
152 Please report them.
153  
154 =head1 SEE ALSO
155  
156 L<IDS::Algorithm>, L<IDS::DataSource>
157  
158 =cut
159  
160  
161 1;