Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deal with blessed regexes #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 33 additions & 35 deletions lib/Data/Dump.pm
Original file line number Diff line number Diff line change
Expand Up @@ -189,42 +189,11 @@ sub _dump
if (defined $out) {
# keep it
}
elsif ($type eq "SCALAR" || $type eq "REF" || $type eq "REGEXP") {
elsif ($type eq "SCALAR" || $type eq "REF") {
if ($ref) {
if ($class && $class eq "Regexp") {
my $v = "$rval";

my $mod = "";
if ($v =~ /^\(\?\^?([msix-]*):([\x00-\xFF]*)\)\z/) {
$mod = $1;
$v = $2;
$mod =~ s/-.*//;
}

my $sep = '/';
my $sep_count = ($v =~ tr/\///);
if ($sep_count) {
# see if we can find a better one
for ('|', ',', ':', '#') {
my $c = eval "\$v =~ tr/\Q$_\E//";
#print "SEP $_ $c $sep_count\n";
if ($c < $sep_count) {
$sep = $_;
$sep_count = $c;
last if $sep_count == 0;
}
}
}
$v =~ s/\Q$sep\E/\\$sep/g;

$out = "qr$sep$v$sep$mod";
undef($class);
}
else {
delete $seen{$id} if $type eq "SCALAR"; # will be seen again shortly
my $val = _dump($$rval, $name, [@$idx, "\$"], 0, $pclass, $pidx);
$out = $class ? "do{\\(my \$o = $val)}" : "\\$val";
}
delete $seen{$id} if $type eq "SCALAR"; # will be seen again shortly
my $val = _dump($$rval, $name, [@$idx, "\$"], 0, $pclass, $pidx);
$out = $class ? "do{\\(my \$o = $val)}" : "\\$val";
} else {
if (!defined $$rval) {
$out = "undef";
Expand Down Expand Up @@ -367,6 +336,35 @@ sub _dump
$out =~ s/,$/ / unless $nl;
$out .= "}";
}
elsif ($type eq "REGEXP") {
my $v = "$rval";

my $mod = "";
if ($v =~ /^\(\?\^?([msix-]*):([\x00-\xFF]*)\)\z/) {
$mod = $1;
$v = $2;
$mod =~ s/-.*//;
}

my $sep = '/';
my $sep_count = ($v =~ tr/\///);
if ($sep_count) {
# see if we can find a better one
for ('|', ',', ':', '#') {
my $c = eval "\$v =~ tr/\Q$_\E//";
#print "SEP $_ $c $sep_count\n";
if ($c < $sep_count) {
$sep = $_;
$sep_count = $c;
last if $sep_count == 0;
}
}
}
$v =~ s/\Q$sep\E/\\$sep/g;

$out = "qr$sep$v$sep$mod";
undef($class) if $class && $class eq "Regexp";
}
elsif ($type eq "CODE") {
$out = 'sub { ... }';
}
Expand Down
4 changes: 4 additions & 0 deletions t/regexp.t
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ $a = {
g => qr,///////,,
h => qr*/|,:*,
i => qr*/|,:#*,
j => bless(qr/Foo/, "Regexp::Alt"),
k => \qr/Foo/,
};

ok(Data::Dump::dump($a) . "\n", <<'EOT');
Expand All @@ -34,5 +36,7 @@ ok(Data::Dump::dump($a) . "\n", <<'EOT');
g => qr|///////|,
h => qr#/|,:#,
i => qr/\/|,:#/,
j => bless(qr/Foo/, "Regexp::Alt"),
k => \qr/Foo/,
}
EOT