Hi all, I was playing with some code that uses an arrayref in various places, and started getting errors when i added some code and the arrayref was undef. Originally, there was something like: (all code has strict & warnings on, these are just snippets)
my $arrayref = getArrayref(); # this sometimes returned undef
foreach my $value (@$arrayref) {
...
}
This worked ok, even when $arrayref == undef. So then i added an if conditional to the method:
if ( @$arrayref ) {
...
}
This resulted in a fatal error:
Can't use an undefined value as an ARRAY reference at ...
My co-worker and I discussed this and decided that it was an rvalue versus lvalue issue. But our resolution still doesn't sit very well in my mind.
Update:This is incorrect, it creates a fatal error.
If i put this line before the if, then things are ok:
@$arrayref
end update
But thats not cool, and results in a warning too.
Anyway, in the end i did the if statement as:
if ( defined($arrayref) && @$arrayref ) {
...
}
(perhaps i should be doing a ref test also, but the value of $arrayref is always set by the same method.)
So im looking for more insight into exactly why this is all behaves like it does. I sort of understand, but not completely.
I use the most powerful debugger available: print!
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.