-1

I've found $\ = $/ when I was investigating how to merge 2 arrays, but I don't understand this at all. An example with it:

use strict;
$\ = $/;

my @array1 = ("string1", "string2");
my @array2 = ("string3", "string4");

my @array = (@array1, @array2);

print for @array;

What do they mean?

xaxes
  • 397
  • 9
  • 21

2 Answers2

10

$\ is the output record separator. Whatever it contains is appended to each print statement. $/ is the input record separator, which has the default value of \n (newline). By setting the output record separator to newline, you don't have to add a newline to your print statements, making the statement:

print for @array;

..look much smoother, compared to

print "$_\n" for @array;

Note that if he had used use 5.010; instead of $\ = $/;, he could have used

say for @array;
ikegami
  • 367,544
  • 15
  • 269
  • 518
TLP
  • 66,756
  • 10
  • 92
  • 149
2

Refer to the Perl documentation, or another good write-up is here.

$/ is the input record separator, $\ is the output record searator. The link above has some helpful mnemonic devices for remembering these and all of the other special Perl variables.

uptownnickbrown
  • 987
  • 7
  • 22