Hi community, this is my very first question here. I have some problems with /g, \G and pos() and i do not understand what's going on (possibly a bug in my version of perl?). Hope someone knows about the following behaviour of perl:
# File: defect1.pl use strict; use warnings; my $str = " xyz"; $str =~ /^(\s*)/g or die; print("match1: pos=", pos($str), "\n"); $str =~ /\G(\s*)/g or die; print("match2: pos=", pos($str), "\n"); $str =~ /\G(\s*)/g or die; print("match3: pos=", pos($str), "\n");
Output:
match1: pos=2 match2: pos=2 Died at defect1.pl line 13.

Why does the third match fail? I'd expect that after the first regex i could repeat $str=~/\G(\s*)/g again and again, it should always succeed (and never change pos()) since \s* also matches zero occurrences of spaces.

Even worse is this:
use strict; use warnings; my $str = " xyz"; $str =~ /^(\s*)/g; my ($x) = ($str =~ /\G(x)/g) or die; defined(pos($str)) or warn("pos() is undefined!\n"); print("this should be x:$x\n");
Output:
pos() is undefined! this should be x:x
Even though the regex succeeded and $x has the correct value, pos($str) is undefined. Same result from this variant:
use strict; use warnings; my $str = " xyz"; $str =~ /^(\s*)/g; my $x = ($str =~ /\G(x)/g)[0] or die; defined(pos($str)) or warn("pos() is undefined!\n"); print("this should be x:$x\n");
Again:
pos() is undefined! this should be x:x
In contrast, the following works fine:
use strict; use warnings; my $str = " xyz"; $str =~ /^(\s*)/g; $str =~ /\G(x)/g or die; my $x = $1; defined(pos($str)) or warn("pos() is undefined!\n"); print("this should be x:$x\n");
Output:
this should be x:x
Obviously, perl clobbers the position associated with $str as soon as i treat the regex's result as an array - but why? I'm running perl 5.14 on Suse Linux 12.1. perl -v says:
This is perl 5, version 14, subversion 2 (v5.14.2) built for x86_64-li +nux-thread-multi

In reply to [Perl 5.14, regex]: Problems with /g, \G and pos() by Darkwing

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.