| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Util::Underscore::Scalars; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | #ABSTRACT: Functions for introspecting and manipulating scalars | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 12 |  |  | 12 |  | 43 | use strict; | 
|  | 12 |  |  |  |  | 15 |  | 
|  | 12 |  |  |  |  | 295 |  | 
| 6 | 12 |  |  | 12 |  | 42 | use warnings; | 
|  | 12 |  |  |  |  | 13 |  | 
|  | 12 |  |  |  |  | 6639 |  | 
| 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 |  |  |  |  |  |  | $Util::Underscore::_ASSIGN_ALIASES->('Scalar::Util', new_dual => 'dualvar'); | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | sub is_dual(_) { | 
| 19 | 4 |  |  | 4 |  | 97495 | goto &Scalar::Util::isdual; | 
| 20 |  |  |  |  |  |  | } | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | sub is_vstring(_) { | 
| 23 | 7 |  |  | 7 |  | 2870 | goto &Scalar::Util::isvstring; | 
| 24 |  |  |  |  |  |  | } | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | sub is_readonly(_) { | 
| 27 | 8 |  |  | 8 |  | 5124 | goto &Scalar::Util::readonly; | 
| 28 |  |  |  |  |  |  | } | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | ## no critic (ProtectPrivateVars) | 
| 31 |  |  |  |  |  |  | $Util::Underscore::_ASSIGN_ALIASES->('Const::Fast', const => 'const'); | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | sub is_tainted (_) { | 
| 34 | 4 |  |  | 4 |  | 10153 | goto &Scalar::Util::tainted; | 
| 35 |  |  |  |  |  |  | } | 
| 36 |  |  |  |  |  |  |  | 
| 37 | 12 |  |  | 12 |  | 2113 | if (eval q{use Data::Alias 1.18 (); 1}) {  ## no critic (ProhibitStringyEval) | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 38 |  |  |  |  |  |  | ## no critic (ProtectPrivateVars) | 
| 39 |  |  |  |  |  |  | $Util::Underscore::_ASSIGN_ALIASES->('Data::Alias', alias => 'alias'); | 
| 40 |  |  |  |  |  |  | } | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | sub is_plain(_) { | 
| 43 | 7 | 100 |  | 7 |  | 3628 | defined $_[0] | 
| 44 |  |  |  |  |  |  | && !defined ref_type $_[0]; | 
| 45 |  |  |  |  |  |  | } | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | sub is_string(_) { | 
| 48 | 27 | 100 | 100 | 27 |  | 3750 | defined $_[0] | 
| 49 |  |  |  |  |  |  | && (!defined ref_type $_[0] | 
| 50 |  |  |  |  |  |  | || overload::Method($_[0], q[""])); | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | sub is_bool(_) { | 
| 54 | 10 | 100 |  | 10 |  | 2958 | !defined ref_type $_[0] | 
| 55 |  |  |  |  |  |  | || overload::Method($_[0], q[bool]); | 
| 56 |  |  |  |  |  |  | } | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | sub is_identifier(_) { | 
| 59 | 11 | 100 |  | 11 |  | 1680 | defined $_[0] | 
| 60 |  |  |  |  |  |  | && scalar($_[0] =~ /\A [^\W\d]\w* \z/xsm); | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | sub is_package(_) { | 
| 64 | 25 | 100 |  | 25 |  | 1872 | defined $_[0] | 
| 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 |  |  | 8 |  | 2626 | local $/ = $/; | 
| 72 | 8 | 100 |  |  |  | 24 | if (@_ > 1) { | 
| 73 | 4 | 50 |  |  |  | 8 | if (_::is_string $_[1]) { | 
| 74 | 4 |  |  |  |  | 8 | $/ = "$_[1]"; | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  | else { | 
| 77 | 0 |  |  |  |  | 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 | 100 |  |  |  | 12 | if (_::is_string $_[0]) { | 
|  |  | 50 |  |  |  |  |  | 
| 84 | 4 |  |  |  |  | 8 | CORE::chomp(my $copy = $_[0]); | 
| 85 | 4 |  |  |  |  | 15 | 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 |  |  |  |  | 4 | my @result = @{ $_[0] }; | 
|  | 4 |  |  |  |  | 10 |  | 
| 92 | 4 |  |  |  |  | 6 | for (@result) { | 
| 93 | 8 | 50 |  |  |  | 10 | goto ERROR if not _::is_string; | 
| 94 | 8 |  |  |  |  | 14 | CORE::chomp; | 
| 95 |  |  |  |  |  |  | } | 
| 96 | 4 |  |  |  |  | 19 | return \@result; | 
| 97 |  |  |  |  |  |  | } | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | ERROR: | 
| 100 | 0 |  |  |  |  | 0 | Carp::croak | 
| 101 |  |  |  |  |  |  | q(_::chomp: first argument must be string or arrayref of strings); | 
| 102 |  |  |  |  |  |  | } | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | sub index($$;$) { | 
| 105 | 7 | 50 | 66 | 7 |  | 1538 | if (@_ >= 3 and $_[2] < 0) { | 
| 106 | 0 |  |  |  |  | 0 | Carp::croak q(_::index: starting position must be non-negative.) | 
| 107 |  |  |  |  |  |  | } | 
| 108 | 7 |  | 100 |  |  | 23 | my $result = CORE::index($_[0], $_[1], $_[2] // 0); | 
| 109 | 7 | 100 |  |  |  | 28 | return ($result >= 0) ? $result : undef; | 
| 110 |  |  |  |  |  |  | } | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | 1; | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | __END__ |