Bare Basic question.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stirfries
    New Member
    • Jul 2008
    • 4

    #1

    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.

    Code:
    use constant UNSUBSCRIBE => 0;
    
    my ($value) = 'n';
    if ($value == UNSUBSCRIBE) {
    	print 'n equals 0';
    }
    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,
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    "n" evaluates to 0 (zero) in that context. Do this and the results will be different:

    Code:
    use constant UNSUBSCRIBE => 1;
     
    my ($value) = 'n';
    if ($value == UNSUBSCRIBE) {
        print 'n equals 0';
    }

    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.

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      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.

      Comment

      • stirfries
        New Member
        • Jul 2008
        • 4

        #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,

        Comment

        • mlpkumar
          New Member
          • Oct 2008
          • 4

          #5
          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';
          }

          Comment

          • Icecrack
            Recognized Expert New Member
            • Sep 2008
            • 174

            #6
            Originally posted by mlpkumar
            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:

            Code:
             ..code goes here..
            Do not link to other websites for promoting/traffic generation. Only link to helpful resources.

            Comment

            Working...