I am quite confused with your second piece of code. What does it try to demo? What is your expected result (to print out the "popped into existence" or not, I tested it, and it printed)?
Also I don't know whether you realize that you actually used the concept of symblic reference in your second demo.
I changed your second demo a little bit, and added comments to explain:
use strict; #this basically disallows symblic ref
use warnings;
my %foo;
$foo{bar} = "key bar's value"; #now you made $foo{bar} EXISTS, there i
+s no need for auto-vivification to create it any more. MOST IMPORTANT
+LY, this can be a symblic reference depending on how you use it, cont
+inue...
if(exists($foo{qux})) {
print "\$foo{qux} exists";
}
if(ref $foo{bar} eq 'HASH' && exists($foo{bar}{baz})) {#Here you are s
+trongly suggesting a symblic reference. As for the code, the part bef
+ore && obviously evaluate to false, so that exists after && will be e
+valuated, but no auto-vivfication here, as $foo{bar} exists any way
print "\$foo{bar}{baz} exists\n";#will not print as there is no $f
+oo{bar}{baz}
}
if(exists($foo{bar})) {#yes
print "\$foo{bar} popped into existence\n";#print out, but not pop
+ped into existence, it is actually created by you
}
#the following is added by me
$foo{bar}{baz} = 1; #violates strict refs, error, because you set $foo
+{bar} to a string earlier. There is no way to create $foo{bar}{baz} i
+n this context, unless you turn off use strict("refs")
update:
Yes, Aristotle is right, and I messed up with && and ||, Thanks for pointing out.
However this mistake does not reduce the value of this post, and the post as a whole is still correct, especially the discussion on symbolic reference.
I keep the mistake there (Remove the original mistake pointed out by other monks, would make other monk's reply funny, and sounds like irrelevant, and I don't do this ;-)
"And the latter is exactly what he's talking
about protecting against by using ref."
Doubt.
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.
|
|