in reply to "force string context"?

I'm much surprised by the programmer's comments.

I must confess that I am not much into Perl internals, but a quick test program did not convince me. Try this:

$number=1082021975087815; $string="1082021975087815"; $converted="$number"; print "$number\n$string\n$converted\n";
Output:
1.08202197508782e+015 1082021975087815 1.08202197508782e+015
Wrapping the variable in "" does not impose string context if beforehand the damage was already done. One should rather make sure that initially the number was stored as a string (by putting it in " " or ' ') and then do only string-like operations on this variable.

CountZero

"If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Replies are listed 'Best First'.
Re: Re: "force string context"?
by bunnyman (Hermit) on Apr 16, 2004 at 16:56 UTC

    I suspect that the guilty programmer was practicing cargo-cult techniques.

    Putting "$variables" in quotes might make people feel better about the way that Perl can "randomly change scalars from data to garbage, without warning or provocation," but it actually serves no real purpose. People coming from a language with strong typing generally feel that there is something strange about Perl, because they are giving up direct control over the representation of data.

    The reality of the situtation is that strings stay strings forever and ever, right up to the time that they are used in a numeric operation, and numbers stay numbers forever and ever, until they are used in string operations. So anybody who thinks that Perl might mangle their data should relax, and if they realize that "eq" and == are different, everything will be just fine.