With regards to points

Original author: Dave Cross
  • Transfer
image
There are several operators in the perl language that consist of only dots. How many such operators can you name?

There are five of them. Typically, 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, concatenation of strings. More precisely, this is the conversion of both operands to strings with their subsequent combination.

my $string = 'one string' . 'another string';
# $string содержит 'one stringanother string'


Sometimes it is useful to force one of the operands to return the result in a scalar context.

say "Time: ", localtime; # возвращает массив значений
say "Time: " . localtime; # возвращает текущую дату и время в виде строки


A little difference between the two lines of code, but what a big difference in the output.

With one point finished.

Two points



With two points is already more interesting. In fact, two points are two different operators. Depends on how you use them.

Most are familiar with the ".." operator as an operator that returns an array of values ​​from any interval.

my @numbers = (1 .. 100);
my @letters = ('a' .. 'z');


It is also convenient to use in a loop.

do_something() for 1 .. 30000;


In older versions of perl, a temporary array was created for this, so such loops could create a big load. Now there is no such problem.

So, in the context of lists, two points are the range operator. But in the scalar - this is a switch statement (flip-flop). It is called so because it switches between two values, false and true. It starts with false, on the next switch it changes to true, then again to false, etc.

Switching the state of an operator is called by calculating its operands. Example.
Suppose we are processing a text file of the form:

START
text here text
text here
END
unnecessary text
garbage
START
again desired text
text here
END

The text we need is between START and END, and the rest is garbage.

The school method of processing 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 statement returns false until the left operand (/ START /) returns true. Then the switch switches to true, and returns it until the right operand (/ END /) returns true. Then it switches to false, and the loop repeats.

Another trick. Suppose we need to process only the 20th to the 40th lines of the file. The current line number read 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 20 to 40 lines of a file is very simple:

while (<$file>) {
  process_this_line($_) if 20 .. 40;
}


Three dots



The three-point operator is called yada-yada (probably blah blah in Russian), and was added in perl 5.12.
Strictly speaking, this is probably a directive of the language, not an operator, since it has no operands.
It is intended to replace code that has not yet been written.

sub reverse_polarity {
  # TODO: Тут будет обалденный код
  ...
}


Programmers have been making such notes in code for a long time. And traditionally, ellipsis means something not yet written. But now these three points can actually be 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 an ellipsis in the code will cause perl to issue a special warning “Unimplemented at _file_ line ##”. Such a reminder.

But in fact, in perl since ancient times there is another operator in the form of an ellipsis. This is another version of the flip-flop switch statement.

A two-point switch operator first calculates the value of the left operand, if true, it switches, and then immediately calculates the value of the right. And if it is also true, it immediately switches back. The operator of three points does not immediately check the value of the right operand, thus guaranteeing at least one execution.

Of course, this seems like an unnecessary complication, but perhaps in some cases this behavior may come in handy. But judging by the fact that very few people know about the three-point flip-flop operator, it is little needed.

So, here they are, 5 operators: concatenation, range, flip-flop, blah blah and another flip-flop.

Also popular now: