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) to
do { 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!"


In reply to Re^2: Lexical $_ in given/when vs. BLOCK arguments by mbethke
in thread Lexical $_ in given/when vs. BLOCK arguments by mbethke

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.