<?xml version="1.0" encoding="windows-1252"?>
<node id="852595" title="Re^4: Numification of strings" created="2010-08-03 00:54:30" updated="2010-08-03 00:54:30">
<type id="11">
note</type>
<author id="176576">
eyepopslikeamosquito</author>
<data>
<field name="doctext">
&lt;P&gt;
Yes, I guess it is explicit in Perl.
In terms of language design, the interesting question is whether the Python/Ruby demand to
explicitly cast/coerce by calling &lt;C&gt;int&lt;/C&gt; is "stronger/safer typing"
or "ugly explicit casting" (frowned upon in C++, for example). Let's clarify with a little test program.
&lt;/P&gt;

&lt;P&gt;
In Perl, running:
&lt;CODE&gt;
use strict;
use warnings;
my $x = "2abc";
my $y = $x + 42;
print "x=", $x, " y=", $y, "\n";
&lt;/CODE&gt;
produces:
&lt;CODE&gt;
Argument "2abc" isn't numeric in addition (+) at f.pl line 4.
x=2abc y=44
&lt;/CODE&gt;
That seems reasonable to me.
&lt;/P&gt;

&lt;P&gt;
In Python, running:
&lt;CODE&gt;
x = "2abc"
y = x + 42
print "x=", x, "y=", y, "\n"
&lt;/CODE&gt;
produces:
&lt;CODE&gt;
Traceback (most recent call last):
  File "f.py", line 2, in &lt;module&gt;
    y = x + 42
TypeError: cannot concatenate 'str' and 'int' objects
&lt;/CODE&gt;
&lt;/P&gt;

&lt;P&gt;
while running:
&lt;CODE&gt;
x = "2abc"
y = int(x) + 42
print "x=", x, "y=", y, "\n"
&lt;/CODE&gt;
also fails with:
&lt;CODE&gt;
Traceback (most recent call last):
  File "f2.py", line 2, in &lt;module&gt;
    y = int(x) + 42
ValueError: invalid literal for int() with base 10: '2abc'
&lt;/CODE&gt;
Python's &lt;C&gt;int&lt;/C&gt; function is stricter in what it accepts than Perl and Ruby.
Finally, running:
&lt;CODE&gt;
x = "2abc"
y = int(x[0:1]) + 42
print "x=", x, "y=", y, "\n"
&lt;/CODE&gt;
produces the desired result:
&lt;CODE&gt;
x= 2abc y= 44
&lt;/CODE&gt;
&lt;/P&gt;

&lt;P&gt;
In Ruby, running:
&lt;CODE&gt;
x = "2abc"
y = x + 42
print "x=", x, "y=", y, "\n"
&lt;/CODE&gt;
produces:
&lt;CODE&gt;
f.rb:2:in `+': can't convert Fixnum into String (TypeError)
	from f.rb:2
&lt;/CODE&gt;
Just like Python.
While running:
&lt;CODE&gt;
x = "2abc"
y = x.to_i() + 42
print "x=", x, " y=", y, "\n"
&lt;/CODE&gt;
produces:
&lt;CODE&gt;
x=2abc y=44
&lt;/CODE&gt;
Ruby's &lt;C&gt;to_i&lt;/C&gt; method being less strict than Python's &lt;C&gt;int&lt;/C&gt; function.
&lt;/P&gt;

&lt;P&gt;
I guess Ruby and Python need to do it that way because the &lt;C&gt;+&lt;/C&gt; operator is overloaded for both numeric addition and string concatenation, while Perl has a separate string concatenation operator, and so there is no ambiguity.
&lt;/P&gt;

&lt;P&gt;
&lt;B&gt;Update:&lt;/B&gt; Python's stricter typing can produce other subtle differences. For example, in Python both &lt;C&gt;5 * "X"&lt;/C&gt; and &lt;C&gt;"X" * 5&lt;/C&gt; produce five Xs. Not so in Perl or Ruby. That is, the string multiply operator is commutative in Python, but not in Perl or Ruby. Not sure if string multiply commutativity is a bug or a feature. Curiously, the new Perl 5.10 smart match operator &lt;C&gt;~~&lt;/C&gt; was commutative in Perl 5.10.0, then that was deemed a mistake and it was made non-commutative in Perl 5.10.1.
&lt;/P&gt;
</field>
<field name="root_node">
852379</field>
<field name="parent_node">
852543</field>
</data>
</node>
