cutter has asked for the wisdom of the Perl Monks concerning the following question:
This is probably something blindingly simple but I have been knocking myself out for about a day now. Adding some functioning to old code and now a subroutine that validates a user id will not work correctly.
This is the existing code, works fine.
sub validate_owner{ my ($str) = @_; print "Owner is $str in sub_validate_owner\n" if ($debug); # Remove leading and trailing whitespace. $str =~ s{\A \* | \s* \z}{}gxm; if ($str =~ /nbk[A-z0-9]{4}/ or /nbd[A-z0-9]{4}/ or /root/){ print "Error $str is not a valid job owner\n"; $error = 1; return 1; } else { #No match for string. return; } } my $owner = get_jobname $_; print "Owner line is $owner\n" if ($debug); # Split id@hostname and get the id value. $owner = (split /@/,$owner)[0]; print "Owner ID is $owner\n" if ($debug); validate_owner($owner);
New code that doesn't work. No change to the sub, just that the value is coming from a hash. The sub will find the first match, but not the nbs or root match in the regex.
sub validate_owner{ my ($str) = @_; print "Owner is $str in sub_validate_owner\n" if ($debug); # Remove leading and trailing whitespace. $str =~ s{\A \* | \s* \z}{}gxm; if ($str =~ /nbk[A-z0-9]{4}/ or /nbd[A-z0-9]{4}/ or /root/){ print "Error $str is not a valid job owner\n"; $error = 1; return 1; } else { #No match for string. return; } } if (defined $jobs{'owner'}){ my $owner = $jobs{'owner'}; # Split id@hostname and get the id value. $owner = (split /@/,$owner)[0]; print "Owner ID is $owner\n" if ($debug); validate_owner($owner); }
Now for the fun part. If you change the subroutine to this, it works
sub validate_owner{ my ($str) = @_; print "Owner is $str in sub_validate_owner\n" if ($debug); # Remove leading and trailing whitespace. $str =~ s{\A \* | \s* \z}{}gxm; print "Owner is $str in sub_validate_owner\n" if ($debug); # if ($str =~ /nbk[A-z0-9]{4}/){ print "Error $str is not a valid job owner\n"; $error = 1; return 1; } if ($str =~ /nbd[A-z0-9]{4}/){ print "Error $str is not a valid job owner\n"; $error = 1; return 1; } if ($str =~ /root/){ print "Error $str is not a valid job owner\n"; $error = 1; return 1; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Not matching REGEX
by FunkyMonk (Bishop) on Aug 30, 2007 at 18:04 UTC | |
by cutter (Acolyte) on Aug 30, 2007 at 18:21 UTC | |
|
Re: Not matching REGEX
by ikegami (Patriarch) on Aug 30, 2007 at 18:48 UTC | |
|
Re: Not matching REGEX
by suaveant (Parson) on Aug 30, 2007 at 18:17 UTC | |
by cutter (Acolyte) on Aug 30, 2007 at 18:40 UTC | |
|
Re: Not matching REGEX
by jwkrahn (Abbot) on Aug 30, 2007 at 20:35 UTC |