| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Data::Printer::Filter::SCALAR; | 
| 2 | 34 |  |  | 34 |  | 235 | use strict; | 
|  | 34 |  |  |  |  | 71 |  | 
|  | 34 |  |  |  |  | 1076 |  | 
| 3 | 34 |  |  | 34 |  | 254 | use warnings; | 
|  | 34 |  |  |  |  | 76 |  | 
|  | 34 |  |  |  |  | 1034 |  | 
| 4 | 34 |  |  | 34 |  | 14233 | use Data::Printer::Filter; | 
|  | 34 |  |  |  |  | 76 |  | 
|  | 34 |  |  |  |  | 259 |  | 
| 5 | 34 |  |  | 34 |  | 201 | use Scalar::Util; | 
|  | 34 |  |  |  |  | 68 |  | 
|  | 34 |  |  |  |  | 5866 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | filter 'SCALAR' => \&parse; | 
| 8 |  |  |  |  |  |  | filter 'LVALUE' => sub { | 
| 9 |  |  |  |  |  |  | my ($scalar_ref, $ddp) = @_; | 
| 10 |  |  |  |  |  |  | my $string = parse($scalar_ref, $ddp); | 
| 11 |  |  |  |  |  |  | if ($ddp->show_lvalue) { | 
| 12 |  |  |  |  |  |  | $string .= $ddp->maybe_colorize(' (LVALUE)', 'lvalue'); | 
| 13 |  |  |  |  |  |  | } | 
| 14 |  |  |  |  |  |  | return $string; | 
| 15 |  |  |  |  |  |  | }; | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | sub parse { | 
| 18 | 316 |  |  | 316 | 0 | 619 | my ($scalar_ref, $ddp) = @_; | 
| 19 |  |  |  |  |  |  |  | 
| 20 | 316 |  |  |  |  | 474 | my $ret; | 
| 21 | 316 | 50 |  |  |  | 838 | my $value = ref $scalar_ref ? $$scalar_ref : $scalar_ref; | 
| 22 |  |  |  |  |  |  |  | 
| 23 | 316 | 100 |  |  |  | 1131 | if (not defined $value) { | 
|  |  | 100 |  |  |  |  |  | 
|  |  | 100 |  |  |  |  |  | 
| 24 | 21 |  |  |  |  | 65 | $ret = $ddp->maybe_colorize('undef', 'undef'); | 
| 25 |  |  |  |  |  |  | } | 
| 26 |  |  |  |  |  |  | elsif ( $ddp->show_dualvar ne 'off' ) { | 
| 27 | 285 |  |  |  |  | 481 | my $numified; | 
| 28 | 34 | 50 |  | 34 |  | 281 | $numified = do { no warnings 'numeric'; 0+ $value } if defined $value; | 
|  | 34 |  |  |  |  | 71 |  | 
|  | 34 |  |  |  |  | 25024 |  | 
|  | 285 |  |  |  |  | 682 |  | 
|  | 285 |  |  |  |  | 763 |  | 
| 29 | 285 | 100 | 66 |  |  | 804 | if ( $numified ) { | 
|  |  | 100 |  |  |  |  |  | 
| 30 | 218 | 100 | 100 |  |  | 881 | if ( "$numified" eq $value | 
|  |  |  | 100 |  |  |  |  | 
|  |  |  | 100 |  |  |  |  | 
| 31 |  |  |  |  |  |  | || ( | 
| 32 |  |  |  |  |  |  | # lax mode allows decimal zeroes | 
| 33 |  |  |  |  |  |  | $ddp->show_dualvar eq 'lax' | 
| 34 |  |  |  |  |  |  | && ((index("$numified",'.') != -1 && $value =~ /\A\s*${numified}[0]*\s*\z/) | 
| 35 |  |  |  |  |  |  | || (index("$numified",'.') == -1 && $value =~ /\A\s*$numified(?:\.[0]*)?\s*\z/)) | 
| 36 |  |  |  |  |  |  | ) | 
| 37 |  |  |  |  |  |  | ) { | 
| 38 | 206 |  |  |  |  | 780 | $value =~ s/\A\s+//; | 
| 39 | 206 |  |  |  |  | 578 | $value =~ s/\s+\z//; | 
| 40 | 206 |  |  |  |  | 541 | $ret = $ddp->maybe_colorize($value, 'number'); | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  | else { | 
| 43 | 12 |  |  |  |  | 56 | $ret = Data::Printer::Common::_process_string( $ddp, "$value", 'string' ); | 
| 44 | 12 |  |  |  |  | 36 | $ret = _quoteme($ddp, $ret); | 
| 45 | 12 |  |  |  |  | 34 | $ret .= ' (dualvar: ' . $ddp->maybe_colorize( $numified, 'number' ) . ')'; | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  | } | 
| 48 |  |  |  |  |  |  | elsif ( !$numified && _is_number($value) ) { | 
| 49 | 8 |  |  |  |  | 28 | $ret = $ddp->maybe_colorize($value, 'number'); | 
| 50 |  |  |  |  |  |  | } | 
| 51 |  |  |  |  |  |  | else { | 
| 52 | 59 |  |  |  |  | 243 | $ret = Data::Printer::Common::_process_string($ddp, $value, 'string'); | 
| 53 | 59 |  |  |  |  | 184 | $ret = _quoteme($ddp, $ret); | 
| 54 |  |  |  |  |  |  | } | 
| 55 |  |  |  |  |  |  | } | 
| 56 |  |  |  |  |  |  | elsif (_is_number($value)) { | 
| 57 | 6 |  |  |  |  | 18 | $ret = $ddp->maybe_colorize($value, 'number'); | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  | else { | 
| 60 | 4 |  |  |  |  | 13 | $ret = Data::Printer::Common::_process_string($ddp, $value, 'string'); | 
| 61 | 4 |  |  |  |  | 19 | $ret = _quoteme($ddp, $ret); | 
| 62 |  |  |  |  |  |  | } | 
| 63 | 316 |  |  |  |  | 822 | $ret .= _check_tainted($ddp, $scalar_ref); | 
| 64 | 316 |  |  |  |  | 733 | $ret .= _check_unicode($ddp, $scalar_ref); | 
| 65 |  |  |  |  |  |  |  | 
| 66 | 316 | 100 | 100 |  |  | 787 | if ($ddp->show_tied and my $tie = ref tied $$scalar_ref) { | 
| 67 | 1 |  |  |  |  | 5 | $ret .= " (tied to $tie)"; | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 316 |  |  |  |  | 1212 | return $ret; | 
| 71 |  |  |  |  |  |  | }; | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | ####################################### | 
| 74 |  |  |  |  |  |  | ### Private auxiliary helpers below ### | 
| 75 |  |  |  |  |  |  | ####################################### | 
| 76 |  |  |  |  |  |  | sub _quoteme { | 
| 77 | 75 |  |  | 75 |  | 181 | my ($ddp, $text) = @_; | 
| 78 |  |  |  |  |  |  |  | 
| 79 | 75 |  |  |  |  | 202 | my $scalar_quotes = $ddp->scalar_quotes; | 
| 80 | 75 | 50 |  |  |  | 232 | if (defined $scalar_quotes) { | 
| 81 |  |  |  |  |  |  | # foo'bar ==> 'foo\'bar' | 
| 82 | 75 | 50 |  |  |  | 292 | $text =~ s{$scalar_quotes}{\\$scalar_quotes}g if index($text, $scalar_quotes) >= 0; | 
| 83 | 75 |  |  |  |  | 204 | my $quote = $ddp->maybe_colorize( $scalar_quotes, 'quotes' ); | 
| 84 | 75 |  |  |  |  | 273 | $text = $quote . $text . $quote; | 
| 85 |  |  |  |  |  |  | } | 
| 86 | 75 |  |  |  |  | 236 | return $text; | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | sub _check_tainted { | 
| 90 | 316 |  |  | 316 |  | 604 | my ($self, $var) = @_; | 
| 91 | 316 | 100 | 100 |  |  | 791 | return ' (TAINTED)' if $self->show_tainted && Scalar::Util::tainted($$var); | 
| 92 | 313 |  |  |  |  | 843 | return ''; | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | sub _check_unicode { | 
| 96 | 316 |  |  | 316 |  | 617 | my ($self, $var) = @_; | 
| 97 | 316 | 100 | 100 |  |  | 790 | return ' (U)' if $self->show_unicode && utf8::is_utf8($$var); | 
| 98 | 315 |  |  |  |  | 687 | return ''; | 
| 99 |  |  |  |  |  |  | } | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | sub _is_number { | 
| 102 | 77 |  |  | 77 |  | 201 | my ($maybe_a_number) = @_; | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | # Scalar values that start with a zero are strings, NOT numbers. | 
| 105 |  |  |  |  |  |  | # You can write `my $foo = 0123`, but then `$foo` will be 83, | 
| 106 |  |  |  |  |  |  | # (numbers starting with zero are octal integers) | 
| 107 | 77 | 50 |  |  |  | 280 | return if $maybe_a_number =~ /^-?0[0-9]/; | 
| 108 |  |  |  |  |  |  |  | 
| 109 | 77 |  |  |  |  | 381 | my $is_number = $maybe_a_number =~ m/ | 
| 110 |  |  |  |  |  |  | ^ | 
| 111 |  |  |  |  |  |  | -?          # numbers may begin with a '-' sign, but can't with a '+'. | 
| 112 |  |  |  |  |  |  | # If they do they are not numbers, but strings. | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | [0-9]+      # then there should be some numbers | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | ( \. [0-9]+ )?      # there can be decimal part, which is optional | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | ( e [+-] [0-9]+ )?  # and an also optional exponential notation part | 
| 119 |  |  |  |  |  |  | \z | 
| 120 |  |  |  |  |  |  | /x; | 
| 121 |  |  |  |  |  |  |  | 
| 122 | 77 |  |  |  |  | 336 | return $is_number; | 
| 123 |  |  |  |  |  |  | } | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | 1; |