Perl file operations

Source: Internet
Author: User
Tags perl interpreter

In Perl, you can use the open or sysopen functions to open files and perform operations. Both functions use a file handle (that is, a file pointer) to read and write files.

The open function is used as an example:

1. Open and Close files

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 is what to do if the file opened successfully

}

After the file operation is completed, close the file with myfile.

Read: open (file handle, "<file name") Open (file handle, "file name"). The prerequisite file must already exist. Otherwise, 0 is returned. The error message is displayed at $! Medium

Write: open (file handle, "> file name"). If the file does not exist, create it. If yes, the content is cleared and the length is cut to 0, $! There is error information.

Append: open (file handle, "> file name"), which is basically the same as write. However, the content in the file will not be cleared, and the new content will be appended to the end of the original text.

Read/write: open (file handle, "+ <file name"). In the "+ <" mode, you can read and write files. You can use the tell () function to move inside the file and locate it through the seek () function. If the file does not exist, it will be created. If the file already exists, the original data will not be cleared.

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.

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

Print myfile ("here is an output line./N ");

Syswrite (file, "This is My Write File/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

If (-e "/path/file1") # Whether-e exists

{

Print stderr ("file file1 exists./N ");

}

File test operator

Operator description

-Whether B is a block Device

-C is a character device

-D is a directory

-E exists?

-F is a common file

-Whether the setgid bit is set for G

-K whether Sticky Bit is set

-L indicates whether it is a symbolic link.

-O whether the file exists

-P is a pipe

-R readable?

-S is not empty

-T indicates the terminal

-Whether the setuid bit is set for u

-W is writable?

-X executable?

-Whether Z is a null File

-How long is a from the last access?

-Whether B is a binary file

-C: How long is inode from the last accessed file?

-M: How long is the last modification time?

-O is only owned by "real users "?

-R is only readable by "real users "?

-S is socket

-T indicates whether the file is a text file.

-W: Is only "real users" writable?

-Is X only "real user" executable?

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 );

Source of the original article (Click here)

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.