Posted by Jason Nugent on September 12, 1998 at 10:38:17:
In Reply to: Re: perl question, re characters posted by Jason Turner on September 12, 1998 at 09:16:34:
: : How do I do this;
: : $value = " (blank)" if ( ($name = "something") && ($value contaings anything but a-z A-Z or 0-9));
: : Is it like this?
: : $value = "" if ( ($name eq "something") and ($value !~ /a-z,A-Z,0-9/));
: $value = "" if ( ($name eq "something") and ($value =~ /[!a-zA-Z0-9]/));
The last character class here should be
[^a-zA-Z0-9] (the ^ represents a negated character.
: And, if you allow underscores
: $value = "" if ( ($name eq "something") and ($value =~ /\W/));
: I think that's what you're asking :)
$value = "" if ( $name eq "something && $value ~ /\W/ ));
Minor points on the choices of and vs &&, and =~ vs ~
I do believe and has a lower priority than &&. No big deal with and, but you might run into problems using its counterpart || vs or. for example:
open (FILE, $file) or die;
vs open (FILE, $file) || die - the die in this case has a higher priority than open and might be tested for first.
about =~ vs ~ - the =~ operator tests and also assigns, so $value will get the results of a match if it is found. Not a big deal since you are setting it to "" afterward anyway if the rest of the statement is true. If its not, though, $value might get changed and you might not want that. Of course, that depends on the order of the test. You're testing for $name eq "something" first, so $value won't get affected if that evaluates to false. Just don't switch the tests. :)
Ah... the subtleties of perl :)