Are file descriptors in perl global?

#!/usr/local/bin/perl sub trial { open (LOGFILE, 'C:\Users\out.txt'); trial(); } trial(); 

Please ignore that it will go into an endless loop.

Will the LOGFILE file LOGFILE local or private to the method? If not, how can I make it private / local? I know mine . But I do not know how to use it in File Handles.

+4
source share
2 answers

These file descriptors are global because they are typeglobs . Sometimes this can lead to very unpleasant surprises, because you can accidentally overwrite a file descriptor that was defined inside some module that you are using.

If you need lexical file descriptors, define them with my as follows:

 open my $fh, '<', 'C:\Users\out.txt'; 

See also:

+10
source

Lexical file descriptors are standard my scanners. See Examples in perldoc for open .

 open (my $logfile, 'C:\Users\out.txt'); 

In general, the third form of the open argument is preferred:

 open (my $logfile, '<', 'C:\Users\out.txt'); 
+4
source

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


All Articles