Automatic string to number conversion or casting in Perl

In most of the programming languages the type of the operands defines how an operator behaves. So "adding" two numbers does numerical adding while "adding" two strings together concatenates them. This is also called operator overloading.

Perl, mostly works in the opposite way.

In Perl the operator is the one that defines how the operands are used.

This means if you are using a numerical operation (e.g. addition) then both values are automatically converted to numbers. If you are using a string operation (e.g. concatenation) then both values are automatically converted to strings.

C programmers would probably call these conversions "casting" but this is word is not used in the Perl world. Probably because the whole thing is automatic.

Perl does not care if you write something as number or as string. It converts between the two automatically based on the context.

The number => string conversion is easy. It is just a matter of imaging as if "" appeared around the number value.

The string => number conversion can be a bit more tricky. If the string looks like a number to perl, then it is easy. The numerical value is just the same thing.

If there is any character that stops perl from fully converting the string to number then perl will use as much as it can on the left hand side of the string for the numerical value and disregard the rest.

Let me show a couple of examples:


   Original   As string   As number

     42         "42"        42
     0.3        "0.3"       0.3
    "42"        "42"        42
    "0.3"       "0.3"       0.3

    "4z"        "4z"        4        (*)
    "4z3"       "4z3"       4        (*)
    "0.3y9"     "0.3y9"     0.3      (*)
    "xyz"       "xyz"       0        (*)
    ""          ""          0        (*)
    "23\n"      "23\n"      23

In all the cases where the string to number conversion is not perfect, except the last one, perl will issue a warning. Well, assuming you turned on use warnings as recommended.

Example

Now that you saw that table let' see a use cases:


  use strict;
  use warnings;

  use v5.10;

  my $x = "4x";
  my $y = 3;

Concatenation converts both values to strings:


  say $x . $y;    # 4x3

Numerical addition converts both values to numbers:


  say $x + $y;    # 7
                  # Argument "4x" isn't numeric in addition (+) at ...

Argument isn't numeric

That's the warning you get when perl is trying to convert a string to a number and the conversion isn't perfect.

There are a number of other common warnings and errors in Perl. For example Global symbol requires explicit package name and Use of uninitialized value.


Perl tutorial and video course

For further articles see the Beginner Perl Maven tutorial book and video course.


In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation.
blog comments powered by Disqus
Online courses:


Would you like to get
updated when I publish
the next article?

Follow me:

Google Plus Twitter RSS feed