|
|
| laziness, impatience, and hubris | |
| PerlMonks |
Can you use string as a HASH ref while "strict refs" in use?by choroba (Cardinal) |
| on Dec 06, 2013 at 00:04 UTC ( [id://1065867]=perlmeditation: print w/replies, xml ) | Need Help?? |
|
At work, I was given a code to add functionality. You can probably imagine - a CGI script that shows a list, some of whose elements might be selected. The part responsible for handling the selected elements confused me (simplified):
Wait, what's going on? We test for $list{$key}{selected}, so $list{$key} is a hash reference. Then, we print $list{$key} - but in the output, we do not get any HASH(0x2561a68), but the correct goods' names. I asked a coworker, and she told me this part of the script was "dark magic"; she had debugged it once and still remembered the value was coming out even if it should not. I played a bit with the script to remember I have already seen a similar behaviour: of course, the script does not use strict! Try yourself:
The last print explains what happens: Perl sees you are trying to use $list{ref} as a hash reference, while its value is the string "Overwritten". It therefore creates a hash of that name for you (even more funny if the string contains spaces). So far, so good. Lesson learnt: the people who tell you "use strict" know what they say. I tried to explain to the coworkers what the problem was, but those not familiar with Perl were not able to understand. But then, I thought to myself: Is it possible to make the code work even under strict? tie and overload would probably be useful, as I remembered from Programming Perl. And after several minutes, I was able to run the code under strict. You can test your skills before revealing my solution: <Reveal this spoiler or all in this thread>
Just for completness, this is the "common" behaviour I expected at the beginning:
Note: The three last code examples should be kept in one file.
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Back to
Meditations
|
|
||||||||||||||||||||||||||||||||||||