Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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 int is "stronger/safer typing" or "ugly explicit casting" (frowned upon in C++, for example). Let's clarify with a little test program.

In Perl, running:

use strict; use warnings; my $x = "2abc"; my $y = $x + 42; print "x=", $x, " y=", $y, "\n";
Argument "2abc" isn't numeric in addition (+) at line 4. x=2abc y=44
That seems reasonable to me.

In Python, running:

x = "2abc" y = x + 42 print "x=", x, "y=", y, "\n"
Traceback (most recent call last): File "", line 2, in <module> y = x + 42 TypeError: cannot concatenate 'str' and 'int' objects

while running:

x = "2abc" y = int(x) + 42 print "x=", x, "y=", y, "\n"
also fails with:
Traceback (most recent call last): File "", line 2, in <module> y = int(x) + 42 ValueError: invalid literal for int() with base 10: '2abc'
Python's int function is stricter in what it accepts than Perl and Ruby. Finally, running:
x = "2abc" y = int(x[0:1]) + 42 print "x=", x, "y=", y, "\n"
produces the desired result:
x= 2abc y= 44

In Ruby, running:

x = "2abc" y = x + 42 print "x=", x, "y=", y, "\n"
f.rb:2:in `+': can't convert Fixnum into String (TypeError) from f.rb:2
Just like Python. While running:
x = "2abc" y = x.to_i() + 42 print "x=", x, " y=", y, "\n"
x=2abc y=44
Ruby's to_i method being less strict than Python's int function.

I guess Ruby and Python need to do it that way because the + operator is overloaded for both numeric addition and string concatenation, while Perl has a separate string concatenation operator, and so there is no ambiguity.

Update: Python's stricter typing can produce other subtle differences. For example, in Python both 5 * "X" and "X" * 5 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 ~~ was commutative in Perl 5.10.0, then that was deemed a mistake and it was made non-commutative in Perl 5.10.1.

In reply to Re^4: Numification of strings by eyepopslikeamosquito
in thread Numification of strings by LanX

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (3)
    As of 2014-07-24 02:38 GMT
    Find Nodes?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:

      Results (156 votes), past polls