Path: utzoo!attcan!uunet!mcsun!sunic!uupsi!rpi!brutus.cs.uiuc.edu!zaphod.mps.ohio-state.edu!van-bc!ubc-cs!alberta!oha!tony From: tony@oha.UUCP (Tony Olekshy) Newsgroups: comp.lang.perl Subject: Re: numeric from string Summary: Use eval to force Perl's string base interpretation. Message-ID: <407@oha.UUCP> Date: 12 Mar 90 21:17:22 GMT References: <13537@cbnewsd.ATT.COM> <7347@jpl-devvax.JPL.NASA.GOV> Reply-To: tony@oha.UUCP Organization: Olekshy Hoover & Associates Ltd., Edmonton, Alberta, Canada. Lines: 38 In-Reply-To: Message <7347@jpl-devvax.JPL.NASA.GOV> dated 8 Mar 90 19:01:22 GMT Return-Path: 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?]