Solved

-- == null ??????

Posted on 1998-09-15
4
140 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
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
cpan issue 1 65
rename outfile before writing 2 74
perl to convert excel to csv 3 253
Vb script to unzip a files and rename the files 5 95
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 …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

856 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