join#
Concatenate a list of strings with a separator between each adjacent pair, and return the single resulting string.
join takes a separator as its first argument and any number of
values as the rest. It stringifies every value in LIST, glues them
together with EXPR placed between each pair, and returns one scalar.
The separator is inserted between items only — never at the start,
never at the end. With zero or one items the separator never appears
at all.
Synopsis#
join $sep, @list
join "", @chars
join "\n", @lines
What you get back#
A single string. The result is a fresh scalar; modifying it does not
reach back into LIST. In scalar context you get that string; join
is effectively always scalar because it only ever produces one value.
Examples#
Comma-separated values, the canonical use:
my @cols = ("alice", 42, "ops");
my $row = join ",", @cols; # "alice,42,ops"
Build a file path with / as the separator:
my @parts = ("var", "log", "app.log");
my $path = "/" . join("/", @parts); # "/var/log/app.log"
Join lines with a newline between each — note that the trailing line
does not get a newline, because join puts the separator between
items:
my @lines = ("first", "second", "third");
my $text = join "\n", @lines; # "first\nsecond\nthird"
No-separator concatenation. Use the empty string "", not undef —
undef triggers an uninitialized warning under use warnings and
stringifies to "" anyway:
my @chars = ("h", "e", "l", "l", "o");
my $word = join "", @chars; # "hello"
An empty LIST yields an empty string — this is safe to rely on and
needs no guard:
my @empty;
my $s = join ",", @empty; # ""
Edge cases#
Empty
LIST: returns"". The separator is never used.Single-item
LIST: returns that item stringified, with no separator anywhere:join ",", ("only")is"only".Separator is stringified: pass a number, a reference, an object with overloaded
""—joincalls the same stringification every other Perl operator would.join 0, 1, 2, 3is"10203".Multi-character separator:
EXPRcan be any string, not just a single character.join ", ", @colsgives"a, b, c".undefitems inLIST: eachundefstringifies to""and emits anuninitializedwarning underuse warnings. Two adjacentundefs still get a separator between them:join ",", (undef, undef)is",".joinis not the inverse ofspliton a single character when the separator you pass differs from the pattern used to split.splittakes a pattern;jointakes a literal string. Round-trippingjoin "\t", split /\s+/, $sloses the distinction between runs of whitespace and a single tab.Not a pattern: unlike
split, the first argument tojoinis not a regex.join /,/, @xis a syntax surprise — the separator is always a string.
Differences from upstream#
Fully compatible with upstream Perl 5.42.
See also#
split— the inverse: take a string and a pattern, return a list of piecessprintf— when you need per-item formatting, padding, or field widths thatjoincan’t expresspack— when the output is a binary record, not a separator-delimited text stringmap— transform items before joining, e.g.join ",", map { uc } @names