[Solved] .= operator in perl


The dot (.) is the concatenation operator in Perl.

$string = $a_substring . $another_substring;

Sometimes you want to concatenate text to the same variable.

$string = $string . $some_extra_text;

Most binary operators in Perl have an “assignment” version which simplifies code like this. So instead of:

$total = $total + $line_value;

You can just write:

$total += $line_value;

Syntax like this can be found in pretty much any C-style programming language.

In Perl, the concatenation operator has an assignment version. So instead of:

$string = $string . $some_extra_text;

You can just write:

$string .= $some_extra_text;

So, reversing that logic, your code:

$contents .= $_;

Is just a shortcut for:

$contents = $contents . $_;

2

solved .= operator in perl