While musing and experimenting over a recent post here at the Monastery, I came across a
bizarre and somewhat unexplainable behaviour of hash autovivification: a particular kind of
scalar (which I dubbed a "stopper scalar" - nothing more than a silly string) would stop autovivification in its tracks, without raising any warnings or runtime errors.
At the end of the post you will find the exemplification script; below there are more detailed
comments over the exemplified cases:
-
Cases 1-3 are "normal". Everything happens as it should.
-
Case 4 is the first sign that something is wrong. The autovivification doesn't take place. At
first I thought that the lack of warnings or errors is due to a quirk put in place to prevent
accidental overwriting of valid non-hashref (existent and defined - see exclusion cases 1-3) scalars
by autovivification. But it felt bizzare and non-Perlish in nature. Bug in Perl? So I kept toddling along...
-
Case 5 is more worrying. The assignment fails without warning or error.
This is a clear sign that something is severely wrong.
Time to use a more 'fundamental' WMBS (Weapon of Mass Bug Squashing) - see below :)
-
Finally, use strict was loose and nuked it to pieces.
See program output below to find out what went wrong!
#!/usr/bin/perl -w
use Data::Dumper;
print "Case 1: non existent\n";
exists $x->{'y'}->{'z'};
print Dumper $x;
print "Case 2: undef\n";
$x = undef;
exists $x->{'y'}->{'z'};
print Dumper $x;
print "Case 3: hashref\n";
$x = {};
exists $x->{'y'}->{'z'};
print Dumper $x;
print "Case 4: stopper scalar\n";
$x = 'stopper';
exists $x->{'y'}->{'z'};
print Dumper $x;
print "Case 5: Even assignment won't do, and won't trigger warning or
+runtime error!\n";
$x = 'stop2';
$x->{'y'}->{'z'} = 'something';
print Dumper $x;
print "Case 6: use strict is your friend :)\n";
eval {
use strict 'refs';
$x = 'stop3';
$x->{'y'}->{'z'} = 'something else';
print Dumper $x;
};
$@ and print $@;
And here's the output.
Case 1: non existent
$VAR1 = {
'y' => {}
};
Case 2: undef
$VAR1 = {
'y' => {}
};
Case 3: hashref
$VAR1 = {
'y' => {}
};
Case 4: stopper scalar
$VAR1 = 'stopper';
Case 5: Even assignment won't do, and won't trigger warning or runtime
+ error!
$VAR1 = 'stop2';
Case 6: use strict is your friend :)
Can't use string ("stop3") as a HASH ref while "strict refs" in use at
+ ./autoviv-stop.pl line 35.
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.