On 2006–07–07, at 10:18, Adam Witney wrote:

is there a way in Perl to force a variable to be a number rather than a
string?

Yes. You already discovered it: perform a redundant mathematical operation on it. The usual idiom is to add zero to it. (Conversely, to force something to be a string, append an empty string to it.)

What's happening internally is that (among other things), perl variables can accommodate for both string and numeric representations of the variable's value, but they're filled in lazily: a string is not converted to a number until the variable is used in a numeric context (and vice versa). Despite this laziness, Perl is usually able to do what you mean as far as numbers and strings are concerned, but sometimes you have to give it a hint. For more information, see the perlnumber man page.
--
Dominic Dunlop

Reply via email to