[comp.lang.perl] numeric from string

weiss@cbnewsd.ATT.COM (edward.j.weiss) (03/08/90)

To start out I'm using Release 3.0 PatchLevel 12.

Try out:

	perl -e '$m="0777"; $n = $m + 1; print $n,"\n";'

What should the answer be? 

I would guess that 0777 would be converted to a number by
using 8 as a base, but it doesn't.  Base 10 is used.
MisFeature?

I found trying to use chmod like:

	$mode=<>; chop; chmod($mode,$file);
-- 

Ed Weiss	   "I thought it was generally accepted, sir, that
att!ihlpf!spock     Vulcans are an advanced and most honorable race."
		   "They are, they are.  And damn annoying at times."

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (03/09/90)

In article <13537@cbnewsd.ATT.COM> weiss@cbnewsd.ATT.COM (edward.j.weiss,ihp,) writes:
: To start out I'm using Release 3.0 PatchLevel 12.
: 
: Try out:
: 
: 	perl -e '$m="0777"; $n = $m + 1; print $n,"\n";'
: 
: What should the answer be? 
: 
: I would guess that 0777 would be converted to a number by
: using 8 as a base, but it doesn't.  Base 10 is used.
: MisFeature?

No, there are too many strings that come in from the outside world that
have leading zeros and yet want to be decimal.  Base conversion only
happens where you have control of it--in the tokener, and by explicit
use of oct() and hex().  Doing it the other way would cause many more
problems than it would solve.

: I found trying to use chmod like:
: 
: 	$mode=<>; chop; chmod($mode,$file);

To stretch the example of oct() from the manual:

	$mode = <>;
	$mode = oct($mode) if $mode =~ /^0/;
	chmod($mode, $file);

(It's unnecessary to do the chop, since ordinary numeric conversion will
ignore it.)

If you wanted to always interpret it as octal even if they didn't input
the leading zero, you would just drop the if modifier.

Larry

chuck@ksr.UUCP (Chuck Shavit) (03/09/90)

In article <13537@cbnewsd.ATT.COM> weiss@cbnewsd.ATT.COM (edward.j.weiss,ihp,) writes:
>Try out:
>
>	perl -e '$m="0777"; $n = $m + 1; print $n,"\n";'
>
>What should the answer be? 
>

Indeed, the duality of a numeric string (both a binary number and a
character string) is buggy.  Here is an example:

% perl -e '$n = 1984; substr($n,1,3) = "812"; printf "%s %d\n",$n,$n;'


Chuck Shavit

tony@oha.UUCP (Tony Olekshy) (03/13/90)

In message <7347@jpl-devvax.JPL.NASA.GOV>, lwall@jpl-devvax.JPL.NASA.GOV
(Larry Wall) writes:
|
| In article <13537@cbnewsd.ATT.COM> weiss@cbnewsd.ATT.COM (edward.j.weiss,ihp,)
| writes:
| : 
| : 	perl -e '$m="0777"; $n = $m + 1; print $n,"\n";'
| : 
| : What should the answer be? 
| : 
| : I would guess that 0777 would be converted to a number by
| : using 8 as a base, but it doesn't.  Base 10 is used.
| : MisFeature?
| 
| To stretch the example of oct() from the manual:
| 
| 	$mode = <>;
| 	$mode = oct($mode) if $mode =~ /^0/;
| 	chmod($mode, $file);
| 
| If you wanted to always interpret it as octal even if they didn't input
| the leading zero, you would just drop the if modifier.

If you have a numeric string $s and want to use Perl's leading 0 base
mechanism, use $n = eval("$s");  Note that you can force base conversion
to octal with $n = eval("0$s"); which is what I used in my chmod script.
Of course, I forgot about oct().

	perl -e '$s = "10"; print eval("$s"),"\n";'     --> 10
	perl -e '$s = "010"; print eval("$s"),"\n";'    --> 8
	perl -e '$s = "0x10"; print eval("$s"),"\n";'   --> 16

--
Yours, etc., Tony Olekshy (...!alberta!oha!tony or tony@oha.UUCP).

Just another perl.

[I'm sorry, I couldn't help it.  Now where's my velvet housecoat?]