Perl join Function

Perl join tutorial shows how to combine list elements into a string in Perl using join function.

Perl join Function

Perl join Function

last modified April 4, 2025

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 strict; 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 strict; 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 strict; 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 strict; use warnings; use v5.34.0;

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

my $color_list = join(’, ‘, values %fruit_colors); print “Fruit colors: $color_list\n”;

We extract hash values with values then join them. The order of values is unpredictable.

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

Complex Separators

Separators can be multi-character strings for complex formatting.

complex.pl

#!/usr/bin/perl

use strict; 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 strict; 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 strict; 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

  • Choose clear separators: Make joined strings readable.

  • Pre-allocate for performance: Use join instead of concatenation in loops.

  • Combine with map/grep: Process elements before joining.

  • Watch for undef: Undefined elements become empty strings.

Source

Perl join Documentation

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

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Perl tutorials.

ad ad