Delete files
Use unlink functions, such as unlink $file, unlink $file1, $file2, $file3
Open the file
Open the file in three parameters, which is very convenient for distinguishing between mode and file name. Versions after perl 5.6 support this method.
#Open the 'txt' file for reading
open FH, '<', "$file_name.txt" or die "Error:$!\n";
#Open the 'txt' file for writing. Creates the #file_name if it doesn't already exist #and will delete/overwrite a pre-existing file of the same name
open FH, '>', "$file_name.txt" or die "Error:$!\n";
#Open the 'txt' file for appending. Creates the #file_name if it doesn't already exist
open FH, '>>', "$file_name.txt" or die "Error:$!\n";
#Open the 'txt' file for a 'read/write'. #Will not create the file if it doesn't #already exist and will not delete/overwrite #a pre-existing file of the same name
open FH, '+<', "$file_name.txt" or die "Error:$!\n";
#Open the 'txt' file for a 'read/write'. Will create #the file if it doesn't already exist and will #delete/overwrite a pre-existing file #of the same name
open FH, '+>', "$file_name.txt" or die "Error:$!\n";
#Open the 'txt' file for a 'read/append'. Will create #the file if it doesn't already exist and will #not delete/overwrite a pre-existing file #of the same name
open FH, '+>>', "$file_name.txt" or die "Error:$!\n";
Read the entire file at once
Use <> to read one line at a time in the scalar environment, and read all lines at a time in the list environment. $/ stores the line separator, and the default is a newline character. We first change $/ so that we can read all lines at a time in the scalar environment (at this time there is no concept of lines, which is to read the entire file). You can also read all lines in the list and then splice all lines together, but that is very slow. Use up Remember to change $/ back.
#!/usr/bin/perl
use strict ;
use warnings ;
sub test{
open FILE, '<', "d:/code/" or die $! ;
my $olds = $/ ;
$/ = undef ;
my $slurp = <FILE> ;
print $slurp, "\n" ;
$/ = $olds ;
close FILE;
}
&test() ;
You can also use the local keyword to set $/ as a local variable, so that after you jump out of scope, $/ returns to its original value.
#!/usr/bin/perl
use strict ;
use warnings ;
sub test{
local $/ ; #??? local $/ = undef ;
open FILE, '<', "d:/code/" or die $! ;
my $slurp = <FILE> ;
print $slurp, "\n" ;
}
&test() ;
1;
The best way is to use modules, which is safer than writing by yourself. File::Slurp and IO::All are both OK.
Please use double quotes to open the file
When opening a file, if the file name has variable replacement, it is best to use double quotes instead of single quotes, because single quotes ignore variable interpolation.
open FILE "<$file" or die $! ; # This is OK.
open FILE '<$file' or die $! ; # This is not possible, because $file will not be interpreted as variable interpolation. Also < will not be interpreted as input symbols.
File handle as parameter
Suppose there is a function test, which has a parameter, which is a file handle, so how should this parameter be passed?
Method 1: When passing parameters, add * in front of the handle
sub main {
open FILE, '+<', '' or die $!;
&test(*FILE);
close FILE;
}
Method 2: Open my $FILE
sub main {
open my $FILE, '+<', '' or die $!;
&test($FILE);
close $FILE;
}