If we begin by taking my out of the equation, and simplify by using an array rather than a hash, it’s easy to see the difference:
23:10 >perl -Mstrict -wE "my @c = ('a' .. 'e'); my $q = \@c; my $r = \ +(@c); say $q, ' --> ', @$q, qq[\n], $r, ' --> ', $$r;" ARRAY(0x18a5654) --> abcde SCALAR(0x4bc344) --> e
Whereas \@... evaluates as a reference to a variable, \(...) evaluates as a reference to the final value (a scalar) of the list within the parentheses.
Does the same logic hold when my is reintroduced? It would appear so:
1:46 >perl -Mstrict -wE "my $q = \my @c; @c = ('f' .. 'h'); say $q, +' --> ', @$q;" ARRAY(0x177554c) --> fgh 1:46 >perl -Mstrict -wE "my $q = \my(@c); @c = ('f' .. 'h'); say $q, +' --> ', $$q;" Use of uninitialized value in say at -e line 1. SCALAR(0x1d20f6c) --> 1:46 >
Assigning to @c affects the contents of @$q, showing that \my @c not only declares @c as a lexical variable but also returns a reference to it. But \my(@c) declares @c as a lexical variable and then returns its contents as an empty list, with the result that the expression returns a reference to undef.
Now to Deparse: The problem is not that it fails to distinguish between the two cases, since it clearly does: \my(%hash) and \(my(%hash)) are not the same. The problem becomes apparent if we add some labels:
(a) \my %hash --> (b) \my(%hash) but (b) \my(%hash) --> (c) \(my(%hash))
Deparse changes (a) into (b) but also changes (b) into (c). That is, running Deparse on its own output will produce a different result the second time around: (a) --> (b) --> (c). This certainly looks like a bug.
The use of warn introduces another anomaly. Consider:
1:11 >perl -wE "warn undef;" Use of uninitialized value in warn at -e line 1. Warning: something's wrong at -e line 1. 1:12 >perl -wE "warn \undef;" SCALAR(0x1d20f6c) at -e line 1. 1:12 >
In the OP’s code, warn appears to be treating \my(%hash) as undef, and not as \undef, as would be expected. I don’t see the reason for this, either.
Update: Thanks to tye for clearing up the behaviour of warn:
Constructing a reference to each scalar in an empty list gives one an empty list of references, which is just an empty list.
I had to read this several times before the lightbulb went on. Of course! There is no undef, only an empty list. I’d forgotten about context — the key point being, as tye says, that \( ... ) returns a list of references.
Athanasius <°(((>< contra mundum
In reply to Re: Deparse says \my(%hash) is the same as \my %hash but it isn't
by Athanasius
in thread Deparse says \my(%hash) is the same as \my %hash but it isn't
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |