ZetCode

Perl join Function

last modified October 8, 2026 Perl 5.44

The Perl join function combines list elements into a single string. It takes a separator and a list, returning the concatenated result.

join is the opposite of split and is essential for string construction. It's efficient and works with any list-like data.

Basic join Usage

The simplest way to use join is with a separator and array.

basic.pl
#!/usr/bin/perl

use warnings;
use v5.34.0;

my @words = ('apple', 'banana', 'cherry');
my $result = join(', ', @words);

print "Joined string: $result\n";

We join three fruits with a comma and space separator. The function combines array elements into one string.

$ ./basic.pl
Joined string: apple, banana, cherry

Joining with Empty Separator

An empty separator concatenates elements without any characters between.

empty.pl
#!/usr/bin/perl

use warnings;
use v5.34.0;

my @numbers = (1, 2, 3, 4);
my $combined = join('', @numbers);

print "Combined: $combined\n";
print "Length: ", length($combined), "\n";

This joins numbers directly together. The result is a single string of digits.

$ ./empty.pl
Combined: 1234
Length: 4

Joining with Newlines

Using newline as separator creates multi-line strings from lists.

newlines.pl
#!/usr/bin/perl

use warnings;
use v5.34.0;

my @lines = ('First line', 'Second line', 'Third line');
my $text = join("\n", @lines);

print "Result:\n$text\n";

Each array element becomes a separate line. This is useful for file output.

$ ./newlines.pl
Result:
First line
Second line
Third line

Joining Hash Values

join can combine hash values after extracting them.

hash.pl
#!/usr/bin/perl

use warnings;
use v5.34.0;

my %fruit_colors = (
    apple  => 'red',
    banana => 'yellow',
    grape  => 'purple'
);

my $color_list = join(', ', sort values %fruit_colors);
print "Fruit colors: $color_list\n";

We extract hash values with values, sort them, and then join them. Hash keys and values are not stored in any particular order, so sorting makes the output predictable.

$ ./hash.pl
Fruit colors: purple, red, yellow

Complex Separators

Separators can be multi-character strings for complex formatting.

complex.pl
#!/usr/bin/perl

use warnings;
use v5.34.0;

my @items = ('CPU', 'RAM', 'SSD', 'GPU');
my $separator = " | ";
my $specs = join($separator, @items);

print "Computer specs:\n$specs\n";

The pipe separator with spaces creates a readable list. This is common in console output.

$ ./complex.pl
Computer specs:
CPU | RAM | SSD | GPU

Joining with Variables

Variables can be used both as separators and list elements.

variables.pl
#!/usr/bin/perl

use warnings;
use v5.34.0;

my $user = 'admin';
my $domain = 'example.com';
my $separator = '@';

my $email = join($separator, $user, $domain);
print "Email: $email\n";

We construct an email address by joining username and domain. Variables provide flexibility.

$ ./variables.pl
Email: admin@example.com

Joining Split Results

join often pairs with split for string processing.

splitjoin.pl
#!/usr/bin/perl

use warnings;
use v5.34.0;

my $csv = "one,two,three,four";
my @parts = split(',', $csv);
my $new_csv = join(';', @parts);

print "Original: $csv\n";
print "Modified: $new_csv\n";

We split a CSV string then rejoin with different separators. This is common in data conversion.

$ ./splitjoin.pl
Original: one,two,three,four
Modified: one;two;three;four

Best practices

Keep these points in mind when working with the join function.

Source

Perl join Documentation

This tutorial covered Perl's join function with practical examples demonstrating its usage in string construction.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Perl tutorials.