in reply to Re: Using pos() inside given/when
in thread Using pos() inside given/when

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!

Replies are listed 'Best First'.
Re^3: Using pos() inside given/when
by tobyink (Canon) on May 10, 2013 at 11:30 UTC

    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