
In the Perl language, there are several operators that consist only of points. How many such operators can you name?
There are five of them. Normally, perl programmers can name two or three.
Let's look at them in order.
One point.
')
Everyone knows the operator from one point. This is concatenation, string concatenation. More precisely, it is the conversion of both operands into strings with their subsequent merging.
my $string = 'one string' . 'another string';
Sometimes it is useful to force one of the operands to return the result in a scalar context.
say "Time: ", localtime;
A small difference between two lines of code, but what a big difference at the output.
With one point finished.
Two points
With two points is more interesting. In fact, two points are two different operators. Depends on how you use them.
Most of the operator is ".." as an operator, issuing an array of values ​​from any interval.
my @numbers = (1 .. 100); my @letters = ('a' .. 'z');
It is also convenient to use it in a loop.
do_something() for 1 .. 30000;
In older versions of perl, a temporary array was created for this, so such cycles could create a large load. Now this problem is not.
So, in the context of lists, two points are the range operator. But in a scalar it is a switch operator (flip-flop). It is called so because it switches between two values, false and true. Starts with false, changes to true on next switch, then false again, etc.
Switching the state of an operator is caused by calculating its operands. Example.
Suppose we process a text file of the form:
START
here is the text
text here
END
unnecessary text
garbage
START
again the necessary text
text here
END
The text we need is between START and END, and the rest is garbage.
School processing method is usually as follows:
my $process_this = 0; while (<$file>) { $process_this = 1 if /START/; $process_this = 0 if /END/; process_this_line($_) if $process_this; }
When using the ".." operator, the same program looks like this:
while (<$file>) { process_this_line($_) if /START/ .. /END/; }
The switch operator returns false until the left operand (/ START /) returns true. The switch then switches to true, and returns it until the right operand (/ END /) returns true. Then it switches to false, and the cycle repeats.
Another trick. Suppose we only need to process from the 20th to the 40th line of the file. The current number of the read line from the last opened file is contained in the special variable $.
If the switch operands are constants, it compares them with the contents of $., And switches when the line number matches. Therefore, processing from 20 to 40 lines of a file is very simple:
while (<$file>) { process_this_line($_) if 20 .. 40; }
Three dots
The operator of the three points is called yada-yada (in Russian, probably, "bla-bla"), and was added to perl 5.12
Strictly speaking, this is probably a language directive, not an operator, since it has no operands.
It is intended to replace unwritten code.
sub reverse_polarity {
This kind of marks programmers have been doing in the code for a long time. And traditionally, the ellipsis implies something not yet written. But now these three points can be really entered into the code, and it will compile and work.
The point is that if you just leave an empty function, it will silently work out and return nothing. And a three-dot in the code will cause perl to issue a special warning “Unimplemented at _file_ line ##”. Such is a reminder.
But in fact, in perl for a long time there is another operator in the form of a three-dot. This is another version of the flip-flop switch operator.
The two-point switch operator first calculates the value of the left operand, if true, switches, and then immediately calculates the value of the right operand. And if it is also true, it immediately switches back. The operator of the three points does not immediately check the value of the right operand, thus ensuring at least one execution.
Of course, it looks like an unnecessary complication, but perhaps in some cases this behavior may be useful. But judging by the fact that very few people know about the flip-flop operator from three points, it is not always needed.
So, here they are, 5 operators: concatenation, range, flip-flop, bla-bla and one more flip-flop.