Main Content

The difference between dot (a.k.a. full stop, period) and comma in Perl

Archive - Originally posted on "The Horse's Mouth" - 2011-12-09 03:37:02 - Graham Ellis

If (Perl) I write
  $x = "12";
  $y = "25";
  print $x,$y;
  print $x.$y;
  print "\n";

Then I'll get output
  12251225

In other words - the output is the same. So is there a difference?

Yes - there's a huge difference.

$x.$y - using the dot operator - joins the two strings together (concatenates them) into one.

$x,$y - using the comma - passes the two strings forward as a list so they are printed one after another.

You can see the difference if you write
  @abc = sort($y,$x);
  @def = sort($y.$x);


In the first case, using , (comma), @abc is a list that's two sorted elements long, so it's
  ['12','25']
whereas in the second case, using . (full stop / period), @def is a list of one item, nothing to sort, so it's
  ['2512']

Full example, including output, [here] from this week's Perl Programming Training Course.