The operator . in Perl

The dot operator is used in Perl for concatenation (connection) lines.

Example:

▶ Run
#!/usr/bin/perl

print 'ABC' . 'DEF';

The program will display the string 'ABCDEF'.

In this example, the dot operator is applied to two lines written in the program code (these lines are called "string literals").

Typically, the operands of the operator point . are string literals, scalar variables, or the results of other functions.

Strings and numbers

The operator . always working with their arguments as strings. The result of all these four operations will be the same line '11'.

  • 1 . 1
  • 1 . '1'
  • '1' . 1
  • '1' . '1'

And that's fine. It can be absolutely no problem to concatenate numbers to strings — and the result is exactly as expected.

Use the array as the operand

Here is an example of a program where an array concatenated with strings using operator .:

▶ Run
#!/usr/bin/perl

my @arr = ('one', 'two', 'three');

print 'A' . @arr . 'B';

The program will display the string 'A3B'.

'A' . @arr . 'B' is the same as that 'A' . scalar(@arr) . 'B'.

When using the array operator point . array is used in scalar context and an array in scalar context returns the number of elements in this array.

Use hash as the operand

Here is a sample program in which the hash concatenated with strings using operator .:

▶ Run
#!/usr/bin/perl

my %h = (
    a => 'one',
    b => 'two',
);

print 'A' . %h . 'B';

Depending on the Perl version, the program will display either a string of the form 'A2/8B', or a string 'A2B'.

'A' . %h . 'B' is the same as that 'A' . scalar(%h) . 'B'.

In the same way as in the array case, when using a hash with the operator point . hash is used in scalar context.

Perl 5.26 hash in scalar context returns the number of pairs of elements in this hash. And in the version of Perl to 5.26 hash in scalar context returns information about the internal structure of the hash.

Use links on Sabu as the operand

Here is an example of a program which link to the Saba concatenated with strings using operator .:

▶ Run
#!/usr/bin/perl

my $sub = sub {
   return 42;
};

print 'A' . $sub . 'B';

Perl to version 5.22 displays something like ACODE(0x1452f00)B, starting from 5.22 on the screen it will be approximately the same place, but there will be more digits after 0x: ACODE(0x561e87413ed8)B.

Different runs of the program will show different numbers.

'A' . $sub . 'B' is the same as 'A' . scalar($sub) . 'B'. When using in scalar context an anonymous Saba returns a string of the form CODE(0x10dbf00) (information about where in memory is the code for that Saba, this knowledge should be very rare).

The underscore _

To concatenate strings in Perl, we use the operator point .. The statement underscores _ in Perl not. But the operator _ is in the popular Perl library for Template::Toolkit and in this library it is used for concatenation.

utf8 flag

In that case, if the concatenation takes part in at least one row which has an internal flag, which means that the content is utf8-encoded, then the resulting row this flag will also be. Here's an example that confirms this behavior:

▶ Run
#!/usr/bin/perl

use utf8;

my $str_with_flag = 'раз-два-три';

my $str_without_flag = 'one-two-three';

print utf8::is_utf8($str_with_flag) . "\n"; # prints 1
print utf8::is_utf8($str_without_flag) . "\n"; # prints empty string

print utf8::is_utf8($str_with_flag . $str_without_flag) . "\n"; # prints 1
print utf8::is_utf8($str_without_flag . $str_with_flag) . "\n"; # prints 1

Related topics

Other articles

Comments