All,
I was reviewing some code from a coworker (no, not my code :-) ), and I found a severe perl error, but the code works fine.
Now, I was wondering, why does it actually work. This is his code reformatted
$a{test}="very ";
$a{test}{this}="strange";
print $a{test};
print $a{test}{this};
Which results in "very strange"
First of all,
strict does not allow this with the message (note:I always use strict!!)
Can't use string ("very ") as a HASH ref while "strict refs" in use ..
If I rewrite this code, it actually does this:
$a{test}="very ";
$a{test}->{this}="strange";
print $a{test};
print very->{this}; #### ?????
So "very" is the bareword that is a reference to a hash, but why does it work???
He claims, "it works, so I won't change it".
What even works is
delete $a{test}
But I am afrait that this does not clear up memory (is this correct?)
Is this one of the many mysteries of perl?
Please no replies with comments about "use strict" because know about that, I just want to know why the bareword reference works.
---------------------------
Dr. Mark Ceulemans
Senior Consultant
IT Masters, Belgium
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.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, 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, summary, 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?
See Writeup Formatting Tips and other pages linked from there for more info.