By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,660 Members | 1,299 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,660 IT Pros & Developers. It's quick & easy.

Bare Basic question.

P: 4
Hello Experts,

I tried googling to get more information about comparison operators in Perl when incompatible data types were involved but wasn't lucky.

I am basically a Java programmer but have been looking at Perl code of late, at work. While debugging a Perl module, I stumbled upon the following code block. I understand Perl has distinct comparison operators for numeric and string data types. But, I am not sure how this works.

Expand|Select|Wrap|Line Numbers
  1. use constant UNSUBSCRIBE => 0;
  2.  
  3. my ($value) = 'n';
  4. if ($value == UNSUBSCRIBE) {
  5.     print 'n equals 0';
  6. }
  7.  
When I run this code block, surprisingly the output of the above code block is 'n equals 0'.

I am not sure how a character ('n') be equal to a numeric number (0). Even the ASCII character value of character ('n') is not 0. Can some Perl guru help me understand what is going on?

Thanks,
Oct 1 '08 #1
Share this Question
Share on Google+
5 Replies


KevinADC
Expert 2.5K+
P: 4,059
"n" evaluates to 0 (zero) in that context. Do this and the results will be different:

Expand|Select|Wrap|Line Numbers
  1. use constant UNSUBSCRIBE => 1;
  2.  
  3. my ($value) = 'n';
  4. if ($value == UNSUBSCRIBE) {
  5.     print 'n equals 0';
  6. }

When a string is compared (and maybe even evaluated) in numeric context perl assigns it a value of 0 (zero). I assume thats because the value (in that context) is false, and in perl 0 equates to false.
Oct 1 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
if you use the warnings pragma

use warnings;

and you should almost always use it, you would have been alerted to part of the problem:

Argument "n" isn't numeric in numeric equal (==) at line .....

if you use the diagnostics module you will get an even more verbose message:

use diagnostics;

but it still might not have explained it fully.
Oct 1 '08 #3

P: 4
When a string is compared (and maybe even evaluated) in numeric context perl assigns it a value of 0 (zero). I assume thats because the value (in that context) is false, and in perl 0 equates to false.
KevinADC,

That was the explanation I was looking for. I was under the assumption that Perl would convert it to its ASCII value like C language does. Apparently, it does not. Thanks for the expert reply. Appreciate it.

Thanks,
Oct 1 '08 #4

P: 4
below code will give you the output what you expected.


use constant UNSUBSCRIBE => 0;

my ($value) = 'n';
if ($value eq UNSUBSCRIBE) {
print 'n equals 0';
}
Oct 2 '08 #5

Icecrack
Expert 100+
P: 174
below code will give you the output what you expected.


use constant UNSUBSCRIBE => 0;

my ($value) = 'n';
if ($value eq UNSUBSCRIBE) {
print 'n equals 0';
}

mlpkumar if your going to help people please use code tags as the posting guidelines say,


REPLY GUIDELINES
Please follow these guidelines when responding to questions.
Provide relevant answers and solutions
Write in clear concise language using correct grammar and spelling
Use CODE tags around your code:

Expand|Select|Wrap|Line Numbers
  1.  ..code goes here.. 
Do not link to other websites for promoting/traffic generation. Only link to helpful resources.
Oct 3 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.