Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Here is a small code which I is not working as I expect:
use feature 'switch'; my $h = {s => 'ababa'}; $h->{s} =~ m/^/g; while (1) { given ($h->{s}) { when(/a/g) { print pos $h->{s}, "\n" } default { exit } } }
On my version of Perl (v5.14.2), I get:
0 0 0
But I expected:
1 3 5
The /g modifier to the regex obviously works because the loop stops after three iterations. So why does pos() fails to report the correct value ?
If I replace the given/when by an if statement then everything works fine.
Any helps understanding this will be appreciated (even more if I can get the value of pos()). Thanks!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Using pos() inside given/when
by Kenosis (Priest) on May 10, 2013 at 00:34 UTC | |
|
Re: Using pos() inside given/when
by Athanasius (Archbishop) on May 10, 2013 at 02:48 UTC | |
by Anonymous Monk on May 10, 2013 at 10:51 UTC | |
by tobyink (Canon) on May 10, 2013 at 11:30 UTC | |
|
Re: Using pos() inside given/when
by educated_foo (Vicar) on May 10, 2013 at 01:09 UTC |