in reply to if multiple conditions & Warinings....
Below the text area when posting you could have seen some tips on how to write a post. <code> tags would have been useful there. UPG: Or Markup in the Monastery could be a useful read.
For you problem ne compares two strings, which /^[+-]\d+$/ is not. It's a matching operation (it's actually m/EXPR/ but with / delimiters around the expression the m can be omitted), which you can use on a string with the binding operator =~ see Binding Operators. And you want to check that $VAR1 is a number before making a number comparison with it. So that's if ($VAR1 !~ /^[\-+]?\d+$/ || $VAR1 != 2).
The next problem: - has a special meaning in a character range, so you have to escape it with \. Putting it in the first place is actually an exception to that, because it isn't ambiguous, but you can escape your - nonetheless, better safe than sorry.
At last, since you seem to be discovering Perl, do turn on warnings and strict, don't wait until you find a bug.
UPG: and maybe unless could make your code easier to read. And I don't see why you would include negative numbers if you're only looking for values different than 2. So you could actually write unless ($VAR1 =~ /^\+?2$/) { do some stuff }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: if multiple conditions & Warinings....
by AnomalousMonk (Archbishop) on Aug 22, 2013 at 01:50 UTC |