473,387 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
5 1232
KevinADC
4,059 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
4,059 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
174 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

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

Similar topics

0
by: John J. Lee | last post by:
Bare "except:", with no exception specified, is nasty because it can easily hide bugs. There is a case where it seemed useful to me in the past. Now it seems like a bad idea to me (but I think...
2
by: Richard A. Wells | last post by:
All I wanted to do was implement a web service where I'd receive an XML document and return one in response. I'd already figured out how to use XmlReader and XmlWriter classes to do the XML work I...
1
by: Rob Bolton | last post by:
Hi there. I just installed Visual Studio 2003 Professional and discovered that the most basic program hangs in the IDE. It runs fine from the command line however. #include "stdafx.h" int...
5
by: Mark Fox | last post by:
Hello, I have a site that was working great until I uploaded it to a server with the .NET Framework 1.1 on it (it was compiled on a box with .NET 1.0 SP2). I am getting an error from the SMTP...
13
by: parley | last post by:
After several years of programming WWW applications in ASP.NET (and several other frameworks) our division has come to what might seem a counterintiutive conclusion: Writing ASP.NET code in a...
13
by: Pete | last post by:
I'm cross posting from mscom.webservices.general as I have received no answer there: There has been a number of recent posts requesting how to satisfactorily enable BASIC authorization at the...
2
by: Phil Lee | last post by:
What's the general opinion on which of these to choose? I see that the SoapDocumentServiceAttribute defaults to literal/wrapped, but this article -...
2
by: pgnPoster | last post by:
I am having the dickens of a time figuring this out. I've been googling high and low, but all the examples I find involve the iframe's src containing some HTML albeit minimal. I just want it to...
26
by: Don Calloway | last post by:
I have created a <filename>.db1 (where <filenameis the name given to the database) Access database that is being used in multi-user mode. When the <filename>.db1 file is opened by a user, the...
5
by: Ian11 | last post by:
I have been searching for weeks to find asp message board code in its rawest form so that I can modify it to my needs. What I am looking for is code that will process a simple html for such as: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.