in reply to resetting Perl RegEx backreferences

I believe the error in your ways is not checking to see if the match succeeded before using the match capture variable values. Instead of this:

$dorient =~ /^([^_]+)_/; $rs[2] = $1;

You should be checking to ensure that the match succeeded:

if( $dorient =~ /^([^_]+)_/ ) { $rs[2] = $1; }

This should be true of almost any time you rely on $1; you should first check to see if $1 is meaningful by verifying the success of its corresponding regex match.

The behavior is described in perlop wherein it states: "NOTE: failed matches in Perl do not reset the match variables...."


Dave

Replies are listed 'Best First'.
Re^2: resetting Perl RegEx backreferences
by samizdat (Vicar) on Mar 08, 2006 at 20:10 UTC
    Ah, very good. Much cleaner! Thanks, Dave! <g>

    Don Wilde
    "There's more than one level to any answer."