PERL : check + or - sign in a variable

I have a variable $max = -3;
It can be $max = +3;

I need to check if this variable is a positive/negative value.
if its positive, should print "positive" if not "negative"

How can this be done?
Thanks in advance

Hi,

Just check wheather the variable have value greater than or equal to zero then its positive else negative.

if ( $max >= 0 )
{
   print "Positive\n";
}
else
{
  print "Negative\n";
}

Note: Above works when the variable is integer scalar variable.

if it is string scalar variable, use regex.

if ( $max =~/^\+/ )
{
   print "Positive\n"
}
elsif ( $max =~/^\-/ )
{
  print "Negative\n";
}
else
{
   print "Positive\n";
}

Looks like the integer test works with strings as well.

$
$ perl -le '$x = "0"; if ($x >= 0) {print "positive"} else {print "negative"}'
positive
$
$ perl -le '$x = "1"; if ($x >= 0) {print "positive"} else {print "negative"}'
positive
$
$ perl -le '$x = "9"; if ($x >= 0) {print "positive"} else {print "negative"}'
positive
$
$ perl -le '$x = "+9"; if ($x >= 0) {print "positive"} else {print "negative"}'
positive
$
$ perl -le '$x = "+0.99"; if ($x >= 0) {print "positive"} else {print "negative"}'
positive
$
$ perl -le '$x = "-0.99"; if ($x >= 0) {print "positive"} else {print "negative"}'
negative
$
$ perl -le '$x = "-1"; if ($x >= 0) {print "positive"} else {print "negative"}'
negative
$
$

tyler_durden