in reply to Using pos() inside given/when

Kenosis and educated_foo have answered the question. I just want to add a few comments:

  1. On my version of perl (v5.16.0) the code in the OP produces an infinite loop.

  2. The real code is, no doubt, more complex than the code in the example. But I suspect that, even so, the logic could be significantly clarified by rearranging the loops. Certainly, the example as given can be written with just one loop:

    print pos $h->{s}, "\n" while $h->{s} =~ /a/g;
  3. The line:

    $h->{s} =~ m/^/g;

    is presumably there only to reset pos, in which case I suggest the following would be clearer:

    pos($h->{s}) = undef;

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: Using pos() inside given/when
by Anonymous Monk on May 10, 2013 at 10:51 UTC

    Thanks for your answers. Switching to a for solved that problem (I could not have rewritten it with a simple loop, as I have a lot of when in a Lex-like code).

    If I understand correctly the fact that in v5.16 the same code gives an infinite loop, it's that the given's version of $_ have been "fixed", in the sense that a new scalar is now created for each iteration (hence the pos() function and the matching are in accord). But the same thing with the for loop will still work as I expect ?

    If the when construct does not care from where does the $_ variable comes from, do you know what was the need to introduce given rather than just stick with for ?

    Anyway, thanks for your help!

      for and given both use $_. The difference is that for uses our $_ and aliases it to the item, while given copies (though I believe it's a cheap optimized copy) the item to my $_.

      package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name