XP is just a number | |
PerlMonks |
Overloading for List Contextby Zaxo (Archbishop) |
on May 31, 2002 at 14:04 UTC ( [id://170716]=perlquestion: print w/replies, xml ) | Need Help?? |
Zaxo has asked for the wisdom of the Perl Monks concerning the following question: Replying to educated_foo's excellent question at Re: Basic Objects with Overloaded Operators, I quoted an example of overloading the diamond operator <> from the Camel Book, 3rd ed., p355. I mentioned my uneasiness about how closely the overloaded operator followed the 'rules' (whatever they are). Here is the overloaded module:
It implements drawing elements from an array without replacement I worried about whether false values in the array would interfere with while (<$foo>) {}, and whether list context would fail. Sooo... I expanded the example to test a number of cases. It turns out that while <> is fine with zeros in the array. If undef is present, it ends the while loop, but the remainder of the array is still available. As I feared, list context fails to read the entire array. That means that neither my @foo = <$foo>; nor for (<$foo>) {} works as expected. I rewrote a version of the module which I thought should do the right thing in list context. It doesn't. Anyone see what I'm forgetting? Here is the test code: And here is the unsuccessful new code:
Why am I not getting a list back? The code here is organized so that it runs as a single file. After Compline,
Back to
Seekers of Perl Wisdom
|
|