The syntax is open (filevar, filename), where filevar is a file handle, or the code used in the program to represent a file, filename is the file name, and its path can be a relative path or an absolute path.
open(FILE1,"file1");
open(FILE1, "/u/jqpublic/file1");
When opening a file, you must decide on the access mode. There are three access modes in PERL: read, write and add. The difference between the latter two modes is that the write mode overwrites the original file and the original content is lost, the form is: open(outfile,">outfile"); while the addition mode continues to add content at the end of the original file, the form is: open(appendfile, ">>appendfile"). It should be noted that you cannot read and write/add files at the same time.
The return value of open is used to determine whether the operation of opening the file is successful. It returns a non-zero value when it succeeds and zero when it fails. Therefore, it can be judged as follows:
if (open(MYFILE, "myfile")) {
# here's what to do if the file opened successfully
}
End the program when the file is opened fails:
unless (open (MYFILE, "file1")) {
die ("cannot open input file file1\n");
}
It can also be represented by logic or operators as follows:
open (MYFILE, "file1") || die ("Could not open file");
When the file operation is completed, use close(MYFILE); to close the file.
2. Read the file
Statement $line = <MYFILE>; Read a line of data from the file and store it into the simple variable $line and move the file pointer back by one line. <STDIN> is a standard input file, usually a keyboard input and does not need to be opened.
Statement @array = <MYFILE>; Read all the contents of the file into the array @array, and each line of the file (including the carriage return) is an element of @array.
3. Write a document
The form is:
open(OUTFILE, ">outfile");
print OUTFILE ("Here is an output line.\n");
Note: STDOUT and STDERR are standard output and standard error files, usually screens and do not need to be opened.
4. Determine the file status
1. File test operator
The syntax is: -op expr, such as:
if (-e "/path/file1") {
print STDERR ("File file1 exists.\n");
}
File test operator
Operator | describe |
-b | Is it a block device? |
-c | Is it a character device? |
-d | Is it a directory? |
-e | Does it exist |
-f | Is it a normal file? |
-g | Is the setgid bit set? |
-k | Is the sticky bit set |
-l | Is it a symbolic link? |
-o | Whether to own the file |
-p | Is it a pipeline? |
-r | Is it readable |
-s | Is it not empty |
-t | Does it mean the terminal |
-u | Is the setuid bit set |
-w | Is it possible to write |
-x | Is it possible to execute |
-z | Is it an empty file? |
-A | How long has it been since the last visit |
-B | Is it a binary file? |
-C | How long does it take to last file access inode |
-M | How long has it been last modified |
-O | Is it only owned by "real users" |
-R | Is it only available for "real users" to read |
-S | Is it a socket? |
-T | Is it a text file? |
-W | Is there only "real users" to write |
-X | Is it possible to execute only "real users" |
Note: "Real user" refers to the userid specified when logging in. It is opposite to the current process user ID. The command suid can change the valid user ID. |
Example:
unless (open(INFILE, "infile")) {
die ("Input file infile cannot be opened.\n");
}
if (-e "outfile") {
die ("Output file outfile already exists.\n");
}
unless (open(OUTFILE, ">outfile")) {
die ("Output file outfile cannot be opened.\n");
}
Equivalent to
open(INFILE, "infile") && !(-e "outfile") &&
open(OUTFILE, ">outfile") || die("Cannot open files\n");
5. Command line parameters
Like C, PERL also has an array @ARGV that stores command line parameters, which can be used to process each command line parameter separately; unlike C, $ARGV[0] is the first parameter, not the program name itself.
$var = $ARGV[0]; # First parameter
$numargs = @ARGV; # Number of parameters
In PERL, the <> operator is actually an implicit reference to the array @ARGV, and its working principle is:
1. When the PERL interpreter sees <> for the first time, open a file with $ARGV[0] as the file name;
2. Execute the action shift(@ARGV); that is, move the element of the array @ARGV forward by one, and the number of elements is reduced by one.
3. The <> operator reads all lines in the file opened in the first step.
4. After reading, the interpreter returns to the first step and repeats it.
Example:
@ARGV = ("myfile1", "myfile2"); #In fact, it is assigned by the command line parameter
while ($line = <>) {
print ($line);
}
The contents of files myfile1 and myfile2 will be printed out.
6. Open the pipeline
The pipeline can also be opened and used in the form of a program (ex:ls > tempfile) like a command line. For example, the statement open (MYPIPE, "| cat >hello"); opens a pipeline and the output sent to MYPIPE becomes the input of the command "cat >hello". Since the cat command will display the contents of the input file, this statement is equivalent to open(MYPIPE, ">hello"); the email is sent through the pipeline as follows:
open (MESSAGE, "| mail dave");
print MESSAGE ("Hi, Dave! Your Perl program sent this!\n");
close (MESSAGE);