ueihp
asked on
-- == null ??????
I have this simple logic code for perl
if ( ($rev==undef) || ($rev=="new") ) {
$rev = "new"; }
else {
$rev = "rev" . $rev;
}
now this works fine when I pass in normal characters to $rev
but when I set $rev = "--" or "-"
it assumes $rev is undef and sets it to "new".
Is - a special character in perl?
why is this code not working properly and how to I get it to do what I want it to do?
if ( ($rev==undef) || ($rev=="new") ) {
$rev = "new"; }
else {
$rev = "rev" . $rev;
}
now this works fine when I pass in normal characters to $rev
but when I set $rev = "--" or "-"
it assumes $rev is undef and sets it to "new".
Is - a special character in perl?
why is this code not working properly and how to I get it to do what I want it to do?
ozo's right
You'd be better off with
if ( !defined($rev) ) {
$rev = 'new';
} elsif ($rev ne 'new') {
$rev = 'rev' . $rev;
}
You'd be better off with
if ( !defined($rev) ) {
$rev = 'new';
} elsif ($rev ne 'new') {
$rev = 'rev' . $rev;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
But since == will evaluate undef to be ==0, anything numerically ==0 will be ==undef
Argument "new" isn't numeric in eq at -e line 1.
Argument "--" isn't numeric in eq at -e line 1.