develooper Front page | perl.perl5.porters | Postings from January 2008

next resetting match variables - bug or feature?

From:
Nicholas Clark
Date:
January 31, 2008 15:03
Subject:
next resetting match variables - bug or feature?
Message ID:
20080131230252.GD38653@plum.flirble.org
I'm trying to work out why ext/B/t/deparse.t is issuing a warning on 5.10.x,
but not 5.11. I have it down to this test case:

#!./perl

use warnings;
use strict;

$/ = "\n####\n";
while (<DATA>) {
    chomp;
    my ($skip_reason);
    s/^#\s*SKIP\s*(.*)\n//m and $skip_reason = $1;
    if ($skip_reason && $skip_reason =~ s/^\?//) {
	my $temp = eval $skip_reason;
	if ($@) {
	    die "# In SKIP code reason:\n# $skip_reason\n$@";
	}
	$skip_reason = $temp;
    }

    s/#\s*(.*)$//mg;
    my ($num, $testname) = $1 =~ m/(\d+)\s*(.*)/;

    if ($skip_reason) {
	next;
    }
}


__DATA__
# SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
# 49 each @array;
each @ARGV;
each @$a;
####
my $a = {};
my $b = \{};
my $c = [];
my $d = \[];


$ ./perl -le/perl -e 'print $]'
5.011000
$ ./perl -Ilib -w ~/test/dp1.pl
$

vs

$ ./perl -Ilib -w ~/test/dp1.pl
Use of uninitialized value $1 in pattern match (m//) at /afs/wu-wien.ac.at/home/ma/nclark/test/dp1.pl line 20, <DATA> chunk 2.
$ ./perl -le 'print $]'
5.010000
$


[I'm failing to make an intelligable test case as a single perl script. It's
late and I'm tired]

Anyway, under 5.10.x the SKIP means that $skip_reason is true, so the next;
is invoked. Whereas under 5.11 there is no next, so when the s/#\s*(.*)$//mg;
fails to match, it seems that $1 is still set from the previous loop
iteration.

Is this a bug?

I see nothing in the documentation that says that match variables behave
differently when a loop repeats via 'next' compared with when it repeats
"naturally".

Nicholas Clark



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About