Path: utzoo!attcan!uunet!jarthur!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Newsgroups: comp.lang.perl Subject: Re: variable is numeric? Message-ID: <7443@jpl-devvax.JPL.NASA.GOV> Date: 16 Mar 90 19:12:31 GMT References: <16918@orstcs.CS.ORST.EDU> Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 24 In article <16918@orstcs.CS.ORST.EDU> pvo@sapphire.OCE.ORST.EDU (Paul O'Neill) writes: : What is the Canonical Test of whether a variable is numeric? I'm not sure what you mean by that. All scalar variables are potentially numeric, and any scalar that has been evaluated in a numeric context has a numeric value. That value will be whatever atof() returned. So non-numeric strings simply have the numeric value 0, along with their string value (a variable actually can have a string value and a numeric value simultaneously-- hopefully these always stay in sync, but a recent patch fixed substr() because it allowed the values to get out of sync (one variable is purposefully forced to be out of sync, and that's $!)). There is no way that I know of (apart from the magical behavior of autoincrement) to tell whether a variable has been referenced in a numeric context. If you're asking about how Perl decides whether to use $# in printing out a number, it does it if it already has a non-zero numeric value, or if the string part matches /^\s*[+-]?\d*(\.?\d*([eE][+-]?\d*)?)?\s*$/ and atof() says it has a non-zero value. (Actually, that regexp is a translation of the C code of looks_like_number(), which is a heap more efficient than that regexp would be.) Larry