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

Yes, but if you say $scalar = 0123 (using an octal constant) the number assigned is 83, and the string value of $scalar is also "83". All perl does for you is decimal to string conversion, so there's the oct() function to convert other (string!) representations of numbers into numbers.

Replies are listed 'Best First'.
Re^4: ' or no?
by xiaoyafeng (Deacon) on Dec 28, 2006 at 05:02 UTC
    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. :)
      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
      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.

      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.