Perl file operations (1) (suitable for beginners)

Source: Internet
Author: User
Tags chop

Use sysopen () for more control
--------------------------------------------------------------------------------
To better control the file opening method, you can use the sysopen () function:

Use fcntl;

Sysopen (FH, $ filename, o_rdwr | o_creat, 0666)

Or die "can't open $ filename for reading/writing/creating: $! ";

The sysopen () function has four parameters. The first parameter is a file handle parameter similar to the open () function, the second parameter is the file name without the mode information, and the third parameter is the mode parameter, A constant composed of logical or operations provided by the fcntl module. The fourth parameter (optional) is the octal value (0666 indicates the data file, and 0777 indicates the program ). If the file can be opened, sysopen () returns true. If the file fails to be opened, false is returned.

Different from the open () function, sysopen () does not provide a short description of the pattern. Instead, it combines some constants, and each pattern constant has a unique meaning, only logical or operations can combine them. You can set a combination of multiple actions.

O_RDONLYRead-only

O_wronly write-only

O_rdwr reading and writing

O_append writes go to the end of the file

O_trunc truncate the file if it existed

O_creat create the file if it didn't exist

O_exclerror if the file already existed (used with o_creat)

When you need to be careful, use the sysopen () function. For example, if you want to add content to a file, if the file does not exist and you do not create a new file, you can write it like this:

Sysopen (log, "/var/log/myprog. log", o_append, 0666)

Or die "can't open/var/log/myprog. log for appending: $! ";

Read a single record
There is an easy way to read filehandles: Using operators. In the scalar content, it returns the next record in the file or the undefined error message. We can use it to read a row into a variable:

$ Line =;

Die "unexpected end-of-file" Unless defined $ line;

In the loop statement, we can write as follows:

While (defined ($ record =) {# long-winded

# $ Record is set to each record in the file, one at a time

}

This is because it requires a lot of such work, which is usually simplified,

Put the record in $ _ instead of $ record:

While (){

# $ _ Each time it is a record in the file

}

In Perl 5.004 _ 04, we can do this:

While ($ record = ){

# $ Record each time is a record in the file

}

Defined () is automatically added. In versions earlier than Perl 5.004 _ 04, this command provides a warning. To understand the Perl version, run the following command:

Perl-V

Once we read a record, we usually intend to remove the record separator (default value: Line Break ):

Chomp ($ record );

In Perl 4.0, only the chop () operation is performed. The last character of the string is removed, no matter what the character is. Chomp () is not so destructive. If a line separator exists, it only removes the line separator. If you want to remove the line separator, use chomp () instead of chop ().

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.