Common/Various.pm

filter: Statementstmt 23.3% 7 / 30 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 7.1% 1 / 14 Podpod 100% 13 / 13 totaltotal 26.2% 21 / 80 CC 11 SCAR 17.7
CC
11
Cov
14%
text2bool
12.0
to_bool
6.0
to_int
6.0
CRAP 5.9
« Common/DataStructure.pm ↑ Summary Common.pm »
1 package MDK::Common::Various;
2  
3 =head1 NAME
4  
5 MDK::Common::Various - miscellaneous functions
6  
7 =head1 SYNOPSIS
8  
9 use MDK::Common::Various qw(:all);
10  
11 =head1 EXPORTS
12  
13 =over
14  
15 =item first(LIST)
16  
17 returns the first value. C<first(XXX)> is an alternative for C<((XXX)[0])>
18  
19 =item second(LIST)
20  
21 returns the second value. C<second(XXX)> is an alternative for C<((XXX)[1])>
22  
23 =item top(LIST)
24  
25 returns the last value. C<top(@l)> is an alternative for C<$l[$#l]>
26  
27 =item to_bool(SCALAR)
28  
29 returns a value in { 0, 1 }
30  
31 =item to_int(STRING)
32  
33 extracts the number from the string. You could use directly C<int "11 foo">, but
34 you'll get I<Argument "11 foo" isn't numeric in int>. It also handles returns
35 11 for C<"foo 11 bar">
36  
37 =item to_float(STRING)
38  
39 extract a decimal number from the string
40  
41 =item bool2text(SCALAR)
42  
43 returns a value in { "true", "false" }
44  
45 =item bool2yesno(SCALAR)
46  
47 returns a value in { "yes", "no" }
48  
49 =item text2bool(STRING)
50  
51 inverse of C<bool2text> and C<bool2yesno>
52  
53 =item chomp_(STRING)
54  
55 non-mutable version of chomp: do not modify the argument, returns the chomp'ed
56 value. Also works on lists: C<chomp_($a, $b)> is equivalent to
57 C<chomp($a) ; chomp($b) ; ($a,$b)>
58  
59 =item backtrace()
60  
61 returns a string describing the backtrace. eg:
62  
63 sub g { print "oops\n", backtrace() }
64 sub f { &g }
65 f();
66  
67 gives
68  
69 oops
70 main::g() called from /tmp/t.pl:2
71 main::f() called from /tmp/t.pl:4
72  
73  
74 =item internal_error(STRING)
75  
76 another way to C<die> with a nice error message and a backtrace
77  
78 =item noreturn()
79  
80 use this to ensure nobody uses the return value of the function. eg:
81  
82 sub g { print "g called\n"; noreturn }
83 sub f { print "g returns ", g() }
84 f();
85  
86 gives
87  
88 test.pl:3: main::f() expects a value from main::g(), but main::g() doesn't return any value
89  
90 =back
91  
92 =head1 SEE ALSO
93  
94 L<MDK::Common>
95  
96 =cut
97
98  
99 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
100 1 our @ISA = qw(Exporter);
101 1 our @EXPORT_OK = qw(first second top to_bool to_int to_float bool2text bool2yesno text2bool chomp_ backtrace internal_error noreturn);
102 1 our %EXPORT_TAGS = (all => [ @EXPORT_OK ]);
103  
104  
105 0 ▶sub first { $_[0] }
Subroutine first: not called CC=1 SCAR=6.9
Pod documented
106 0 ▶sub second { $_[1] }
Subroutine second: not called CC=1 SCAR=6.9
Pod documented
107 0 ▶sub top { $_[-1] }
Subroutine top: not called CC=1 SCAR=6.9
Pod documented
108  
109 0 ▶sub to_bool { $_[0] ? 1 : 0 }
Subroutine to_bool: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 0 0 0
110 0 ▶sub to_int { $_[0] =~ /\s*(\d*)/ && $1 }
Subroutine to_int: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /\s*(\d*)/ and $1 0 0 0
111 0 ▶sub to_float { $_[0] =~ /\s*(\d*(\.\d*)?)/ && $1 }
Subroutine to_float: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /\s*(\d*(\.\d*)?)/ and $1 0 0 0
112 0 ▶sub bool2text { $_[0] ? "true" : "false" }
Subroutine bool2text: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 0 0 0
113 0 ▶sub bool2yesno { $_[0] ? "yes" : "no" }
Subroutine bool2yesno: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 0 0 0
114 0 ▶sub text2bool { my $t = lc($_[0]); $t eq "true" || $t eq "yes" ? 1 : 0 }
Subroutine text2bool: not called CC=3 SCAR=24.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$t eq 'true' || $t eq 'yes' ? : 0 0 0
Condition0%0/3
$t eq "true" or $t eq "yes"
l!l&&r!l&&!r
000
MC/DC0%0/2
$t eq "true" or $t eq "yes"
$t eq "true" $t eq "yes"
Truth table0%0/3
A or B
A= $t eq "true"B= $t eq "yes"
ABresult
1X1
011
000
115  
116 0 ▶sub chomp_ { my @l = @_; chomp @l; wantarray() ? @l : $l[0] }
Subroutine chomp_: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
117  
118 0 ▶sub backtrace() {
Subroutine backtrace: not called CC=2 SCAR=17.9
Pod documented
119 0 my $s;
120 0 ▶ for (my $i = 1; caller($i); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if (caller $i) 0 0 0
121 0 my ($_package, $file, $line, $func) = caller($i);
122 0 $s .= "$func() called from $file:$line\n";
123 }
124 0 $s;
125 }
126  
127 0 ▶sub internal_error {
Subroutine internal_error: not called CC=1 SCAR=6.9
Pod documented
128 0 die "INTERNAL ERROR: $_[0]\n" . backtrace();
129 }
130  
131 0 ▶sub noreturn() {
Subroutine noreturn: not called CC=2 SCAR=17.9
Pod documented
132 0 ▶ if (defined wantarray()) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) 0 0 0
133 0 my ($_package, $file, $line, $func) = caller(1);
134 0 my (undef, undef, undef, $func2) = caller(2);
135 0 die "$file:$line: $func2() expects a value from $func(), but $func() doesn't return any value\n";
136 }
137 }
138  
139 1 1;
140  
« Common/DataStructure.pm ↑ Summary Common.pm »