This blog no longer exists.
You will be redirected to something cool.

Sunday, August 21, 2011

Adding, Concatenation, Ruby, & Perl

I've noticed some similarities between Ruby and Perl, but have noticed some really strange differences. One of these is this:

0.1 + 0.1
0.1 + "0.1"
"0.1" + "0.1"

In Ruby, the answers would be as follows:
Error: String can't be coerced into Float (TypeError)


In Perl:

I'll have to do some digging to see why it happens like this in Perl, but it's really strange to me, perhaps because I learned it, well, the Ruby way.


Perl decides what to do based on the operator. The mathematic operators, like +, always do math. Perl automatically converts strings to numbers.

If you want to join strings, you can use the concatenation operator, . (the single dot). That will always treat its operands as strings.

Perl depends on context, and what you are doing is more important what you are doing it with.