| 1 | package TPath::Stringifiable; | ||||||||||||||||||||||||||||
| 2 | 12 | $TPath::Stringifiable::VERSION = '1.007'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: role requiring that a class have a to_string method | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 12 | ▶ | use v5.10; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 12 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 12 | ▶ | use Scalar::Util qw(looks_like_number); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 12 | requires 'to_string'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # method available to to_string that adds escape characters as needed | ||||||||||||||||||||||||||||
| 14 | # params: string -- string to escape | ||||||||||||||||||||||||||||
| 15 | # chars -- characters to escape -- \ always added | ||||||||||||||||||||||||||||
| 16 | 0 | ▶ | sub _escape { | ||||||||||||||||||||||||||
|
Subroutine
_escape: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 17 | 0 | my ( $self, $string, @chars ) = @_; | |||||||||||||||||||||||||||
| 18 | 0 | my $s = ''; | |||||||||||||||||||||||||||
| 19 | 0 | my %chars = map { $_ => 1 } @chars, '\\'; | |||||||||||||||||||||||||||
| 20 | 0 | state $special = [ "\t", "\f", "\n", "\r", "\013", "\b" ]; | |||||||||||||||||||||||||||
| 21 | 0 | for my $c ( split //, $string ) { | |||||||||||||||||||||||||||
| 22 | 0 | ▶ | if ( $c ~~ $special ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 23 | 0 | for ($c) { | |||||||||||||||||||||||||||
| 24 | 0 | when ("\t") { $c = '\\t' } | |||||||||||||||||||||||||||
| 25 | 0 | when ("\f") { $c = '\\f' } | |||||||||||||||||||||||||||
| 26 | 0 | when ("\012") { $c = '\\n' } | |||||||||||||||||||||||||||
| 27 | 0 | when ("\015") { $c = '\\r' } | |||||||||||||||||||||||||||
| 28 | 0 | when ("\013") { $c = '\\v' } | |||||||||||||||||||||||||||
| 29 | 0 | when ("\b") { $c = '\\b' } | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | elsif ( $chars{$c} ) { | ||||||||||||||||||||||||||||
| 33 | 0 | $c = '\\' . $c; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | 0 | $s .= $c; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | 0 | return $s; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # general stringification procedure | ||||||||||||||||||||||||||||
| 41 | 0 | ▶ | sub _stringify { | ||||||||||||||||||||||||||
|
Subroutine
_stringify: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 42 | 0 | my ( $self, $arg, @args ) = @_; | |||||||||||||||||||||||||||
| 43 | 0 | ▶ | return $arg->to_string(@args) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
builtin::blessed($arg) and $arg->can("to_string")
MC/DC0%0/2
builtin::blessed($arg) and $arg->can("to_string")
builtin::blessed($arg)
$arg->can("to_string")
Truth table0%0/3
A and B
A= builtin::blessed($arg)B= $arg->can("to_string")
| |||||||||||||||||||||||||||||
| 44 | if blessed $arg && $arg->can('to_string'); | ||||||||||||||||||||||||||||
| 45 | 0 | ▶ | confess 'unexpected argument type: ' . ref $arg if ref $arg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | return $arg if looks_like_number $arg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 0 | return "'" . $self->_escape( $arg, "'" ) . "'"; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # converts some label -- tag name or attribute name -- into a parsable string | ||||||||||||||||||||||||||||
| 51 | 0 | ▶ | sub _stringify_label { | ||||||||||||||||||||||||||
|
Subroutine
_stringify_label: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 52 | 0 | my ( $self, $string, $first ) = @_; | |||||||||||||||||||||||||||
| 53 | 0 | ▶ | return $string | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | if $string =~ | ||||||||||||||||||||||||||||
| 55 | /^(?>\\.|[\p{L}\$_])(?>[\p{L}\$\p{N}_]|[-.:](?=[\p{L}_\$\p{N}])|\\.)*+$/; | ||||||||||||||||||||||||||||
| 56 | 0 | ▶ | return ( $first ? ':' : '' ) . '"' . $self->_escape($string) . '"' | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 57 | unless ( index $string, '"' ) > -1; | ||||||||||||||||||||||||||||
| 58 | 0 | ▶ | return ( $first ? ':' : '' ) . "'" . $self->_escape($string) . "'" | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 59 | unless ( index $string, "'" ) > -1; | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # safety fallback | ||||||||||||||||||||||||||||
| 62 | 0 | return $self->_escape( $string, grep { !/[\p{L}]\$_/ } split //, $string ); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 12 | 1; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | __END__ | ||||||||||||||||||||||||||||