Perl file read/write details

Source: Internet
Author: User
Tags perl interpreter

1. Open and Close files
The syntax is open (filevar, filename), where filevar is the file handle, or is Program Is used to represent the code of a file. filename is the file name, and its path can be relative or absolute.
Open (file1, "file1 ");
Open (file1, "/u/jqpublic/file1 ");
When opening a file, the access mode must be determined. 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 in the form of open (OUTFILE, "> OUTFILE "); in addition, content is added at the end of the original file in the form of open (appendfile, "> appendfile "). Note that you cannot read, write, or add files at the same time.
The return value of open is used to determine whether the file is successfully opened. If the file is successfully opened, a non-zero value is returned, and zero is returned if the file fails. Therefore, the following judgment can be made:
If (open (myfile, "myfile ")){
# Here's what to do if the file opened successfully
}
End the program when the file fails to be opened:
Unless (open (myfile, "file1 ")){
Die ("cannot open input file file1 \ n ");
}
It can also be expressed as follows using logic or operators:
Open (myfile, "file1") | die ("cocould not open file ");
After the file operation is completed, close the file with myfile.
Ii. Reading files
Statement $ line = <myfile>; read a row of data from the file and store it in the simple variable $ line, and move the file pointer back to a row. <Stdin> it is a standard input file, usually a keyboard input, and does not need to be opened.
Statement @ array = <myfile>; read all the content of the file into the array @ array. each row of the file (including the carriage return) is an element of @ array.
Iii. File writing
Format:
Open (OUTFILE, "> OUTFILE ");
Print OUTFILE ("here is an output line. \ n ");
Note: stdout and stderr are standard output and standard error files, which are usually screen files and do not need to be opened.
Iv. Determine the File status
1. File test operators
Syntax:-op expr, for example:
If (-e "/path/file1 "){
Print stderr ("file file1 exists. \ n ");
}

File test operator

Operator Description
-B Block device?
-C Character device or not
-D Is it a directory?
-E Exist?
-F Is it a common file?
-G Whether setgid bit is set
-K Whether Sticky Bit is set
-L Symbolic Link or not
-O Whether the file exists
-P Pipe or not
-R Readable or not
-S Not empty
-T Indicates the terminal
-U Whether the setuid bit is set
-W Writable or not
-X Executable or not
-Z Empty file or not
- How long is the last access time?
-B Whether it is a binary file
-C How long is the inode from the last accessed file?
-M How long is the last modification time?
-O Is it owned only by "real users "?
-R Is it readable only for "real users "?
-S Socket?
-T Whether it is a text file
-W Is it only "real user" writable?
-X Is it executable only for "real users "?
Note: "real user" refers to the userid specified during logon, which is opposite to the user ID of the current process. The SUID command 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
Open (infile, "infile ")&&! (-E "OUTFILE ")&&
Open (OUTFILE, "> OUTFILE") | die ("cannot open files \ n ");
V. Command Line Parameters
Like C, Perl also has an array that stores command line parameters @ argv, which can be used to process command line parameters separately. Unlike C, $ argv [0] is the first parameter, not the program name.
$ Var = $ argv [0]; # The first parameter
$ Numargs = @ argv; # number of parameters
In Perl, the <> operator is actually an implicit reference to the array @ argv. its working principle is:
1. When the perl interpreter sees <> for the first time, open the file named $ argv [0;
2. Execute shift (@ argv); that is, move the element of array @ argv forward, and the number of elements is reduced by one.
3. <> the operator reads all rows in the file opened in step 1.
4. After reading the interpreter, repeat it in the first step.
Example:
@ Argv = ("myfile1", "myfile2"); # The value is actually assigned by the command line parameter.
While ($ line = <> ){
Print ($ line );
}
The contents of myfile1 and myfile2 will be printed out.
6. Open the MPs queue
You can also open and use pipelines (ex: ls> tempfile) in the form of a program like a command line ). For example, open (mypipe, "| cat> hello"); open an MPS queue and send the output to mypipe as the input of the command "cat> hello. Since the cat command will display the content of the input file, this statement is equivalent to open (mypipe, "> hello"). An email is sent in an MTS queue as follows:
Open (message, "| mail Dave ");
Print message ("Hi, Dave! Your Perl program sent this! \ N ");
Close (Message );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.