I could swear I've seen this behavior before, but can't find the post.
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
my $questions = {
1 => { foo => 'bar' },
2 => { foo => 'bar' },
3 => { foo => 'bar' }
};
print "(before):\n",Dumper($questions),"\n";
for (1 .. 4) {
my $foo = $questions->{$_}{foo} || "daklfjsdalk";
}
print "(after): \n",Dumper($questions), "\n";
This returns something like:
(before):
$VAR1 = {
1 => {
'foo' => 'bar'
},
2 => {
'foo' => 'bar'
},
3 => {
'foo' => 'bar'
}
};
(after):
$VAR1 = {
1 => {
'foo' => 'bar'
},
2 => {
'foo' => 'bar'
},
3 => {
'foo' => 'bar'
},
4 => {}
};
And I'm not sure why, my question is twofold:
1) Is there a good reason why perl creates the reference (4)?
2) Is there a good reason why perl doesn't WARN that I am trying to access a value that doesn't exist? Note that even taking out the || "djkafdkljfa" does not produce a warning
The problem is appearing in a bit of code I am developing, and I can get around it, but this tripped me up while I was working on it (using exists($hashref->{key} to determine execution path).
ps:
Damn, I miss this place, I think it's time to start hanging out here again (especially now that I "do" perl for a living :))
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.