PERL5 Fifth chapter file read and write

Source: Internet
Author: User
Tags perl interpreter

Fifth chapter file Read and write

by Flamephoenix

One, open, close the file
Second, read the document
Third, write the document
Iv. Judging the status of the file
Five, command-line parameters
Six, open the pipeline

One, open, close the file
The syntax is open (Filevar, filename), where Filevar is the file handle, or the code used to represent a file, filename, filename, the path can be a relative path, can also be an absolute path.
Open (FILE1, "file1");
Open (FILE1, "/u/jqpublic/file1");
The access mode must be determined when the file is opened, and there are three access modes in Perl: Read, Write, and Add. The difference between the latter two modes is that the original file is overwritten by the write mode, the original content is lost, the form is: open (outfile, ">outfile"), and the Add mode continues to add content at the end of the original file, in the form: 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 of the opened file succeeds, returns a value other than 0 when it succeeds, and returns zero if it fails, so you can judge it as follows:
if (Open (MYFILE, "MYFILE")) {
# Here's what-do if the file opened successfully
End program when File open fails:
Unless (open (MYFILE, "File1")) {
Die ("Cannot open input file file1\n");
It can also be represented by a logical OR operator as follows:
Open (MYFILE, "File1") | | Die ("Could not open File");
When the file operation is finished, use Close (MYFILE); Close 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 backwards one line. <STDIN> is a standard input file, usually keyboard input, and does not need to be opened.
Statement @array = <MYFILE>; reads the entire contents of the file into an array @array, each line of the file (with a carriage return) is an element of @array.
Third, write the document
The form is:
Open (OUTFILE, ">outfile");
Print OUTFILE ("Here are 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 the file
1. File test operator
The syntax is:-op expr, such as:
if (-E "/path/file1") {
Print STDERR ("File file1 exists.\n");

File Test Operators

Operator Describe
-B is a block device
-C is a character device
-D is a directory
-E Whether there is
-F is a normal file
-G Whether the Setgid bit is set
-K Whether the sticky bit is set
-L Whether it is a symbolic link
-O Whether you own the file
-P Whether it is a pipeline
-R Whether it is readable
-S is not empty
-T Indicates whether the terminal
-U Whether the setuid bit is set
-W Whether it can be written
-X is executable
-Z is an empty file
-A How long before the last visit
-B is a binary file
-C How long is the inode from the last time you accessed the file
-M How long before the last modification
-O is only for "real users"
-R is only a "real user" readable
-S is the socket
-T is a text file
-W Whether only "real users" can write
-X is only "real user" executable
Note: "Real user" means the userid specified at login, relative to the current process user ID, and the command suid can change the valid user ID.

Unless (open (INFILE, "INFILE")) {
Die ("Input file infile cannot is opened.\n");
if (-E "outfile") {
Die ("Output file outfile already exists.\n");
Unless (open (OUTFILE, ">outfile")) {
Die ("Output file outfile cannot is opened.\n");
Equivalent to
Open (INFILE, "INFILE") &&! (-E "outfile") &&
Open (OUTFILE, ">outfile") | | Die ("Cannot open files\n");
Five, command-line parameters
Like C, Perl also has an array @argv that stores command-line arguments, which can be used to handle individual command-line arguments, unlike C, where $ARGV [0] is the first parameter, not the program name itself.
$var = $ARGV [0]; # The first parameter
$numargs = @ARGV; # Number of parameters
The,<> operator in Perl is actually an implicit reference to an array of @argv, which works as follows:
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.
@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) in the form of a program, just like the command line. such as statement open (Mypipe, "| Cat >hello "); Open a pipe and send the output to mypipe as input to the command "cat >hello". Since 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);

Previous chapter Next Chapter catalogue

Perl5 fifth file read and write

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: 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.