Data-Dmp-Prune
view release on metacpan or search on metacpan
lib/Data/Dmp/Prune.pm view on Meta::CPAN
return qq("$_") unless /[^\040-\176]/; # fast exit
s/([\a\b\t\n\f\r\e])/$esc{$1}/g;
# no need for 3 digits in escape for these
s/([\0-\037])(?!\d)/sprintf('\\%o',ord($1))/eg;
s/([\0-\037\177-\377])/sprintf('\\x%02X',ord($1))/eg;
s/([^\040-\176])/sprintf('\\x{%X}',ord($1))/eg;
return qq("$_");
}
# END COPY PASTE FROM Data::Dump
sub _dump_code {
my $code = shift;
state $deparse = do {
require B::Deparse;
B::Deparse->new("-l"); # -i option doesn't have any effect?
};
my $res = $deparse->coderef2text($code);
my ($res_before_first_line, $res_after_first_line) =
$res =~ /(.+?)^(#line .+)/ms;
if ($OPT_REMOVE_PRAGMAS) {
$res_before_first_line = "{";
} elsif ($OPT_PERL_VERSION < 5.016) {
# older perls' feature.pm doesn't yet support q{no feature ':all';}
# so we replace it with q{no feature}.
$res_before_first_line =~ s/no feature ':all';/no feature;/m;
}
$res_after_first_line =~ s/^#line .+//gm;
$res = "sub" . $res_before_first_line . $res_after_first_line;
$res =~ s/^\s+//gm;
$res =~ s/\n+//g;
$res =~ s/;\}\z/}/;
$res;
}
sub _quote_key {
$_[0] =~ /\A-?[A-Za-z_][A-Za-z0-9_]*\z/ ||
$_[0] =~ /\A-?[1-9][0-9]{0,8}\z/ ? $_[0] : _double_quote($_[0]);
}
sub _dump {
my ($val, $subscript, $path) = @_;
my $ref = ref($val);
if ($ref eq '') {
if (!defined($val)) {
return "undef";
} elsif (looks_like_number($val) && !$OPT_STRINGIFY_NUMBERS &&
# perl does several normalizations to number literal, e.g.
# "+1" becomes 1, 0123 is octal literal, etc. make sure we
# only leave out quote when the number is not normalized
$val eq $val+0 &&
# perl also doesn't recognize Inf and NaN as numeric
# literals (ref: perldata) so these unquoted literals will
# choke under 'use strict "subs"
$val !~ /\A-?(?:inf(?:inity)?|nan)\z/i
) {
return $val;
} else {
return _double_quote($val);
}
}
my $refaddr = refaddr($val);
$_subscripts{$refaddr} //= $subscript;
if ($_seen_refaddrs{$refaddr}++) {
push @_fixups, "\$a->$subscript=\$a",
($_subscripts{$refaddr} ? "->$_subscripts{$refaddr}" : ""), ";";
return "'fix'";
}
my $class;
if ($ref eq 'Regexp' || $ref eq 'REGEXP') {
require Regexp::Stringify;
return Regexp::Stringify::stringify_regexp(
regexp=>$val, with_qr=>1, plver=>$OPT_PERL_VERSION);
}
if (blessed $val) {
$class = $ref;
$ref = reftype($val);
}
my $res;
if ($ref eq 'ARRAY') {
$res = "[";
my $i = 0;
for (@$val) {
my $elpath = "$path$i";
$res .= "," if $i;
if ($_prune_paths{$elpath}) {
$res .= "'PRUNED'";
} else {
$res .= _dump($_, "$subscript\[$i]", "$path$i/");
}
$i++;
}
$res .= "]";
} elsif ($ref eq 'HASH') {
$res = "{";
my $i = 0;
for (sort keys %$val) {
my $elpath = "$path$_";
next if $_prune_paths{$elpath};
$res .= "," if $i++;
my $k = _quote_key($_);
my $v = _dump($val->{$_}, "$subscript\{$k}", "$path$k/");
$res .= "$k=>$v";
}
$res .= "}";
} elsif ($ref eq 'SCALAR') {
$res = "\\"._dump($$val, $subscript, $path);
} elsif ($ref eq 'REF') {
( run in 1.791 second using v1.01-cache-2.11-cpan-39bf76dae61 )