biohisham has asked for the wisdom of the Perl Monks concerning the following question:

From perldoc -f hex, the hex strings may only represent integers, this can mean that an EXPR passed to hex has to be wrapped around by quotes, but what will hex and oct interpret and return when the EXPR is not enclosed within quotations is remarkably different. What I am seeking wise monks is your graceful guidance into how the darker sides of these functions perform and yield the outcomes exhibited below for I thought perl could also understand the context of the EXPR passed in conjunction of the function used regardless of quotation for such functions as ord, oct and hex...
C:\>perl - print hex '0xAf'; print $/; print oct '0xAf'; print $/; print oct 0xAf; print $/; print hex 0xAf; __END__ 175 175 125 373


Excellence is an Endeavor of Persistence. A Year-Old Monk :D .

Replies are listed 'Best First'.
Re: To hex EXPR or 'EXPR' is my question
by BrowserUk (Patriarch) on Nov 07, 2010 at 08:55 UTC

    In the latter two examples, the hex has already been interpreted as a hex value before the hex function is called. So what gets passed to the hex function is the decimal string representation of the number. This may clarify:

    $n = 0xaf;; print $n;; 175 print hex $n;; 373 print oct $n;; 125 print hex '175';; 373 print oct '175';; 125

    Ie. both hex & oct take strings which they then interpret as numbers in a default base (16 or 8) respectively. If you pass them a number, it is converted to a string first.

    But it also gets more confusing as they will also interpret number is each others bases if that is indicated by a prefix in their arguments:

    print hex '0x' . '175';; 373 print hex '0' . '175';; 373 print oct '0x' . '175';; 373 print oct '0' . '175';; 125

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      But it also gets more confusing as they will also interpret number is each others bases if that is indicated by a prefix in their arguments:
      print hex '0' . '175';; 373
      But 175 octal is 125 decimal. I would say the confusing thing may be that oct does interpret a number as hex when prefixed by '0x', but hex doesn't interpret a number as octal when it's prefixed by 0(?!x). Now, I wouldn't want hex to do so, as hex bitfields are often fixed length, and will have leading 0s.
      Thanks folks, this made me see this confusion under another light, it seems after all that perl interprets EXPR so quickly that both hex and oct become useless anatomical-appendices (metaphorically speaking) if EXPR was passed preceded by '0x' in a string, not trying to make a generalization here rather than a mere observation and not trying to dig myself a hole by including the '0'-preceded EXPRs either (octals may have another behavior I guess), so correct me if I am wrong with regard to the following:
      print 0xCA; 202 print hex '0xCA'; #<-- what use does hex have? 202 print oct '0xCA'; #<-- what use does oct have? 202
      The issue is, what can be gotten out of this behavior is brain-bombing and to quote EXPR or not warrants someone being absolutely aware of how they're going about it since the docs and faqs are kinda shallow when it comes to making us aware of this..What do you monks think??


      Excellence is an Endeavor of Persistence. A Year-Old Monk :D .

        Interpreting strings that are read from external sources:

        print 'deadbeef';; deadbeef print '0x'.'deadbeef';; 0xdeadbeef print 0+ '0x'.'deadbeef';; Argument "0x" isn't numeric in addition (+) 0deadbeef print hex 'deadbeef';; 3735928559 print '77777777';; 77777777 print '0'.'77777777';; 077777777 print 0+'0'.'77777777';; 077777777 print oct '77777777';; 16777215

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: To hex EXPR or 'EXPR' is my question
by kcott (Archbishop) on Nov 07, 2010 at 09:11 UTC

    This might provide some insight:

    $ perl -wE 'say hex 0xAf; say hex q{0xAf}; say hex qq{0xAf};' 373 175 175 $ perl -wE 'say sprintf q{%s}, 0xAf; say sprintf q{%s}, q{0xAf}; say s +printf q{%s}, qq{0xAf};' 175 0xAf 0xAf $ perl -wE 'say hex 175' 373 $ perl -wE 'say oct 0xAf; say oct q{0xAf}; say oct qq{0xAf};' 125 175 175 $ perl -wE 'say oct 175' 125

    In oct it says: "If EXPR happens to start off with 0x , interprets it as a hex string."

    Just for reference, here's the hex link.

    -- Ken