9.1. The ',' operator

In perl the comma (,) is an operator, which we encountered before in function calls. The comma concatenates two arrays. We can use it to initialise an array in one call:

@lines = ("One fish", "Two fish", "Red fish", "Blue fish");

for $idx (0 .. $#lines)
{
    print $lines[$idx], "\n";
}

We can also use it to concatenate two existing arrays:

@primes1 = (2,3,5);
@primes2 = (7,11,13);
@primes = (@primes1,@primes2,17);
@primes = (@primes,19);

for $idx (0 .. $#primes)
{
    print $primes[$idx], "\n";
}

So why it is used in function calls? In perl every function accepts an array of arguments and returns an array of return values. That's why the comma is useful for calling functions which accept more than one argument.


Written by Shlomi Fish