I have to admit I didn't even search because after a look at the trivial code of any() (and pasting it in my script to make sure it's not the fault of some XS foo) I was pretty convinced it can't possibly be the function's fault. It's written exactly as I would do it and as every book I've seen recommends it.
Thanks to everybody who recommended "our" etc.---I'd only tried local which unsurprisingly didn't work. Still, having to think about something like this is a major PITA, particularly when it's inconsistent and e.g. works with builtins like grep. I think for me it boils down to "given is evil". for/when should do fine as long as there are no continues.
List::* and similar modules could probably work around that with some bytecode hack as suggested in a reply to the bug but having to introduce such extreme ugliness to restore sane behavior suggests something else needs fixing
So I think this is at least a documentation bug of given/when. Why the lexical $_ I still don't know; making
given($foo) {
when('bar') { ... }
}
equivalent (well, with some added magic for the last/continue stuff) todo {
if($foo ~~ 'bar') { ... }
}
would seem to make more sense IMHO. But if it behaves like it does, it should carry a big fat warning saying "If you define any blocks or callbacks within given(), don't expect $_ to behave like everywhere else, use explicit "our" or there will be weeping and gnashing of teeth!!!11!" |