Filename | /usr/local/lib/perl5/5.24/mach/Data/Dumper.pm |
Statements | Executed 42 statements in 11.3ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 70µs | 417µs | BEGIN@24 | Data::Dumper::
1 | 1 | 1 | 43µs | 249µs | BEGIN@279 | Data::Dumper::
1 | 1 | 1 | 36µs | 315µs | BEGIN@22 | Data::Dumper::
1 | 1 | 1 | 36µs | 36µs | BEGIN@12 | Data::Dumper::
1 | 1 | 1 | 34µs | 44µs | BEGIN@763 | Data::Dumper::
1 | 1 | 1 | 24µs | 24µs | BEGIN@18 | Data::Dumper::
1 | 1 | 1 | 8µs | 8µs | _vstring (xsub) | Data::Dumper::
1 | 1 | 1 | 6µs | 6µs | CORE:qr (opcode) | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Bless | Data::Dumper::
0 | 0 | 0 | 0s | 0s | DESTROY | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Deepcopy | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Deparse | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Dump | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Dumper | Data::Dumper::
0 | 0 | 0 | 0s | 0s | DumperX | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Dumpperl | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Freezer | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Indent | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Maxdepth | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Maxrecurse | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Names | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Pad | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Pair | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Purity | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Quotekeys | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Reset | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Seen | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Sortkeys | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Sparseseen | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Terse | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Toaster | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Trailingcomma | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Useperl | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Useqq | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Values | Data::Dumper::
0 | 0 | 0 | 0s | 0s | Varname | Data::Dumper::
0 | 0 | 0 | 0s | 0s | _compose_out | Data::Dumper::
0 | 0 | 0 | 0s | 0s | _dump | Data::Dumper::
0 | 0 | 0 | 0s | 0s | _quote | Data::Dumper::
0 | 0 | 0 | 0s | 0s | _refine_name | Data::Dumper::
0 | 0 | 0 | 0s | 0s | _sortkeys | Data::Dumper::
0 | 0 | 0 | 0s | 0s | format_refaddr | Data::Dumper::
0 | 0 | 0 | 0s | 0s | init_refaddr_format | Data::Dumper::
0 | 0 | 0 | 0s | 0s | new | Data::Dumper::
0 | 0 | 0 | 0s | 0s | qquote | Data::Dumper::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | # | ||||
2 | # Data/Dumper.pm | ||||
3 | # | ||||
4 | # convert perl data structures into perl syntax suitable for both printing | ||||
5 | # and eval | ||||
6 | # | ||||
7 | # Documentation at the __END__ | ||||
8 | # | ||||
9 | |||||
10 | package Data::Dumper; | ||||
11 | |||||
12 | # spent 36µs within Data::Dumper::BEGIN@12 which was called:
# once (36µs+0s) by Razor2::String::BEGIN@7 at line 14 | ||||
13 | 1 | 17µs | $VERSION = '2.160'; # Don't forget to set version and release | ||
14 | 1 | 41µs | 1 | 36µs | } # date in POD below! # spent 36µs making 1 call to Data::Dumper::BEGIN@12 |
15 | |||||
16 | #$| = 1; | ||||
17 | |||||
18 | 2 | 120µs | 1 | 24µs | # spent 24µs within Data::Dumper::BEGIN@18 which was called:
# once (24µs+0s) by Razor2::String::BEGIN@7 at line 18 # spent 24µs making 1 call to Data::Dumper::BEGIN@18 |
19 | 1 | 2µs | require Exporter; | ||
20 | 1 | 2µs | require overload; | ||
21 | |||||
22 | 2 | 203µs | 2 | 594µs | # spent 315µs (36+279) within Data::Dumper::BEGIN@22 which was called:
# once (36µs+279µs) by Razor2::String::BEGIN@7 at line 22 # spent 315µs making 1 call to Data::Dumper::BEGIN@22
# spent 279µs making 1 call to Exporter::import |
23 | |||||
24 | # spent 417µs (70+347) within Data::Dumper::BEGIN@24 which was called:
# once (70µs+347µs) by Razor2::String::BEGIN@7 at line 38 | ||||
25 | 1 | 18µs | @ISA = qw(Exporter); | ||
26 | 1 | 2µs | @EXPORT = qw(Dumper); | ||
27 | 1 | 2µs | @EXPORT_OK = qw(DumperX); | ||
28 | |||||
29 | # if run under miniperl, or otherwise lacking dynamic loading, | ||||
30 | # XSLoader should be attempted to load, or the pure perl flag | ||||
31 | # toggled on load failure. | ||||
32 | 1 | 14µs | eval { | ||
33 | 1 | 13µs | require XSLoader; | ||
34 | 1 | 367µs | 1 | 347µs | XSLoader::load( 'Data::Dumper' ); # spent 347µs making 1 call to XSLoader::load |
35 | 1 | 3µs | 1 | ||
36 | } | ||||
37 | or $Useperl = 1; | ||||
38 | 1 | 2.41ms | 1 | 417µs | } # spent 417µs making 1 call to Data::Dumper::BEGIN@24 |
39 | |||||
40 | 1 | 3µs | my $IS_ASCII = ord 'A' == 65; | ||
41 | |||||
42 | # module vars and their defaults | ||||
43 | 1 | 2µs | $Indent = 2 unless defined $Indent; | ||
44 | 1 | 2µs | $Trailingcomma = 0 unless defined $Trailingcomma; | ||
45 | 1 | 5µs | $Purity = 0 unless defined $Purity; | ||
46 | 1 | 2µs | $Pad = "" unless defined $Pad; | ||
47 | 1 | 2µs | $Varname = "VAR" unless defined $Varname; | ||
48 | 1 | 2µs | $Useqq = 0 unless defined $Useqq; | ||
49 | 1 | 2µs | $Terse = 0 unless defined $Terse; | ||
50 | 1 | 2µs | $Freezer = "" unless defined $Freezer; | ||
51 | 1 | 2µs | $Toaster = "" unless defined $Toaster; | ||
52 | 1 | 2µs | $Deepcopy = 0 unless defined $Deepcopy; | ||
53 | 1 | 1µs | $Quotekeys = 1 unless defined $Quotekeys; | ||
54 | 1 | 2µs | $Bless = "bless" unless defined $Bless; | ||
55 | #$Expdepth = 0 unless defined $Expdepth; | ||||
56 | 1 | 2µs | $Maxdepth = 0 unless defined $Maxdepth; | ||
57 | 1 | 2µs | $Pair = ' => ' unless defined $Pair; | ||
58 | 1 | 2µs | $Useperl = 0 unless defined $Useperl; | ||
59 | 1 | 1µs | $Sortkeys = 0 unless defined $Sortkeys; | ||
60 | 1 | 2µs | $Deparse = 0 unless defined $Deparse; | ||
61 | 1 | 2µs | $Sparseseen = 0 unless defined $Sparseseen; | ||
62 | 1 | 2µs | $Maxrecurse = 1000 unless defined $Maxrecurse; | ||
63 | |||||
64 | # | ||||
65 | # expects an arrayref of values to be dumped. | ||||
66 | # can optionally pass an arrayref of names for the values. | ||||
67 | # names must have leading $ sign stripped. begin the name with * | ||||
68 | # to cause output of arrays and hashes rather than refs. | ||||
69 | # | ||||
70 | sub new { | ||||
71 | my($c, $v, $n) = @_; | ||||
72 | |||||
73 | croak "Usage: PACKAGE->new(ARRAYREF, [ARRAYREF])" | ||||
74 | unless (defined($v) && (ref($v) eq 'ARRAY')); | ||||
75 | $n = [] unless (defined($n) && (ref($n) eq 'ARRAY')); | ||||
76 | |||||
77 | my($s) = { | ||||
78 | level => 0, # current recursive depth | ||||
79 | indent => $Indent, # various styles of indenting | ||||
80 | trailingcomma => $Trailingcomma, # whether to add comma after last elem | ||||
81 | pad => $Pad, # all lines prefixed by this string | ||||
82 | xpad => "", # padding-per-level | ||||
83 | apad => "", # added padding for hash keys n such | ||||
84 | sep => "", # list separator | ||||
85 | pair => $Pair, # hash key/value separator: defaults to ' => ' | ||||
86 | seen => {}, # local (nested) refs (id => [name, val]) | ||||
87 | todump => $v, # values to dump [] | ||||
88 | names => $n, # optional names for values [] | ||||
89 | varname => $Varname, # prefix to use for tagging nameless ones | ||||
90 | purity => $Purity, # degree to which output is evalable | ||||
91 | useqq => $Useqq, # use "" for strings (backslashitis ensues) | ||||
92 | terse => $Terse, # avoid name output (where feasible) | ||||
93 | freezer => $Freezer, # name of Freezer method for objects | ||||
94 | toaster => $Toaster, # name of method to revive objects | ||||
95 | deepcopy => $Deepcopy, # do not cross-ref, except to stop recursion | ||||
96 | quotekeys => $Quotekeys, # quote hash keys | ||||
97 | 'bless' => $Bless, # keyword to use for "bless" | ||||
98 | # expdepth => $Expdepth, # cutoff depth for explicit dumping | ||||
99 | maxdepth => $Maxdepth, # depth beyond which we give up | ||||
100 | maxrecurse => $Maxrecurse, # depth beyond which we abort | ||||
101 | useperl => $Useperl, # use the pure Perl implementation | ||||
102 | sortkeys => $Sortkeys, # flag or filter for sorting hash keys | ||||
103 | deparse => $Deparse, # use B::Deparse for coderefs | ||||
104 | noseen => $Sparseseen, # do not populate the seen hash unless necessary | ||||
105 | }; | ||||
106 | |||||
107 | if ($Indent > 0) { | ||||
108 | $s->{xpad} = " "; | ||||
109 | $s->{sep} = "\n"; | ||||
110 | } | ||||
111 | return bless($s, $c); | ||||
112 | } | ||||
113 | |||||
114 | # Packed numeric addresses take less memory. Plus pack is faster than sprintf | ||||
115 | |||||
116 | # Most users of current versions of Data::Dumper will be 5.008 or later. | ||||
117 | # Anyone on 5.6.1 and 5.6.2 upgrading will be rare (particularly judging by | ||||
118 | # the bug reports from users on those platforms), so for the common case avoid | ||||
119 | # complexity, and avoid even compiling the unneeded code. | ||||
120 | |||||
121 | sub init_refaddr_format { | ||||
122 | } | ||||
123 | |||||
124 | sub format_refaddr { | ||||
125 | require Scalar::Util; | ||||
126 | pack "J", Scalar::Util::refaddr(shift); | ||||
127 | }; | ||||
128 | |||||
129 | 1 | 14µs | if ($] < 5.008) { | ||
130 | eval <<'EOC' or die; | ||||
131 | no warnings 'redefine'; | ||||
132 | my $refaddr_format; | ||||
133 | sub init_refaddr_format { | ||||
134 | require Config; | ||||
135 | my $f = $Config::Config{uvxformat}; | ||||
136 | $f =~ tr/"//d; | ||||
137 | $refaddr_format = "0x%" . $f; | ||||
138 | } | ||||
139 | |||||
140 | sub format_refaddr { | ||||
141 | require Scalar::Util; | ||||
142 | sprintf $refaddr_format, Scalar::Util::refaddr(shift); | ||||
143 | } | ||||
144 | |||||
145 | 1 | ||||
146 | EOC | ||||
147 | } | ||||
148 | |||||
149 | # | ||||
150 | # add-to or query the table of already seen references | ||||
151 | # | ||||
152 | sub Seen { | ||||
153 | my($s, $g) = @_; | ||||
154 | if (defined($g) && (ref($g) eq 'HASH')) { | ||||
155 | init_refaddr_format(); | ||||
156 | my($k, $v, $id); | ||||
157 | while (($k, $v) = each %$g) { | ||||
158 | if (defined $v) { | ||||
159 | if (ref $v) { | ||||
160 | $id = format_refaddr($v); | ||||
161 | if ($k =~ /^[*](.*)$/) { | ||||
162 | $k = (ref $v eq 'ARRAY') ? ( "\\\@" . $1 ) : | ||||
163 | (ref $v eq 'HASH') ? ( "\\\%" . $1 ) : | ||||
164 | (ref $v eq 'CODE') ? ( "\\\&" . $1 ) : | ||||
165 | ( "\$" . $1 ) ; | ||||
166 | } | ||||
167 | elsif ($k !~ /^\$/) { | ||||
168 | $k = "\$" . $k; | ||||
169 | } | ||||
170 | $s->{seen}{$id} = [$k, $v]; | ||||
171 | } | ||||
172 | else { | ||||
173 | carp "Only refs supported, ignoring non-ref item \$$k"; | ||||
174 | } | ||||
175 | } | ||||
176 | else { | ||||
177 | carp "Value of ref must be defined; ignoring undefined item \$$k"; | ||||
178 | } | ||||
179 | } | ||||
180 | return $s; | ||||
181 | } | ||||
182 | else { | ||||
183 | return map { @$_ } values %{$s->{seen}}; | ||||
184 | } | ||||
185 | } | ||||
186 | |||||
187 | # | ||||
188 | # set or query the values to be dumped | ||||
189 | # | ||||
190 | sub Values { | ||||
191 | my($s, $v) = @_; | ||||
192 | if (defined($v)) { | ||||
193 | if (ref($v) eq 'ARRAY') { | ||||
194 | $s->{todump} = [@$v]; # make a copy | ||||
195 | return $s; | ||||
196 | } | ||||
197 | else { | ||||
198 | croak "Argument to Values, if provided, must be array ref"; | ||||
199 | } | ||||
200 | } | ||||
201 | else { | ||||
202 | return @{$s->{todump}}; | ||||
203 | } | ||||
204 | } | ||||
205 | |||||
206 | # | ||||
207 | # set or query the names of the values to be dumped | ||||
208 | # | ||||
209 | sub Names { | ||||
210 | my($s, $n) = @_; | ||||
211 | if (defined($n)) { | ||||
212 | if (ref($n) eq 'ARRAY') { | ||||
213 | $s->{names} = [@$n]; # make a copy | ||||
214 | return $s; | ||||
215 | } | ||||
216 | else { | ||||
217 | croak "Argument to Names, if provided, must be array ref"; | ||||
218 | } | ||||
219 | } | ||||
220 | else { | ||||
221 | return @{$s->{names}}; | ||||
222 | } | ||||
223 | } | ||||
224 | |||||
225 | sub DESTROY {} | ||||
226 | |||||
227 | sub Dump { | ||||
228 | return &Dumpxs | ||||
229 | unless $Data::Dumper::Useperl || (ref($_[0]) && $_[0]->{useperl}) | ||||
230 | || $Data::Dumper::Deparse || (ref($_[0]) && $_[0]->{deparse}) | ||||
231 | |||||
232 | # Use pure perl version on earlier releases on EBCDIC platforms | ||||
233 | || (! $IS_ASCII && $] lt 5.021_010); | ||||
234 | return &Dumpperl; | ||||
235 | } | ||||
236 | |||||
237 | # | ||||
238 | # dump the refs in the current dumper object. | ||||
239 | # expects same args as new() if called via package name. | ||||
240 | # | ||||
241 | sub Dumpperl { | ||||
242 | my($s) = shift; | ||||
243 | my(@out, $val, $name); | ||||
244 | my($i) = 0; | ||||
245 | local(@post); | ||||
246 | init_refaddr_format(); | ||||
247 | |||||
248 | $s = $s->new(@_) unless ref $s; | ||||
249 | |||||
250 | for $val (@{$s->{todump}}) { | ||||
251 | @post = (); | ||||
252 | $name = $s->{names}[$i++]; | ||||
253 | $name = $s->_refine_name($name, $val, $i); | ||||
254 | |||||
255 | my $valstr; | ||||
256 | { | ||||
257 | local($s->{apad}) = $s->{apad}; | ||||
258 | $s->{apad} .= ' ' x (length($name) + 3) if $s->{indent} >= 2 and !$s->{terse}; | ||||
259 | $valstr = $s->_dump($val, $name); | ||||
260 | } | ||||
261 | |||||
262 | $valstr = "$name = " . $valstr . ';' if @post or !$s->{terse}; | ||||
263 | my $out = $s->_compose_out($valstr, \@post); | ||||
264 | |||||
265 | push @out, $out; | ||||
266 | } | ||||
267 | return wantarray ? @out : join('', @out); | ||||
268 | } | ||||
269 | |||||
270 | # wrap string in single quotes (escaping if needed) | ||||
271 | sub _quote { | ||||
272 | my $val = shift; | ||||
273 | $val =~ s/([\\\'])/\\$1/g; | ||||
274 | return "'" . $val . "'"; | ||||
275 | } | ||||
276 | |||||
277 | # Old Perls (5.14-) have trouble resetting vstring magic when it is no | ||||
278 | # longer valid. | ||||
279 | 2 | 6.42ms | 3 | 454µs | # spent 249µs (43+206) within Data::Dumper::BEGIN@279 which was called:
# once (43µs+206µs) by Razor2::String::BEGIN@7 at line 279 # spent 249µs making 1 call to Data::Dumper::BEGIN@279
# spent 198µs making 1 call to constant::import
# spent 8µs making 1 call to Data::Dumper::_vstring |
280 | |||||
281 | # | ||||
282 | # twist, toil and turn; | ||||
283 | # and recurse, of course. | ||||
284 | # sometimes sordidly; | ||||
285 | # and curse if no recourse. | ||||
286 | # | ||||
287 | sub _dump { | ||||
288 | my($s, $val, $name) = @_; | ||||
289 | my($out, $type, $id, $sname); | ||||
290 | |||||
291 | $type = ref $val; | ||||
292 | $out = ""; | ||||
293 | |||||
294 | if ($type) { | ||||
295 | |||||
296 | # Call the freezer method if it's specified and the object has the | ||||
297 | # method. Trap errors and warn() instead of die()ing, like the XS | ||||
298 | # implementation. | ||||
299 | my $freezer = $s->{freezer}; | ||||
300 | if ($freezer and UNIVERSAL::can($val, $freezer)) { | ||||
301 | eval { $val->$freezer() }; | ||||
302 | warn "WARNING(Freezer method call failed): $@" if $@; | ||||
303 | } | ||||
304 | |||||
305 | require Scalar::Util; | ||||
306 | my $realpack = Scalar::Util::blessed($val); | ||||
307 | my $realtype = $realpack ? Scalar::Util::reftype($val) : ref $val; | ||||
308 | $id = format_refaddr($val); | ||||
309 | |||||
310 | # Note: By this point $name is always defined and of non-zero length. | ||||
311 | # Keep a tab on it so that we do not fall into recursive pit. | ||||
312 | if (exists $s->{seen}{$id}) { | ||||
313 | if ($s->{purity} and $s->{level} > 0) { | ||||
314 | $out = ($realtype eq 'HASH') ? '{}' : | ||||
315 | ($realtype eq 'ARRAY') ? '[]' : | ||||
316 | 'do{my $o}' ; | ||||
317 | push @post, $name . " = " . $s->{seen}{$id}[0]; | ||||
318 | } | ||||
319 | else { | ||||
320 | $out = $s->{seen}{$id}[0]; | ||||
321 | if ($name =~ /^([\@\%])/) { | ||||
322 | my $start = $1; | ||||
323 | if ($out =~ /^\\$start/) { | ||||
324 | $out = substr($out, 1); | ||||
325 | } | ||||
326 | else { | ||||
327 | $out = $start . '{' . $out . '}'; | ||||
328 | } | ||||
329 | } | ||||
330 | } | ||||
331 | return $out; | ||||
332 | } | ||||
333 | else { | ||||
334 | # store our name | ||||
335 | $s->{seen}{$id} = [ ( | ||||
336 | ($name =~ /^[@%]/) | ||||
337 | ? ('\\' . $name ) | ||||
338 | : ($realtype eq 'CODE' and $name =~ /^[*](.*)$/) | ||||
339 | ? ('\\&' . $1 ) | ||||
340 | : $name | ||||
341 | ), $val ]; | ||||
342 | } | ||||
343 | my $no_bless = 0; | ||||
344 | my $is_regex = 0; | ||||
345 | if ( $realpack and ($] >= 5.009005 ? re::is_regexp($val) : $realpack eq 'Regexp') ) { | ||||
346 | $is_regex = 1; | ||||
347 | $no_bless = $realpack eq 'Regexp'; | ||||
348 | } | ||||
349 | |||||
350 | # If purity is not set and maxdepth is set, then check depth: | ||||
351 | # if we have reached maximum depth, return the string | ||||
352 | # representation of the thing we are currently examining | ||||
353 | # at this depth (i.e., 'Foo=ARRAY(0xdeadbeef)'). | ||||
354 | if (!$s->{purity} | ||||
355 | and defined($s->{maxdepth}) | ||||
356 | and $s->{maxdepth} > 0 | ||||
357 | and $s->{level} >= $s->{maxdepth}) | ||||
358 | { | ||||
359 | return qq['$val']; | ||||
360 | } | ||||
361 | |||||
362 | # avoid recursing infinitely [perl #122111] | ||||
363 | if ($s->{maxrecurse} > 0 | ||||
364 | and $s->{level} >= $s->{maxrecurse}) { | ||||
365 | die "Recursion limit of $s->{maxrecurse} exceeded"; | ||||
366 | } | ||||
367 | |||||
368 | # we have a blessed ref | ||||
369 | my ($blesspad); | ||||
370 | if ($realpack and !$no_bless) { | ||||
371 | $out = $s->{'bless'} . '( '; | ||||
372 | $blesspad = $s->{apad}; | ||||
373 | $s->{apad} .= ' ' if ($s->{indent} >= 2); | ||||
374 | } | ||||
375 | |||||
376 | $s->{level}++; | ||||
377 | my $ipad = $s->{xpad} x $s->{level}; | ||||
378 | |||||
379 | if ($is_regex) { | ||||
380 | my $pat; | ||||
381 | my $flags = ""; | ||||
382 | if (defined(*re::regexp_pattern{CODE})) { | ||||
383 | ($pat, $flags) = re::regexp_pattern($val); | ||||
384 | } | ||||
385 | else { | ||||
386 | $pat = "$val"; | ||||
387 | } | ||||
388 | $pat =~ s <(\\.)|/> { $1 || '\\/' }ge; | ||||
389 | $out .= "qr/$pat/$flags"; | ||||
390 | } | ||||
391 | elsif ($realtype eq 'SCALAR' || $realtype eq 'REF' | ||||
392 | || $realtype eq 'VSTRING') { | ||||
393 | if ($realpack) { | ||||
394 | $out .= 'do{\\(my $o = ' . $s->_dump($$val, "\${$name}") . ')}'; | ||||
395 | } | ||||
396 | else { | ||||
397 | $out .= '\\' . $s->_dump($$val, "\${$name}"); | ||||
398 | } | ||||
399 | } | ||||
400 | elsif ($realtype eq 'GLOB') { | ||||
401 | $out .= '\\' . $s->_dump($$val, "*{$name}"); | ||||
402 | } | ||||
403 | elsif ($realtype eq 'ARRAY') { | ||||
404 | my($pad, $mname); | ||||
405 | my($i) = 0; | ||||
406 | $out .= ($name =~ /^\@/) ? '(' : '['; | ||||
407 | $pad = $s->{sep} . $s->{pad} . $s->{apad}; | ||||
408 | ($name =~ /^\@(.*)$/) ? ($mname = "\$" . $1) : | ||||
409 | # omit -> if $foo->[0]->{bar}, but not ${$foo->[0]}->{bar} | ||||
410 | ($name =~ /^\\?[\%\@\*\$][^{].*[]}]$/) ? ($mname = $name) : | ||||
411 | ($mname = $name . '->'); | ||||
412 | $mname .= '->' if $mname =~ /^\*.+\{[A-Z]+\}$/; | ||||
413 | for my $v (@$val) { | ||||
414 | $sname = $mname . '[' . $i . ']'; | ||||
415 | $out .= $pad . $ipad . '#' . $i | ||||
416 | if $s->{indent} >= 3; | ||||
417 | $out .= $pad . $ipad . $s->_dump($v, $sname); | ||||
418 | $out .= "," | ||||
419 | if $i++ < $#$val | ||||
420 | || ($s->{trailingcomma} && $s->{indent} >= 1); | ||||
421 | } | ||||
422 | $out .= $pad . ($s->{xpad} x ($s->{level} - 1)) if $i; | ||||
423 | $out .= ($name =~ /^\@/) ? ')' : ']'; | ||||
424 | } | ||||
425 | elsif ($realtype eq 'HASH') { | ||||
426 | my ($k, $v, $pad, $lpad, $mname, $pair); | ||||
427 | $out .= ($name =~ /^\%/) ? '(' : '{'; | ||||
428 | $pad = $s->{sep} . $s->{pad} . $s->{apad}; | ||||
429 | $lpad = $s->{apad}; | ||||
430 | $pair = $s->{pair}; | ||||
431 | ($name =~ /^\%(.*)$/) ? ($mname = "\$" . $1) : | ||||
432 | # omit -> if $foo->[0]->{bar}, but not ${$foo->[0]}->{bar} | ||||
433 | ($name =~ /^\\?[\%\@\*\$][^{].*[]}]$/) ? ($mname = $name) : | ||||
434 | ($mname = $name . '->'); | ||||
435 | $mname .= '->' if $mname =~ /^\*.+\{[A-Z]+\}$/; | ||||
436 | my $sortkeys = defined($s->{sortkeys}) ? $s->{sortkeys} : ''; | ||||
437 | my $keys = []; | ||||
438 | if ($sortkeys) { | ||||
439 | if (ref($s->{sortkeys}) eq 'CODE') { | ||||
440 | $keys = $s->{sortkeys}($val); | ||||
441 | unless (ref($keys) eq 'ARRAY') { | ||||
442 | carp "Sortkeys subroutine did not return ARRAYREF"; | ||||
443 | $keys = []; | ||||
444 | } | ||||
445 | } | ||||
446 | else { | ||||
447 | $keys = [ sort keys %$val ]; | ||||
448 | } | ||||
449 | } | ||||
450 | |||||
451 | # Ensure hash iterator is reset | ||||
452 | keys(%$val); | ||||
453 | |||||
454 | my $key; | ||||
455 | while (($k, $v) = ! $sortkeys ? (each %$val) : | ||||
456 | @$keys ? ($key = shift(@$keys), $val->{$key}) : | ||||
457 | () ) | ||||
458 | { | ||||
459 | my $nk = $s->_dump($k, ""); | ||||
460 | |||||
461 | # _dump doesn't quote numbers of this form | ||||
462 | if ($s->{quotekeys} && $nk =~ /^(?:0|-?[1-9][0-9]{0,8})\z/) { | ||||
463 | $nk = $s->{useqq} ? qq("$nk") : qq('$nk'); | ||||
464 | } | ||||
465 | elsif (!$s->{quotekeys} and $nk =~ /^[\"\']([A-Za-z_]\w*)[\"\']$/) { | ||||
466 | $nk = $1 | ||||
467 | } | ||||
468 | |||||
469 | $sname = $mname . '{' . $nk . '}'; | ||||
470 | $out .= $pad . $ipad . $nk . $pair; | ||||
471 | |||||
472 | # temporarily alter apad | ||||
473 | $s->{apad} .= (" " x (length($nk) + 4)) | ||||
474 | if $s->{indent} >= 2; | ||||
475 | $out .= $s->_dump($val->{$k}, $sname) . ","; | ||||
476 | $s->{apad} = $lpad | ||||
477 | if $s->{indent} >= 2; | ||||
478 | } | ||||
479 | if (substr($out, -1) eq ',') { | ||||
480 | chop $out if !$s->{trailingcomma} || !$s->{indent}; | ||||
481 | $out .= $pad . ($s->{xpad} x ($s->{level} - 1)); | ||||
482 | } | ||||
483 | $out .= ($name =~ /^\%/) ? ')' : '}'; | ||||
484 | } | ||||
485 | elsif ($realtype eq 'CODE') { | ||||
486 | if ($s->{deparse}) { | ||||
487 | require B::Deparse; | ||||
488 | my $sub = 'sub ' . (B::Deparse->new)->coderef2text($val); | ||||
489 | $pad = $s->{sep} . $s->{pad} . $s->{apad} . $s->{xpad} x ($s->{level} - 1); | ||||
490 | $sub =~ s/\n/$pad/gse; | ||||
491 | $out .= $sub; | ||||
492 | } | ||||
493 | else { | ||||
494 | $out .= 'sub { "DUMMY" }'; | ||||
495 | carp "Encountered CODE ref, using dummy placeholder" if $s->{purity}; | ||||
496 | } | ||||
497 | } | ||||
498 | else { | ||||
499 | croak "Can't handle '$realtype' type"; | ||||
500 | } | ||||
501 | |||||
502 | if ($realpack and !$no_bless) { # we have a blessed ref | ||||
503 | $out .= ', ' . _quote($realpack) . ' )'; | ||||
504 | $out .= '->' . $s->{toaster} . '()' | ||||
505 | if $s->{toaster} ne ''; | ||||
506 | $s->{apad} = $blesspad; | ||||
507 | } | ||||
508 | $s->{level}--; | ||||
509 | } | ||||
510 | else { # simple scalar | ||||
511 | |||||
512 | my $ref = \$_[1]; | ||||
513 | my $v; | ||||
514 | # first, catalog the scalar | ||||
515 | if ($name ne '') { | ||||
516 | $id = format_refaddr($ref); | ||||
517 | if (exists $s->{seen}{$id}) { | ||||
518 | if ($s->{seen}{$id}[2]) { | ||||
519 | $out = $s->{seen}{$id}[0]; | ||||
520 | #warn "[<$out]\n"; | ||||
521 | return "\${$out}"; | ||||
522 | } | ||||
523 | } | ||||
524 | else { | ||||
525 | #warn "[>\\$name]\n"; | ||||
526 | $s->{seen}{$id} = ["\\$name", $ref]; | ||||
527 | } | ||||
528 | } | ||||
529 | $ref = \$val; | ||||
530 | if (ref($ref) eq 'GLOB') { # glob | ||||
531 | my $name = substr($val, 1); | ||||
532 | if ($name =~ /^[A-Za-z_][\w:]*$/ && $name ne 'main::') { | ||||
533 | $name =~ s/^main::/::/; | ||||
534 | $sname = $name; | ||||
535 | } | ||||
536 | else { | ||||
537 | $sname = $s->_dump( | ||||
538 | $name eq 'main::' || $] < 5.007 && $name eq "main::\0" | ||||
539 | ? '' | ||||
540 | : $name, | ||||
541 | "", | ||||
542 | ); | ||||
543 | $sname = '{' . $sname . '}'; | ||||
544 | } | ||||
545 | if ($s->{purity}) { | ||||
546 | my $k; | ||||
547 | local ($s->{level}) = 0; | ||||
548 | for $k (qw(SCALAR ARRAY HASH)) { | ||||
549 | my $gval = *$val{$k}; | ||||
550 | next unless defined $gval; | ||||
551 | next if $k eq "SCALAR" && ! defined $$gval; # always there | ||||
552 | |||||
553 | # _dump can push into @post, so we hold our place using $postlen | ||||
554 | my $postlen = scalar @post; | ||||
555 | $post[$postlen] = "\*$sname = "; | ||||
556 | local ($s->{apad}) = " " x length($post[$postlen]) if $s->{indent} >= 2; | ||||
557 | $post[$postlen] .= $s->_dump($gval, "\*$sname\{$k\}"); | ||||
558 | } | ||||
559 | } | ||||
560 | $out .= '*' . $sname; | ||||
561 | } | ||||
562 | elsif (!defined($val)) { | ||||
563 | $out .= "undef"; | ||||
564 | } | ||||
565 | elsif (defined &_vstring and $v = _vstring($val) | ||||
566 | and !_bad_vsmg || eval $v eq $val) { | ||||
567 | $out .= $v; | ||||
568 | } | ||||
569 | elsif (!defined &_vstring | ||||
570 | and ref $ref eq 'VSTRING' || eval{Scalar::Util::isvstring($val)}) { | ||||
571 | $out .= sprintf "%vd", $val; | ||||
572 | } | ||||
573 | # \d here would treat "1\x{660}" as a safe decimal number | ||||
574 | elsif ($val =~ /^(?:0|-?[1-9][0-9]{0,8})\z/) { # safe decimal number | ||||
575 | $out .= $val; | ||||
576 | } | ||||
577 | else { # string | ||||
578 | if ($s->{useqq} or $val =~ tr/\0-\377//c) { | ||||
579 | # Fall back to qq if there's Unicode | ||||
580 | $out .= qquote($val, $s->{useqq}); | ||||
581 | } | ||||
582 | else { | ||||
583 | $out .= _quote($val); | ||||
584 | } | ||||
585 | } | ||||
586 | } | ||||
587 | if ($id) { | ||||
588 | # if we made it this far, $id was added to seen list at current | ||||
589 | # level, so remove it to get deep copies | ||||
590 | if ($s->{deepcopy}) { | ||||
591 | delete($s->{seen}{$id}); | ||||
592 | } | ||||
593 | elsif ($name) { | ||||
594 | $s->{seen}{$id}[2] = 1; | ||||
595 | } | ||||
596 | } | ||||
597 | return $out; | ||||
598 | } | ||||
599 | |||||
600 | # | ||||
601 | # non-OO style of earlier version | ||||
602 | # | ||||
603 | sub Dumper { | ||||
604 | return Data::Dumper->Dump([@_]); | ||||
605 | } | ||||
606 | |||||
607 | # compat stub | ||||
608 | sub DumperX { | ||||
609 | return Data::Dumper->Dumpxs([@_], []); | ||||
610 | } | ||||
611 | |||||
612 | # | ||||
613 | # reset the "seen" cache | ||||
614 | # | ||||
615 | sub Reset { | ||||
616 | my($s) = shift; | ||||
617 | $s->{seen} = {}; | ||||
618 | return $s; | ||||
619 | } | ||||
620 | |||||
621 | sub Indent { | ||||
622 | my($s, $v) = @_; | ||||
623 | if (defined($v)) { | ||||
624 | if ($v == 0) { | ||||
625 | $s->{xpad} = ""; | ||||
626 | $s->{sep} = ""; | ||||
627 | } | ||||
628 | else { | ||||
629 | $s->{xpad} = " "; | ||||
630 | $s->{sep} = "\n"; | ||||
631 | } | ||||
632 | $s->{indent} = $v; | ||||
633 | return $s; | ||||
634 | } | ||||
635 | else { | ||||
636 | return $s->{indent}; | ||||
637 | } | ||||
638 | } | ||||
639 | |||||
640 | sub Trailingcomma { | ||||
641 | my($s, $v) = @_; | ||||
642 | defined($v) ? (($s->{trailingcomma} = $v), return $s) : $s->{trailingcomma}; | ||||
643 | } | ||||
644 | |||||
645 | sub Pair { | ||||
646 | my($s, $v) = @_; | ||||
647 | defined($v) ? (($s->{pair} = $v), return $s) : $s->{pair}; | ||||
648 | } | ||||
649 | |||||
650 | sub Pad { | ||||
651 | my($s, $v) = @_; | ||||
652 | defined($v) ? (($s->{pad} = $v), return $s) : $s->{pad}; | ||||
653 | } | ||||
654 | |||||
655 | sub Varname { | ||||
656 | my($s, $v) = @_; | ||||
657 | defined($v) ? (($s->{varname} = $v), return $s) : $s->{varname}; | ||||
658 | } | ||||
659 | |||||
660 | sub Purity { | ||||
661 | my($s, $v) = @_; | ||||
662 | defined($v) ? (($s->{purity} = $v), return $s) : $s->{purity}; | ||||
663 | } | ||||
664 | |||||
665 | sub Useqq { | ||||
666 | my($s, $v) = @_; | ||||
667 | defined($v) ? (($s->{useqq} = $v), return $s) : $s->{useqq}; | ||||
668 | } | ||||
669 | |||||
670 | sub Terse { | ||||
671 | my($s, $v) = @_; | ||||
672 | defined($v) ? (($s->{terse} = $v), return $s) : $s->{terse}; | ||||
673 | } | ||||
674 | |||||
675 | sub Freezer { | ||||
676 | my($s, $v) = @_; | ||||
677 | defined($v) ? (($s->{freezer} = $v), return $s) : $s->{freezer}; | ||||
678 | } | ||||
679 | |||||
680 | sub Toaster { | ||||
681 | my($s, $v) = @_; | ||||
682 | defined($v) ? (($s->{toaster} = $v), return $s) : $s->{toaster}; | ||||
683 | } | ||||
684 | |||||
685 | sub Deepcopy { | ||||
686 | my($s, $v) = @_; | ||||
687 | defined($v) ? (($s->{deepcopy} = $v), return $s) : $s->{deepcopy}; | ||||
688 | } | ||||
689 | |||||
690 | sub Quotekeys { | ||||
691 | my($s, $v) = @_; | ||||
692 | defined($v) ? (($s->{quotekeys} = $v), return $s) : $s->{quotekeys}; | ||||
693 | } | ||||
694 | |||||
695 | sub Bless { | ||||
696 | my($s, $v) = @_; | ||||
697 | defined($v) ? (($s->{'bless'} = $v), return $s) : $s->{'bless'}; | ||||
698 | } | ||||
699 | |||||
700 | sub Maxdepth { | ||||
701 | my($s, $v) = @_; | ||||
702 | defined($v) ? (($s->{'maxdepth'} = $v), return $s) : $s->{'maxdepth'}; | ||||
703 | } | ||||
704 | |||||
705 | sub Maxrecurse { | ||||
706 | my($s, $v) = @_; | ||||
707 | defined($v) ? (($s->{'maxrecurse'} = $v), return $s) : $s->{'maxrecurse'}; | ||||
708 | } | ||||
709 | |||||
710 | sub Useperl { | ||||
711 | my($s, $v) = @_; | ||||
712 | defined($v) ? (($s->{'useperl'} = $v), return $s) : $s->{'useperl'}; | ||||
713 | } | ||||
714 | |||||
715 | sub Sortkeys { | ||||
716 | my($s, $v) = @_; | ||||
717 | defined($v) ? (($s->{'sortkeys'} = $v), return $s) : $s->{'sortkeys'}; | ||||
718 | } | ||||
719 | |||||
720 | sub Deparse { | ||||
721 | my($s, $v) = @_; | ||||
722 | defined($v) ? (($s->{'deparse'} = $v), return $s) : $s->{'deparse'}; | ||||
723 | } | ||||
724 | |||||
725 | sub Sparseseen { | ||||
726 | my($s, $v) = @_; | ||||
727 | defined($v) ? (($s->{'noseen'} = $v), return $s) : $s->{'noseen'}; | ||||
728 | } | ||||
729 | |||||
730 | # used by qquote below | ||||
731 | 1 | 9µs | my %esc = ( | ||
732 | "\a" => "\\a", | ||||
733 | "\b" => "\\b", | ||||
734 | "\t" => "\\t", | ||||
735 | "\n" => "\\n", | ||||
736 | "\f" => "\\f", | ||||
737 | "\r" => "\\r", | ||||
738 | "\e" => "\\e", | ||||
739 | ); | ||||
740 | |||||
741 | 1 | 23µs | 1 | 6µs | my $low_controls = ($IS_ASCII) # spent 6µs making 1 call to Data::Dumper::CORE:qr |
742 | |||||
743 | # This includes \177, because traditionally it has been | ||||
744 | # output as octal, even though it isn't really a "low" | ||||
745 | # control | ||||
746 | ? qr/[\0-\x1f\177]/ | ||||
747 | |||||
748 | # EBCDIC low controls. | ||||
749 | : qr/[\0-\x3f]/; | ||||
750 | |||||
751 | # put a string value in double quotes | ||||
752 | sub qquote { | ||||
753 | local($_) = shift; | ||||
754 | s/([\\\"\@\$])/\\$1/g; | ||||
755 | |||||
756 | # This efficiently changes the high ordinal characters to \x{} if the utf8 | ||||
757 | # flag is on. On ASCII platforms, the high ordinals are all the | ||||
758 | # non-ASCII's. On EBCDIC platforms, we don't include in these the non-ASCII | ||||
759 | # controls whose ordinals are less than SPACE, excluded below by the range | ||||
760 | # \0-\x3f. On ASCII platforms this range just compiles as part of :ascii:. | ||||
761 | # On EBCDIC platforms, there is just one outlier high ordinal control, and | ||||
762 | # it gets output as \x{}. | ||||
763 | 2 | 1.49ms | 2 | 53µs | # spent 44µs (34+10) within Data::Dumper::BEGIN@763 which was called:
# once (34µs+10µs) by Razor2::String::BEGIN@7 at line 763 # spent 44µs making 1 call to Data::Dumper::BEGIN@763
# spent 10µs making 1 call to bytes::import |
764 | s/([^[:ascii:]\0-\x3f])/sprintf("\\x{%x}",ord($1))/ge | ||||
765 | if $bytes > length | ||||
766 | || (! $IS_ASCII && $] ge 5.008_001 && utf8::is_utf8($_)); | ||||
767 | |||||
- - | |||||
773 | return qq("$_") unless /[[:^print:]]/; # fast exit if only printables | ||||
774 | |||||
775 | # Here, there is at least one non-printable to output. First, translate the | ||||
776 | # escapes. | ||||
777 | s/([\a\b\t\n\f\r\e])/$esc{$1}/g; | ||||
778 | |||||
779 | # no need for 3 digits in escape for octals not followed by a digit. | ||||
780 | s/($low_controls)(?!\d)/'\\'.sprintf('%o',ord($1))/eg; | ||||
781 | |||||
782 | # But otherwise use 3 digits | ||||
783 | s/($low_controls)/'\\'.sprintf('%03o',ord($1))/eg; | ||||
784 | |||||
785 | # all but last branch below not supported --BEHAVIOR SUBJECT TO CHANGE-- | ||||
786 | my $high = shift || ""; | ||||
787 | if ($high eq "iso8859") { # Doesn't escape the Latin1 printables | ||||
788 | if ($IS_ASCII) { | ||||
789 | s/([\200-\240])/'\\'.sprintf('%o',ord($1))/eg; | ||||
790 | } | ||||
791 | elsif ($] ge 5.007_003) { | ||||
792 | my $high_control = utf8::unicode_to_native(0x9F); | ||||
793 | s/$high_control/sprintf('\\%o',ord($1))/eg; | ||||
794 | } | ||||
795 | } elsif ($high eq "utf8") { | ||||
796 | # Some discussion of what to do here is in | ||||
797 | # https://rt.perl.org/Ticket/Display.html?id=113088 | ||||
798 | # use utf8; | ||||
799 | # $str =~ s/([^\040-\176])/sprintf "\\x{%04x}", ord($1)/ge; | ||||
800 | } elsif ($high eq "8bit") { | ||||
801 | # leave it as it is | ||||
802 | } else { | ||||
803 | s/([[:^ascii:]])/'\\'.sprintf('%03o',ord($1))/eg; | ||||
804 | #s/([^\040-\176])/sprintf "\\x{%04x}", ord($1)/ge; | ||||
805 | } | ||||
806 | |||||
807 | return qq("$_"); | ||||
808 | } | ||||
809 | |||||
810 | # helper sub to sort hash keys in Perl < 5.8.0 where we don't have | ||||
811 | # access to sortsv() from XS | ||||
812 | sub _sortkeys { [ sort keys %{$_[0]} ] } | ||||
813 | |||||
814 | sub _refine_name { | ||||
815 | my $s = shift; | ||||
816 | my ($name, $val, $i) = @_; | ||||
817 | if (defined $name) { | ||||
818 | if ($name =~ /^[*](.*)$/) { | ||||
819 | if (defined $val) { | ||||
820 | $name = (ref $val eq 'ARRAY') ? ( "\@" . $1 ) : | ||||
821 | (ref $val eq 'HASH') ? ( "\%" . $1 ) : | ||||
822 | (ref $val eq 'CODE') ? ( "\*" . $1 ) : | ||||
823 | ( "\$" . $1 ) ; | ||||
824 | } | ||||
825 | else { | ||||
826 | $name = "\$" . $1; | ||||
827 | } | ||||
828 | } | ||||
829 | elsif ($name !~ /^\$/) { | ||||
830 | $name = "\$" . $name; | ||||
831 | } | ||||
832 | } | ||||
833 | else { # no names provided | ||||
834 | $name = "\$" . $s->{varname} . $i; | ||||
835 | } | ||||
836 | return $name; | ||||
837 | } | ||||
838 | |||||
839 | sub _compose_out { | ||||
840 | my $s = shift; | ||||
841 | my ($valstr, $postref) = @_; | ||||
842 | my $out = ""; | ||||
843 | $out .= $s->{pad} . $valstr . $s->{sep}; | ||||
844 | if (@{$postref}) { | ||||
845 | $out .= $s->{pad} . | ||||
846 | join(';' . $s->{sep} . $s->{pad}, @{$postref}) . | ||||
847 | ';' . | ||||
848 | $s->{sep}; | ||||
849 | } | ||||
850 | return $out; | ||||
851 | } | ||||
852 | |||||
853 | 1 | 35µs | 1; | ||
854 | __END__ | ||||
# spent 6µs within Data::Dumper::CORE:qr which was called:
# once (6µs+0s) by Razor2::String::BEGIN@7 at line 741 | |||||
# spent 8µs within Data::Dumper::_vstring which was called:
# once (8µs+0s) by Data::Dumper::BEGIN@279 at line 279 |