| 1 | package Util::Underscore::Scalars; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | #ABSTRACT: Functions for introspecting and manipulating scalars | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 12 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | ## no critic (ProhibitMultiplePackages) | ||||||||||||||||||||||||||||||||||||||||||
| 10 | package # hide from PAUSE | ||||||||||||||||||||||||||||||||||||||||||
| 11 | _; | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | ## no critic (RequireArgUnpacking, RequireFinalReturn, ProhibitSubroutinePrototypes) | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | ## no critic (ProtectPrivateVars) | ||||||||||||||||||||||||||||||||||||||||||
| 16 | 12 | $Util::Underscore::_ASSIGN_ALIASES->('Scalar::Util', new_dual => 'dualvar'); | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | sub is_dual(_) { | ||||||||||||||||||||||||||||||||||||||||||
| 19 | 4 | ▶ | goto &Scalar::Util::isdual; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_dual: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | sub is_vstring(_) { | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 7 | ▶ | goto &Scalar::Util::isvstring; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_vstring: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | sub is_readonly(_) { | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 8 | ▶ | goto &Scalar::Util::readonly; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_readonly: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | ## no critic (ProtectPrivateVars) | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 12 | $Util::Underscore::_ASSIGN_ALIASES->('Const::Fast', const => 'const'); | |||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | sub is_tainted (_) { | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 4 | ▶ | goto &Scalar::Util::tainted; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_tainted: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 12 | ▶ | if (eval q{use Data::Alias 1.18 (); 1}) { ## no critic (ProhibitStringyEval) | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | ## no critic (ProtectPrivateVars) | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | $Util::Underscore::_ASSIGN_ALIASES->('Data::Alias', alias => 'alias'); | |||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | sub is_plain(_) { | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 7 | ▶ | defined $_[0] | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_plain: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | && !defined ref_type $_[0]; | ||||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | sub is_string(_) { | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 27 | ▶ | defined $_[0] | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_string: called CC=3 SCAR=11.0
Branch: 1 branch
Condition100%6/6
defined $_[0] && (!defined(&ref_type($_[0])) || overload::Method($_[0], '""'))
not defined &ref_type($_[0]) or overload::Method($_[0], "\"\"")
MC/DC100%3/3
defined $_[0] && (!defined(&ref_type($_[0])) || overload::Method($_[0], '""'))
defined $_[0]
not defined &ref_type($_[0])
overload::Method($_[0], "\"\"")
Truth table100%4/4
A && not(B) or C
A= defined $_[0]B= not defined &ref_type($_[0])C= overload::Method($_[0], "\"\"")
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | && (!defined ref_type $_[0] | ||||||||||||||||||||||||||||||||||||||||||
| 50 | || overload::Method($_[0], q[""])); | ||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | sub is_bool(_) { | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 10 | ▶ | !defined ref_type $_[0] | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_bool: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | || overload::Method($_[0], q[bool]); | ||||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | sub is_identifier(_) { | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 11 | ▶ | defined $_[0] | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_identifier: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | && scalar($_[0] =~ /\A [^\W\d]\w* \z/xsm); | ||||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | sub is_package(_) { | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 25 | ▶ | defined $_[0] | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_package: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | && scalar($_[0] =~ /\A [^\W\d]\w* (?: [:][:]\w+ )* \z/xsm); | ||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | sub chomp(_;$) { | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | # localizedly set the input record separator | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 8 | ▶ | local $/ = $/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
chomp: called CC=7 SCAR=20.0
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 8 | ▶ | if (@_ > 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 4 | ▶ | if (_::is_string $_[1]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 4 | $/ = "$_[1]"; | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | Carp::croak q(_::chomp: second argument must be a string); | |||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | # handle a single string | ||||||||||||||||||||||||||||||||||||||||||
| 82 | # ATTENTION: this depends on _::is_string | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 8 | ▶ | if (_::is_string $_[0]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 4 | CORE::chomp(my $copy = $_[0]); | |||||||||||||||||||||||||||||||||||||||||
| 85 | 4 | return $copy; | |||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | # handle an arrayref of strings (effectively auto-mapping) | ||||||||||||||||||||||||||||||||||||||||||
| 89 | # ATTENTION: _::is_array_ref will be defined later | ||||||||||||||||||||||||||||||||||||||||||
| 90 | elsif (_::is_array_ref($_[0])) { | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 4 | my @result = @{ $_[0] }; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 4 | for (@result) { | |||||||||||||||||||||||||||||||||||||||||
| 93 | 8 | ▶ | goto ERROR if not _::is_string; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 8 | CORE::chomp; | |||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||
| 96 | 4 | return \@result; | |||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | ERROR: | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | Carp::croak | |||||||||||||||||||||||||||||||||||||||||
| 101 | q(_::chomp: first argument must be string or arrayref of strings); | ||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | sub index($$;$) { | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 7 | ▶ | if (@_ >= 3 and $_[2] < 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
index: called CC=5 SCAR=16.7
Branch: 1 branch
Condition66%2/3
@_ >= 3 and $_[2] < 0
MC/DC0%0/2
@_ >= 3 and $_[2] < 0
@_ >= 3
$_[2] < 0
Truth table66%2/3
A and B
A= @_ >= 3B= $_[2] < 0
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | Carp::croak q(_::index: starting position must be non-negative.) | |||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 7 | ▶ | my $result = CORE::index($_[0], $_[1], $_[2] // 0); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$_[2] // 0
MC/DC100%1/1
$_[2] // 0
$_[2]
Truth table100%2/2
A
A= $_[2]
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 7 | ▶ | return ($result >= 0) ? $result : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 12 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | __END__ | ||||||||||||||||||||||||||||||||||||||||||