|Keep It Simple, Stupid|
Eliminating "used only once" warnings from List::Util::reduceby davido (Archbishop)
|on Jul 30, 2013 at 00:37 UTC||Need Help??|
davido has asked for the
wisdom of the Perl Monks concerning the following question:
This is something that's bothered me for awhile now, but until a few days ago I didn't take the time to investigate the "internals" involved.
Consider the following code:
This will generate the following output:
On the other hand...
Given that List::Util is a core module, reduce really ought to have the same privileges as sort with respect to gracefully using $a and $b. I looked at what makes sort's code different from reduce, aside from the obvious fact that one sorts and one reduces. I began by trying to find two needles in the Perl code-base haystack (searching for a and b doesn't really narrow it down). tye was helpful in pointing me to some code in op.c.
This seems to be the relevant segment:
GvMULTI_on must have the effect of tricking Perl into thinking that $a and $b were already used more than once, which takes care of the warning. perlguts, says this in reference to to the SV* get_sv("package::varname", GV_ADD); function:
I thought maybe just using GV_ADD|GV_ADDMULTI could have the same effect without an explicit call to the undocumented GvMULTI_on().
Then I looked at the relevant code in List::Utils:
Ah-hah! Add a call to GvMULTI_on(), or "or"-ing (|) GV_ADDMULTI into the fetch, should yield a patch for reduce that allows it to play nice with warnings.
Which brings me here to the font of Perl wisdom, hoping someone can explain why, why not, and possibly what needs to happen (in the module or perl) to solve it. This would also help to settle the same bug in List::MoreUtils::pairwise, as well as helping me out with a pet project.
Can anyone shed some light?
Update: I'm aware of many of the previous conversations on the topic. I'm looking at this from an internals perspective; what can the XS code of these modules do to eliminate the problem, not how can I make my use of these modules not trigger warnings. Actually, BrowserUk may be on to something... time for more tests. ;)