Perl file Read and Write detail _ Basics Tutorial

Source: Internet
Author: User
Tags perl interpreter readable
First, open, close the file
Syntax is open (Filevar, filename), where Filevar is a file handle, or the program used to represent a file code name, filename is filename, its path can be relative path, can also be absolute path.
Open (FILE1, "file1");
Open (FILE1, "/u/jqpublic/file1");
You must decide the access mode when you open the file, and there are three modes of access in Perl: Read, Write, and Add. The difference between the latter two modes is that the original file is covered by the write mode, the original content is lost, in the form of open (outfile, ">outfile"), while the Add mode continues to add the content at the end of the original file, in the form of open (AppendFile, ">> AppendFile "). Note that you cannot read and write/Add to a file at the same time.
The return value of open is used to determine whether the operation that opened the file succeeds, returns a value other than 0 when it succeeds, and returns zero when it fails, so it can be judged as follows:
if (Open (MYFILE, "MYFILE")) {
# Here's what to do if the file opened successfully
}
End program when File open fails:
Unless (open (MYFILE, "File1")) {
Die ("Cannot open input file file1\n");
}
You can also use logical OR operator notation as follows:
Open (MYFILE, "File1") | | Die ("Could not open file");
When the file operation is finished, use Close (MYFILE); Closes the file.
Second, read the document
Statement $line = <MYFILE>; reads a row of data from a file into a simple variable $line and moves the file pointer back one line. <STDIN> for standard input files, usually keyboard input, do not need to be opened.
The statement @array = <MYFILE>; reads the entire contents of the file into an array @array, and each line of the file (including a carriage return) is an element of @array.
Third, write the document
The form is:
Open (outfile, ">outfile");
Print outfile ("Here's an output line.\n");
Note: STDOUT, stderr are standard output and standard error files, usually screen, and do not need to be opened.
Iv. Judging the status of documents
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 a block device
-C is a character device
-D is a directory
-E Is there
-F is a normal file
-G Whether the Setgid bit is set
-K Whether the sticky bit is set
-L is a symbolic link
-O Whether to own the file
-P Whether it is a pipe
-R is readable
-S is not empty
-T Indicates whether the terminal
-U Whether the setuid bit is set
-W Whether to write
-X Whether to perform
-Z is an empty file
-A How long is the last visit?
-B is a binary file
-C How long is the inode from which the file was last accessed
-M How long does it last modified?
-O is owned only for "true users"
-R is only "true user" readable
-S is a socket
-T is a text file
-W is only "real user" writable
-X is only "real user" executable
Note: "Real user" refers to the UserID specified at logon, which, in contrast to the current process user ID, commands suid can change the valid user ID.

Cases:
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");
V. Command-line Parameters
Like C, Perl also has array @argv that store command-line arguments, which can be used to handle individual command-line arguments; unlike C, $ARGV [0] is the first argument, not the program name itself.
$var = $ARGV [0]; # first parameter
$numargs = @ARGV; # Number of parameters
The,<> operator in Perl is actually an implied reference to an array of @argv, which works as follows:
1. When the Perl interpreter first sees <>, open the file with $argv[0] as filename;
2, carry out action shift (@ARGV); That is, the elements that @argv the array are moved forward one, and the number of elements is reduced by one.
3. The <> operator reads all the rows in the file opened in the first step.
4, after reading, the interpreter returned to the first step repeat.
Cases:
@ARGV = ("Myfile1", "myfile2"); #实际上由命令行参数赋值
while ($line = <>) {
Print ($line);
}
The contents of the file Myfile1 and Myfile2 will be printed out.
Six, open the pipeline
You can also open and use pipelines (Ex:ls > Tempfile) as a command line in the form of a program. such as statement open (Mypipe, "| Cat >hello "); Opens a pipe, and the output sent to mypipe becomes the input of the command "cat >hello". Because the cat command will display the contents of the input file, the statement is equivalent to open (Mypipe, ">hello"); Send the message by pipeline 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.