Solved

-- == null ??????

Posted on 1998-09-15
4
135 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
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
But since == will evaluate undef to be ==0, anything numerically ==0 will be ==undef
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now