summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFather Chrysostomos <sprout@cpan.org>2011-12-21 12:55:56 -0800
committerFather Chrysostomos <sprout@cpan.org>2011-12-22 13:31:22 -0800
commit10e8e32b3dba62bbc9452bff12981666bbf0fe4e (patch)
treecd24780fc801cacaf89940a4e5b948af8809a512
parent88c5c9718b7d6fb655f3f200b727d7d65a92c750 (diff)
downloadperl-10e8e32b3dba62bbc9452bff12981666bbf0fe4e.tar.gz
Deparse "string"->[$expr] and ->{$expr} correctly
This commit finishes the work done by b89b7257 and be6cf5cf0a by mak- ing "string"->[...] and "string"->{...} in general deparse correctly when "string" is not a valid identifier.
-rw-r--r--dist/B-Deparse/Deparse.pm17
-rw-r--r--dist/B-Deparse/t/deparse.t16
2 files changed, 26 insertions, 7 deletions
diff --git a/dist/B-Deparse/Deparse.pm b/dist/B-Deparse/Deparse.pm
index d99080aa03..927afd5bd7 100644
--- a/dist/B-Deparse/Deparse.pm
+++ b/dist/B-Deparse/Deparse.pm
@@ -3186,13 +3186,15 @@ sub elem_or_slice_array_name
} elsif (is_scope($array)) { # ${expr}[0]
return "{" . $self->deparse($array, 0) . "}";
} elsif ($array->name eq "gv") {
- $array = $self->gv_name($self->gv_or_padgv($array));
- if ($array !~ /::/) {
- my $prefix = ($left eq '[' ? '@' : '%');
- $array = $self->{curstash}.'::'.$array
- if $self->lex_in_scope($prefix . $array);
+ ($array, my $quoted) =
+ $self->stash_variable_name(
+ $left eq '[' ? '@' : '%', $self->gv_or_padgv($array)
+ );
+ if (!$allow_arrow && $quoted) {
+ # This cannot happen.
+ die "Invalid variable name $array for slice";
}
- return $array;
+ return $quoted ? "$array->" : $array;
} elsif (!$allow_arrow || is_scalar $array) { # $x[0], $$x[0], ...
return $self->deparse($array, 24);
} else {
@@ -3250,7 +3252,8 @@ sub elem {
}
if (my $array_name=$self->elem_or_slice_array_name
($array, $left, $padname, 1)) {
- return "\$" . $array_name . $left . $idx . $right;
+ return ($array_name =~ /->\z/ ? $array_name : "\$" . $array_name)
+ . $left . $idx . $right;
} else {
# $x[20][3]{hi} or expr->[20]
my $arrow = is_subscriptable($array) ? "" : "->";
diff --git a/dist/B-Deparse/t/deparse.t b/dist/B-Deparse/t/deparse.t
index 2baef66027..780d8b3093 100644
--- a/dist/B-Deparse/t/deparse.t
+++ b/dist/B-Deparse/t/deparse.t
@@ -908,11 +908,27 @@ no strict 'vars';
() = '####'->[0];
() = '^A'->[0];
() = "\ca"->[0];
+() = 'open'->[$_]; #aelem
+() = '####'->[$_];
+() = '^A'->[$_];
+() = "\ca"->[$_];
+() = 'open'->{0}; #helem
+() = '####'->{0};
+() = '^A'->{0};
+() = "\ca"->{0};
>>>>
() = $open[0];
() = '####'->[0];
() = '^A'->[0];
() = $^A[0];
+() = $open[$_];
+() = '####'->[$_];
+() = '^A'->[$_];
+() = $^A[$_];
+() = $open{'0'};
+() = '####'->{'0'};
+() = '^A'->{'0'};
+() = $^A{'0'};
####
# [perl #74740] -(f()) vs -f()
$_ = -(f());