in reply to To hex EXPR or 'EXPR' is my question

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.
RIP an inspiration; A true Folk's Guy

Replies are listed 'Best First'.
Re^2: To hex EXPR or 'EXPR' is my question
by JavaFan (Canon) on Nov 07, 2010 at 15:59 UTC
    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.
Re^2: To hex EXPR or 'EXPR' is my question
by biohisham (Priest) on Nov 07, 2010 at 18:07 UTC
    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.