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; }

In reply to Not matching REGEX by cutter

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.