?
Solved

-- == null ??????

Posted on 1998-09-15
4
Medium Priority
?
143 Views
Last Modified: 2010-03-05
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?
0
Comment
Question by:ueihp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1204871
perl -we '$rev = "--"; if( ($rev=="new") ){ $rev = "new"; }'
Argument "new" isn't numeric in eq at -e line 1.
Argument "--" isn't numeric in eq at -e line 1.
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1204872
ozo's right

You'd be better off with

if ( !defined($rev) ) {
   $rev = 'new';
} elsif ($rev ne 'new') {
   $rev  = 'rev' . $rev;
}



0
 
LVL 1

Accepted Solution

by:
stormerider earned 0 total points
ID: 1204873
Or, closer to the original code,

if ( ($rev == undef) || ($rev eq "new") ) {
   $rev = "new";
} else {
   $rev = "rev" . $rev;
}


0
 
LVL 84

Expert Comment

by:ozo
ID: 1204874
But since == will evaluate undef to be ==0, anything numerically ==0 will be ==undef
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question