467,154 Members | 788 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,154 developers. It's quick & easy.

Bare Basic question.

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
  • viewed: 1138
Share:
5 Replies
KevinADC
Expert 2GB
"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 2GB
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
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
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+
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.

Similar topics

reply views Thread by John J. Lee | last post: by
2 posts views Thread by Richard A. Wells | last post: by
1 post views Thread by Rob Bolton | last post: by
5 posts views Thread by Mark Fox | last post: by
2 posts views Thread by pgnPoster | last post: by
26 posts views Thread by Don Calloway | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.