Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: Numification of strings

by BrowserUk (Pope)
on Aug 02, 2010 at 05:39 UTC ( #852409=note: print w/replies, xml ) Need Help??

in reply to Numification of strings

Isn't this behaviour mainly hiding ugly bugs?

What would you have Perl do?

Replies are listed 'Best First'.
Re^2: Numification of strings
by eyepopslikeamosquito (Chancellor) on Aug 02, 2010 at 08:27 UTC

    Unlike Perl and PHP, both Python and Ruby don't allow you to convert strings implicitly to numbers like this, instead demanding that you explicitly convert them (by calling int() in Python and the to_i() method in Ruby). Curiously, Ruby's to_i() method, a la Perl and C's atoi(), happily converts "2abc" to the integer 2, while Python's stricter int() function instead issues a run time error. I know about all this from playing too much multi-language golf. :- ) In golf, these explicit conversions are certainly a chronic pest. In normal programming, however, I don't feel strongly -- though I suspect demanding explicit conversion may help the programmer avoid some data conversion boo boos.

      What's implicit about using numeric operators on strings? If you use string operators on strings, you get string behavior. If you use numeric operators (which you must do explicitly), you get numeric behavior. What isn't explicit about that?

        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.

        well it's implicit if you use dynamically typed variables, i.e. you can't tell in advance if it's an integer or a number in the scalar.

        Cheers Rolf

      > Curiously, Ruby's to_i() method, a la Perl

      Not that surprising, Ruby is semantically a Perl clone with new syntax and Smalltalk OOP.

      It's a kind of newspeak... (I have a friend who discovered the flip-flop operator just after learning Ruby ... after working in a Perl project for 2 years. :)

      The only semantic resemblance to python I could find so far is the way new variables are bound and scoped.

      Cheers Rolf

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://852409]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2018-06-23 18:31 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.