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

But that means oct convert a string to a number?strange logic.and according to perl theroy,number and string are one type "scalar".
Anyway,thanks for your answer!

Replies are listed 'Best First'.
Re^3: ' or no?
by ikegami (Patriarch) on Dec 28, 2006 at 03:56 UTC

    Strings, numbers and many other things can be placed in a scalar. Not all scalars are numbers. Not all scalars are strings. Perl will implicitely convert back and forth as needed, possibly resulting in warnings.

    print 2+3; # Number converted to string. print 'a'.3; # N->S print '2'+3; # S->N, N->S print 'z'+3; # S->N (warn), N->S print 3+undef; # U->N (warn), N->S print ''.undef; # U->S (warn) print \$a; # R->S print \$a+0; # R->N->S
Re^3: ' or no?
by ysth (Canon) on Dec 28, 2006 at 04:12 UTC
    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.
      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.
        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.

Re^3: ' or no?
by swampyankee (Parson) on Dec 28, 2006 at 17:21 UTC

    Actually, it's not "strange logic," after all "numbers" and "characters" are just human-readable strings representing the underlying bit representations; every time you type $x = 1234;, the tokenizer converts it to the underlying binary representation. The tokenizer also converts $y&nbps;= '1234'; to a binary form. oct and hex merely make the string to number conversion explicit, in the same way you would use sprintf|sscanf in C or ENCODE|DECODE in old (pre-Fortran 77) dialects of Fortran (now, you would use write|read). Perl automatically "stringifies" and "numerifies" based on context; print needs strings as inputs, so Perl automatically converts numbers to strings. sin needs a numerical argument, so Perl will convert a string to its numerical representation.

    emc

    At that time [1909] the chief engineer was almost always the chief test pilot as well. That had the fortunate result of eliminating poor engineering early in aviation.

    —Igor Sikorsky, reported in AOPA Pilot magazine February 2003.