ig has asked for the wisdom of the Perl Monks concerning the following question:
I am seeking enlightenment to understand the behavior of the following program and to know what tools are available to understand/debug/display what is going on.
My test program:
use strict; use warnings; use Data::Dumper; my @strings = qw(exception:tex exception:mex exception:mex asdf tex:ex +ception:mex asdf exception:mex asdf asdf exception:mex); foreach (1..4) { my @filtered = grep { /exception:/g && !/\Gtex/ } @strings; print Dumper(\@filtered); }
produces the following output (perl 5.10.0 on CentOS 5.3):
$VAR1 = [ 'exception:mex', 'exception:mex', 'tex:exception:mex', 'exception:mex', 'exception:mex' ]; $VAR1 = []; $VAR1 = [ 'exception:mex', 'exception:mex', 'tex:exception:mex', 'exception:mex', 'exception:mex' ]; $VAR1 = [];
I am expecting the same output from each iteration.
One way to get my expected output is to add "pos = 0;".
use strict; use warnings; use Data::Dumper; my @strings = qw(exception:tex exception:mex exception:mex asdf tex:ex +ception:mex asdf exception:mex asdf asdf exception:mex); foreach (1..4) { my @filtered = grep { pos = 0; /exception:/g && !/\Gtex/ } @string +s; print Dumper(\@filtered); }
which produces
$VAR1 = [ 'exception:mex', 'exception:mex', 'tex:exception:mex', 'exception:mex', 'exception:mex' ]; $VAR1 = [ 'exception:mex', 'exception:mex', 'tex:exception:mex', 'exception:mex', 'exception:mex' ]; $VAR1 = [ 'exception:mex', 'exception:mex', 'tex:exception:mex', 'exception:mex', 'exception:mex' ]; $VAR1 = [ 'exception:mex', 'exception:mex', 'tex:exception:mex', 'exception:mex', 'exception:mex' ];
I don't understand the behavior and I don't know what tools are available to easily inspect the behavior. I have used gdb to debug RE code in the past, but it is a bit painful.
|
|---|