Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

g-Option with m// in a loop #993

Closed
p5pRT opened this issue Dec 27, 1999 · 2 comments
Closed

g-Option with m// in a loop #993

p5pRT opened this issue Dec 27, 1999 · 2 comments

Comments

@p5pRT
Copy link

p5pRT commented Dec 27, 1999

Migrated from rt.perl.org#1943 (status was 'resolved')

Searchable as RT1943$

@p5pRT
Copy link
Author

p5pRT commented Dec 27, 1999

From pstadt@stud.fh-heilbronn.de

I tried this with perl, version 5.005_03 built for i586-linux-thread

--8<-------------------------------------------------------
#!/usr/bin/perl

$v1="../test";

print "-- without global " . "-" x 30 ."\n";
foreach $l (1 .. 10) {
  if ($v1 =~ /\//is) {
  print "$l\t$v1\n";
  }
}

print "-- with global " . "-" x 33 ."\n";
foreach $l (1 .. 10) {
  if ($v1 =~ /\//igs) {
  print "$l\t$v1\n";
  }
}

print "-- with global and redefined var " . "-" x 15 ."\n";
foreach $l (1 .. 10) {
  $v1="../test$l";
  if ($v1 =~ /\//igs) {
  print "$l\t$v1\n";
  }
}

--8<---------------------------------------------------------

I expected all three loops the same output, but there wasn't.
If I match with global against a variable in a loop without altering this
var there are problems with the matching, I assume the match-pos is not
reset.
And I think it is a BUG, because if I alter the var, the match-pos is
reset.
Or replace the assignment of $v1 in the last loop with
$v1="$v1"; # matches every line
$v1=$v1; # matches every second line

If it is a "feature" please add it to perltrap.
I'm going to test this against various older versions on other boxes and
OSs. ;-)

BYtE Oli

\ / ASCII Ribbon Campaign - Say NO to HTML in email and news
x oliver@​paukstadt.de http​://www.paukstadt.de/

@p5pRT
Copy link
Author

p5pRT commented Dec 27, 1999

From @tamias

On Mon, Dec 27, 1999 at 07​:01​:11PM +0100, Oliver Paukstadt wrote​:

HY HY

I tried this with perl, version 5.005_03 built for i586-linux-thread

--8<-------------------------------------------------------
#!/usr/bin/perl

$v1="../test";

print "-- without global " . "-" x 30 ."\n";
foreach $l (1 .. 10) {
if ($v1 =~ /\//is) {
print "$l\t$v1\n";
}
}

print "-- with global " . "-" x 33 ."\n";
foreach $l (1 .. 10) {
if ($v1 =~ /\//igs) {
print "$l\t$v1\n";
}
}

print "-- with global and redefined var " . "-" x 15 ."\n";
foreach $l (1 .. 10) {
$v1="../test$l";
if ($v1 =~ /\//igs) {
print "$l\t$v1\n";
}
}

--8<---------------------------------------------------------

I expected all three loops the same output, but there wasn't.
If I match with global against a variable in a loop without altering this
var there are problems with the matching, I assume the match-pos is not
reset.
And I think it is a BUG, because if I alter the var, the match-pos is
reset.
Or replace the assignment of $v1 in the last loop with
$v1="$v1"; # matches every line
$v1=$v1; # matches every second line

If it is a "feature" please add it to perltrap.
I'm going to test this against various older versions on other boxes and
OSs. ;-)

The behavior you are seeing is *exactly* as documented. I do not
understand why you think this is a bug.

perlop​:

  In scalar context, each execution of m//g finds
  the next match, returning TRUE if it matches, and
  FALSE if there is no further match. The position
  after the last match can be read or set using the
  pos() function; see the pos entry in the perlfunc
  manpage. A failed match normally resets the
  search position to the beginning of the string,
  but you can avoid that by adding the /c modifier
  (e.g. m//gc). Modifying the target string also
  resets the search position.

pos() is not reset after a successful match with /g. pos() is reset after
an unsuccessful match with /g. pos() is reset when a variable is
reassigned.

If you don't want the effects of /g, don't use /g!

Ronald

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant