What is the join() method in Perl?

Overview

The join() method in Perl is used to join strings that are separated by the specified separator.

Syntax

join(separator, *strings)

Parameters

  • separator: A string or a character that separates the joined strings.

  • *strings: One or more strings that we want to join.

Return value

This method returns a new string with the strings joined together and separated by the separator separator.

Example

Let’s look at the code below:

# join strings
$str1 = join(":", "hello", "we");
$str2 = join("-", "Edpresso", "is", "awesome");
$str3 = join("**", "the", "stars");
# print results
print "$str1\n";
print "$str2\n";
print "$str3";

Explanation

  • Lines 2 to 4: We use the join() method to join some strings and the results are saved in some variables.
  • Lines 7 to 9: We print the results to the console.

Free Resources