Perl: the Markov chain saw PerlMonks

### Comment on

 Need Help??

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (10)
As of 2017-06-24 14:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How many monitors do you use while coding?

Results (558 votes). Check out past polls.