What is the point value in this open () use in Perl?

How can I understand the following use of the open() function in Perl File I / O?

 open(FHANDLE, ">" . $file ) 

I tried to find this type of syntax in the docs , but did not; note there is. (period) after ">".

All I can’t understand is the use of the point, the rest I know.

+5
source share
2 answers

This is an example of an old form with two open arguments (which should be avoided now that the three open arguments are available). In Perl . - add statement. It combines two lines into one line.

The line of sent code is equivalent to open(FHANDLE, ">$file" ) , it just uses a different join method > and $file .

The best way to do this these days is open(my $fhandle, '>', $file) , as shown in the documentation you linked to.

+12
source

These are two open arguments. The point . is a string concatenation operator in Perl. If open is called with two arguments, the second argument contains both mode and path.

In your case, it will open a file called $file for writing.

However, for several reasons, you should not do this. The most common use is a three-argument descriptor and lexical file descriptors instead of a global GLOB file descriptor.

The lexical file descriptor ensures that Perl implicitly closes handel for you as soon as it goes out of scope. Using different arguments for the mode and file name is a security issue, because otherwise the attacker could drag and drop changes to the file name into mode.

 open my $fh, '>', $file or die $!; 

In addition to the now lexical file descriptor and the separation of mode and file name, we also check for errors in this code, which is always a good idea.

+8
source

Source: https://habr.com/ru/post/1272129/


All Articles