in reply to Re^3: ' or no?
in thread ' or no?

I see. perl stored all numbers in decimal background. right? In other word, oct and hex is needless. We could use print to replace them. maybe it's TIMTOWTDI. :)

Replies are listed 'Best First'.
Re^5: ' or no?
by BrowserUk (Patriarch) on Dec 28, 2006 at 05:14 UTC
Re^5: ' or no?
by xiaoyafeng (Deacon) on Dec 28, 2006 at 11:33 UTC
    Thanks all gurus! I admit language is a barrier to communicate with monks,especially for a perl beginner:)
    why
    print oct(0xff); # 173
    ? because oct function require a "string" parameter,So perl convert(stealthily) 0xff(number) to 255(string),then oct convert 255(octal) to 173(decimal). right?I hopefully don't misunderstand reply being provided.
    But you have to admit it's a trap to a beginner.Although I add "use warnings" and" use strict",perl don't give any useful messages.It just return 173. For me,I prefer to choose print(printf):
    my $num = 0xff; print oct($num); #wrong! printf('xo',$num);# right!
    p.s. maybe I can use pack,but it's another issue.

      oct converts *from* oct/hex/bin (oct in your case).
      Conversion from dec is done automatically when needed.
      printf converts *to* dec/oct/hex/bin.

      >perl -le "printf '%d', '255' 255 >perl -le "printf '0%o', oct '0377' 0377 >perl -le "printf '0x%X', oct '0xFF' 0xFF >perl -le "printf '0b%b', oct '0b11111111' 0b11111111
Re^5: ' or no?
by ysth (Canon) on Dec 28, 2006 at 05:39 UTC
    How "use print to replace them"? If you have a file that has "0xdeadbeef" stored in it, and you want to read it in and show what the decimal equivalent is, you use oct or hex. print doesn't do that. Again, oct is for converting strings containing a number formatted in hex, octal, or binary into something perl can use as a number.
Re^5: ' or no?
by ikegami (Patriarch) on Dec 28, 2006 at 06:17 UTC

    Decimal is a string representation of a number, so no. The number is stored in the machine's native format for signed integers and/or the machine's native format for real numbers.

    Note: The decimal representation of a number is sometimes present in the scalar for efficiency reasons. However, that's only used when the number needs to be converted to a string and not when doing any type of mathematical operation.