Filename | /Users/ether/.perlbrew/libs/36.0@std/lib/perl5/YAML/PP/Representer.pm |
Statements | Executed 20 statements in 1.26ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 31µs | 35µs | BEGIN@1.294 | YAML::PP::Dumper::
1 | 1 | 1 | 9µs | 60µs | BEGIN@9 | YAML::PP::Representer::
1 | 1 | 1 | 8µs | 54µs | BEGIN@2.295 | YAML::PP::Dumper::
1 | 1 | 1 | 7µs | 10µs | BEGIN@18 | YAML::PP::Representer::
1 | 1 | 1 | 7µs | 46µs | BEGIN@7 | YAML::PP::Representer::
1 | 1 | 1 | 6µs | 6µs | new | YAML::PP::Representer::
0 | 0 | 0 | 0s | 0s | _represent_node_nonref | YAML::PP::Representer::
0 | 0 | 0 | 0s | 0s | _represent_noderef | YAML::PP::Representer::
0 | 0 | 0 | 0s | 0s | clone | YAML::PP::Representer::
0 | 0 | 0 | 0s | 0s | preserve_alias | YAML::PP::Representer::
0 | 0 | 0 | 0s | 0s | preserve_flow_style | YAML::PP::Representer::
0 | 0 | 0 | 0s | 0s | preserve_order | YAML::PP::Representer::
0 | 0 | 0 | 0s | 0s | preserve_scalar_style | YAML::PP::Representer::
0 | 0 | 0 | 0s | 0s | represent_node | YAML::PP::Representer::
0 | 0 | 0 | 0s | 0s | schema | YAML::PP::Representer::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | 2 | 39µs | 2 | 39µs | # spent 35µs (31+4) within YAML::PP::Dumper::BEGIN@1.294 which was called:
# once (31µs+4µs) by YAML::PP::Dumper::BEGIN@10 at line 1 # spent 35µs making 1 call to YAML::PP::Dumper::BEGIN@1.294
# spent 4µs making 1 call to strict::import |
2 | 2 | 58µs | 2 | 100µs | # spent 54µs (8+46) within YAML::PP::Dumper::BEGIN@2.295 which was called:
# once (8µs+46µs) by YAML::PP::Dumper::BEGIN@10 at line 2 # spent 54µs making 1 call to YAML::PP::Dumper::BEGIN@2.295
# spent 46µs making 1 call to warnings::import |
3 | package YAML::PP::Representer; | ||||
4 | |||||
5 | 1 | 1µs | our $VERSION = '0.035'; # VERSION | ||
6 | |||||
7 | 2 | 38µs | 2 | 85µs | # spent 46µs (7+39) within YAML::PP::Representer::BEGIN@7 which was called:
# once (7µs+39µs) by YAML::PP::Dumper::BEGIN@10 at line 7 # spent 46µs making 1 call to YAML::PP::Representer::BEGIN@7
# spent 39µs making 1 call to Exporter::import |
8 | |||||
9 | 1 | 1µs | # spent 60µs (9+51) within YAML::PP::Representer::BEGIN@9 which was called:
# once (9µs+51µs) by YAML::PP::Dumper::BEGIN@10 at line 17 | ||
10 | YAML_PLAIN_SCALAR_STYLE YAML_SINGLE_QUOTED_SCALAR_STYLE | ||||
11 | YAML_DOUBLE_QUOTED_SCALAR_STYLE | ||||
12 | YAML_ANY_SCALAR_STYLE | ||||
13 | YAML_LITERAL_SCALAR_STYLE YAML_FOLDED_SCALAR_STYLE | ||||
14 | YAML_FLOW_SEQUENCE_STYLE YAML_FLOW_MAPPING_STYLE | ||||
15 | YAML_BLOCK_MAPPING_STYLE YAML_BLOCK_SEQUENCE_STYLE | ||||
16 | PRESERVE_ORDER PRESERVE_SCALAR_STYLE PRESERVE_FLOW_STYLE PRESERVE_ALIAS | ||||
17 | 1 | 24µs | 2 | 111µs | /; # spent 60µs making 1 call to YAML::PP::Representer::BEGIN@9
# spent 51µs making 1 call to Exporter::import |
18 | 2 | 1.08ms | 2 | 13µs | # spent 10µs (7+3) within YAML::PP::Representer::BEGIN@18 which was called:
# once (7µs+3µs) by YAML::PP::Dumper::BEGIN@10 at line 18 # spent 10µs making 1 call to YAML::PP::Representer::BEGIN@18
# spent 3µs making 1 call to B::import |
19 | |||||
20 | # spent 6µs within YAML::PP::Representer::new which was called:
# once (6µs+0s) by YAML::PP::Dumper::new at line 46 of YAML/PP/Dumper.pm | ||||
21 | 1 | 1µs | my ($class, %args) = @_; | ||
22 | 1 | 1µs | my $preserve = delete $args{preserve} || 0; | ||
23 | 1 | 1µs | if ($preserve == 1) { | ||
24 | $preserve = PRESERVE_ORDER | PRESERVE_SCALAR_STYLE | PRESERVE_FLOW_STYLE | PRESERVE_ALIAS; | ||||
25 | } | ||||
26 | my $self = bless { | ||||
27 | schema => delete $args{schema}, | ||||
28 | 1 | 1µs | preserve => $preserve, | ||
29 | }, $class; | ||||
30 | 1 | 0s | if (keys %args) { | ||
31 | die "Unexpected arguments: " . join ', ', sort keys %args; | ||||
32 | } | ||||
33 | 1 | 6µs | return $self; | ||
34 | } | ||||
35 | |||||
36 | sub clone { | ||||
37 | my ($self) = @_; | ||||
38 | my $clone = { | ||||
39 | schema => $self->schema, | ||||
40 | preserve => $self->{preserve}, | ||||
41 | }; | ||||
42 | return bless $clone, ref $self; | ||||
43 | } | ||||
44 | |||||
45 | sub schema { return $_[0]->{schema} } | ||||
46 | sub preserve_order { return $_[0]->{preserve} & PRESERVE_ORDER } | ||||
47 | sub preserve_scalar_style { return $_[0]->{preserve} & PRESERVE_SCALAR_STYLE } | ||||
48 | sub preserve_flow_style { return $_[0]->{preserve} & PRESERVE_FLOW_STYLE } | ||||
49 | sub preserve_alias { return $_[0]->{preserve} & PRESERVE_ALIAS } | ||||
50 | |||||
51 | sub represent_node { | ||||
52 | my ($self, $node) = @_; | ||||
53 | |||||
54 | my $preserve_alias = $self->preserve_alias; | ||||
55 | my $preserve_style = $self->preserve_scalar_style; | ||||
56 | if ($preserve_style or $preserve_alias) { | ||||
57 | if (ref $node->{value} eq 'YAML::PP::Preserve::Scalar') { | ||||
58 | my $value = $node->{value}->value; | ||||
59 | if ($preserve_style) { | ||||
60 | $node->{style} = $node->{value}->style; | ||||
61 | } | ||||
62 | # $node->{tag} = $node->{value}->tag; | ||||
63 | $node->{value} = $value; | ||||
64 | } | ||||
65 | } | ||||
66 | $node->{reftype} = reftype($node->{value}); | ||||
67 | if (not $node->{reftype} and reftype(\$node->{value}) eq 'GLOB') { | ||||
68 | $node->{reftype} = 'GLOB'; | ||||
69 | } | ||||
70 | |||||
71 | if ($node->{reftype}) { | ||||
72 | $self->_represent_noderef($node); | ||||
73 | } | ||||
74 | else { | ||||
75 | $self->_represent_node_nonref($node); | ||||
76 | } | ||||
77 | $node->{reftype} = (reftype $node->{data}) || ''; | ||||
78 | |||||
79 | if ($node->{reftype} eq 'HASH' and my $tied = tied(%{ $node->{data} })) { | ||||
80 | my $representers = $self->schema->representers; | ||||
81 | $tied = ref $tied; | ||||
82 | if (my $def = $representers->{tied_equals}->{ $tied }) { | ||||
83 | my $code = $def->{code}; | ||||
84 | my $done = $code->($self, $node); | ||||
85 | } | ||||
86 | } | ||||
87 | |||||
88 | if ($node->{reftype} eq 'HASH') { | ||||
89 | unless (defined $node->{items}) { | ||||
90 | # by default we sort hash keys | ||||
91 | my @keys; | ||||
92 | if ($self->preserve_order) { | ||||
93 | @keys = keys %{ $node->{data} }; | ||||
94 | } | ||||
95 | else { | ||||
96 | @keys = sort keys %{ $node->{data} }; | ||||
97 | } | ||||
98 | for my $key (@keys) { | ||||
99 | push @{ $node->{items} }, $key, $node->{data}->{ $key }; | ||||
100 | } | ||||
101 | } | ||||
102 | my %args; | ||||
103 | if ($self->preserve_flow_style and reftype $node->{value} eq 'HASH') { | ||||
104 | if (my $tied = tied %{ $node->{value} } ) { | ||||
105 | $args{style} = $tied->{style}; | ||||
106 | } | ||||
107 | } | ||||
108 | return [ mapping => $node, %args ]; | ||||
109 | } | ||||
110 | elsif ($node->{reftype} eq 'ARRAY') { | ||||
111 | unless (defined $node->{items}) { | ||||
112 | @{ $node->{items} } = @{ $node->{data} }; | ||||
113 | } | ||||
114 | my %args; | ||||
115 | if ($self->preserve_flow_style and reftype $node->{value} eq 'ARRAY') { | ||||
116 | if (my $tied = tied @{ $node->{value} } ) { | ||||
117 | $args{style} = $tied->{style}; | ||||
118 | } | ||||
119 | } | ||||
120 | return [ sequence => $node, %args ]; | ||||
121 | } | ||||
122 | elsif ($node->{reftype}) { | ||||
123 | die "Cannot handle reftype '$node->{reftype}' (you might want to enable YAML::PP::Schema::Perl)"; | ||||
124 | } | ||||
125 | else { | ||||
126 | unless (defined $node->{items}) { | ||||
127 | $node->{items} = [$node->{data}]; | ||||
128 | } | ||||
129 | return [ scalar => $node ]; | ||||
130 | } | ||||
131 | |||||
132 | } | ||||
133 | |||||
134 | 1 | 0s | my $bool_code = <<'EOM'; | ||
135 | sub { | ||||
136 | my ($x) = @_; | ||||
137 | use experimental qw/ builtin /; | ||||
138 | builtin::is_bool($x); | ||||
139 | } | ||||
140 | EOM | ||||
141 | 1 | 1µs | my $is_bool; | ||
142 | |||||
143 | sub _represent_node_nonref { | ||||
144 | my ($self, $node) = @_; | ||||
145 | my $representers = $self->schema->representers; | ||||
146 | |||||
147 | if (not defined $node->{value}) { | ||||
148 | if (my $undef = $representers->{undef}) { | ||||
149 | return 1 if $undef->($self, $node); | ||||
150 | } | ||||
151 | else { | ||||
152 | $node->{style} = YAML_SINGLE_QUOTED_SCALAR_STYLE; | ||||
153 | $node->{data} = ''; | ||||
154 | return 1; | ||||
155 | } | ||||
156 | } | ||||
157 | if ($] >= 5.036000 and my $rep = $representers->{bool}) { | ||||
158 | $is_bool ||= eval $bool_code; | ||||
159 | if ($is_bool->($node->{value})) { | ||||
160 | return $rep->{code}->($self, $node); | ||||
161 | } | ||||
162 | } | ||||
163 | for my $rep (@{ $representers->{flags} }) { | ||||
164 | my $check_flags = $rep->{flags}; | ||||
165 | my $flags = B::svref_2object(\$node->{value})->FLAGS; | ||||
166 | if ($flags & $check_flags) { | ||||
167 | return 1 if $rep->{code}->($self, $node); | ||||
168 | } | ||||
169 | |||||
170 | } | ||||
171 | if (my $rep = $representers->{equals}->{ $node->{value} }) { | ||||
172 | return 1 if $rep->{code}->($self, $node); | ||||
173 | } | ||||
174 | for my $rep (@{ $representers->{regex} }) { | ||||
175 | if ($node->{value} =~ $rep->{regex}) { | ||||
176 | return 1 if $rep->{code}->($self, $node); | ||||
177 | } | ||||
178 | } | ||||
179 | unless (defined $node->{data}) { | ||||
180 | $node->{data} = $node->{value}; | ||||
181 | } | ||||
182 | unless (defined $node->{style}) { | ||||
183 | $node->{style} = YAML_ANY_SCALAR_STYLE; | ||||
184 | $node->{style} = ""; | ||||
185 | } | ||||
186 | } | ||||
187 | |||||
188 | sub _represent_noderef { | ||||
189 | my ($self, $node) = @_; | ||||
190 | my $representers = $self->schema->representers; | ||||
191 | |||||
192 | if (my $classname = blessed($node->{value})) { | ||||
193 | if (my $def = $representers->{class_equals}->{ $classname }) { | ||||
194 | my $code = $def->{code}; | ||||
195 | return 1 if $code->($self, $node); | ||||
196 | } | ||||
197 | for my $matches (@{ $representers->{class_matches} }) { | ||||
198 | my ($re, $code) = @$matches; | ||||
199 | if (ref $re and $classname =~ $re or $re) { | ||||
200 | return 1 if $code->($self, $node); | ||||
201 | } | ||||
202 | } | ||||
203 | for my $isa (@{ $representers->{class_isa} }) { | ||||
204 | my ($class_name, $code) = @$isa; | ||||
205 | if ($node->{ value }->isa($class_name)) { | ||||
206 | return 1 if $code->($self, $node); | ||||
207 | } | ||||
208 | } | ||||
209 | } | ||||
210 | if ($node->{reftype} eq 'SCALAR' and my $scalarref = $representers->{scalarref}) { | ||||
211 | my $code = $scalarref->{code}; | ||||
212 | return 1 if $code->($self, $node); | ||||
213 | } | ||||
214 | if ($node->{reftype} eq 'REF' and my $refref = $representers->{refref}) { | ||||
215 | my $code = $refref->{code}; | ||||
216 | return 1 if $code->($self, $node); | ||||
217 | } | ||||
218 | if ($node->{reftype} eq 'CODE' and my $coderef = $representers->{coderef}) { | ||||
219 | my $code = $coderef->{code}; | ||||
220 | return 1 if $code->($self, $node); | ||||
221 | } | ||||
222 | if ($node->{reftype} eq 'GLOB' and my $glob = $representers->{glob}) { | ||||
223 | my $code = $glob->{code}; | ||||
224 | return 1 if $code->($self, $node); | ||||
225 | } | ||||
226 | $node->{data} = $node->{value}; | ||||
227 | |||||
228 | } | ||||
229 | |||||
230 | 1 | 4µs | 1; |