my $str = "abc"; while ($str =~ m/.|$/gc) { printf("1: %d\n", pos($str)); } while ($str =~ m/.|$/gc) { printf("2: %d\n", pos($str)); } Output: 1: 1 1: 2 1: 3 1: 3