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 } } }