1. Open and Close files
Open (Filevar, filename);
Close (Filevar);
2. Read the file
$line = <MYFILE>; #读取一行
@lines = <MYFILE>; #读取一个文件, each of the items in the array is each row of the file
3. Writing files
Open (OUTFILE, ">outfile");
Print OUTFILE ("Here are an output line.\n");
4. Determine file status
The syntax is:-op exp
5. Command-line arguments
$var = $ARGV [0]; # The first parameter
$numargs = @ARGV; # Number of parameters
1) When the Perl interpreter first sees <>, open the file with $argv[0] as the filename;
2) Perform the action Shift (@ARGV); That is to move the elements of the array @argv forward one, the number of its elements is reduced by one.
3) The <> operator reads all the lines in the file opened in the first step.
4) After reading, the interpreter returns to the first step to repeat.
6. Open the pipe
You can also open and use pipelines (Ex:ls > Tempfile) like a command line programmatically, for example,
Statement Open (Mypipe, "| Cat >hello "); Open a pipe and send the output to mypipe as input to the command "cat >hello". Because the cat command will display the contents of the input file, the statement is equivalent to open (Mypipe, ">hello")
File read/write