Perl-17-file operation functions

Source: Internet
Author: User
Tags perl interpreter

first. Open a file:

$ Openfileflag = open (filehandle, "pattern identifier FILENAME ");

If the file is successfully opened, the true value (non-null) is returned. If the file fails to be opened, the false value (null value) is returned. The default access mode of the file is read-only;

1. Use the OPEN function for file redirection:

$ Openflag = open (hfcat, "Cat file * | ");

The open function constructs the output of the cat command into a file and associates it with the file handle variable hfcat;

$ Openflag1 = open (fh1, "> FILENAME ");

$ Openflag2 = open (fh2, "> & fh1 ");

The functions of these two statements are to associate both the file handle fh1 and hf2 with the file filename. Both file handles write data to the same file;

The parameter "> & fh1" in the second statement tells the perl interpreter to use a file that has been opened and is associated with the file handle fh1, that is: both the file handles fh1 and fh2 point to the files associated with fh1;

second. close the file:

Close () is used to close a file. If the closed file is a pipeline file, that is, a redirection command, the program will wait until the execution of the redirection command ends;

Close (filehandle );

third. Print, printf, and write functions:

These three functions are all output functions. Print is the simplest function;

1. Print ("Hello, Perl world/N ");

Output to the current default file (selected by the Select () function). If select () is not called, the default output file is stdout;

2. Print OUTFILE ("Hello, Perl world/N ");

Output data to the file pointed to by the specified file handle OUTFILE;

3. printf ($ formatstring, arg1, arg2,..., argn );

Format the data first, and then write the formatted data to the default file (selected by the Select () function). If select () is not called, the default output is

The file is stdout of standard output;

4. printf OUTFILE ($ formatstring, arg1, arg2,..., argn );

Format the data first, and then output the data to the file pointed to by the specified file handle OUTFILE;

5. Write ($ formatstring, arg1, arg2,..., argn );

Format the data first, and then write the formatted data to the default file (selected by the Select () function). If select () is not called, the default output is

The file is stdout of standard output;

6. Write OUTFILE ($ formatstring, arg1, arg2,..., argn );

Format the data first, and then output the data to the file pointed to by the specified file handle OUTFILE;

7. Select () function:

Select (filehandle );

This function sets the file handle specified by the filehandle parameter to the new default file. when calling the print, printf, and write functions

If no file is specified, it is written to the file pointed to by the file handle filehandle by default;

8. EOF () function:

The EOF function determines whether the last object read operation is the last Object Record (check whether the object read/write position pointer has reached the end of the object ),

If yes, a non-zero value (true) is returned. If not, otherwise, a zero value (false) is returned. That is to say, when the last file is read

If the pointer has reached the end of the file, the EOF function returns a non-zero value (true). If the pointer does not reach the end of the file, the zero value (false) is returned );
Generally, the call of an EOF () function with parentheses is the same as that of an EOF function without parentheses.

If you use them together, the effect will be different. When you input multiple files in the command line, the EOF function call without parentheses will read each

When reading the end of the file, the system returns a non-zero value (true); otherwise, the system returns a zero value (false); and The EOF () function with parentheses

The number of calls will be considered to be the end of reading the file only when all the files have been read, that is, only when the file read/write position pointer

Only when the end of the last file is reached will the end of reading the file. At this time, the EOF () function returns a non-zero value (true); otherwise, it returns a zero value (false). Only

All files have been read, and the EOF () function returns the true value. If it is only the end of the previous several files in multiple files, the EOF () function returns

The return value, because there are other input to be read;

fourth. system read/write functions:

1. Read () function:

Perl designs this function to be equivalent to the fread () function in the unux system. It is used to read any length of characters (bytes) from a specified file and read

The data to be stored in a simple variable or array element. The call syntax is as follows:

$ Bytestoread = read (filevariable, buffer, length, bytestpskip );

Filevariable: simple variable used to store file handles;

Buffer: A simple variable (OR array element) used to store read data );

Length: the maximum number of bytes to read;

Bytestoskip: optional, used to specify the number of bytes to be skipped before reading the file;

Returned value: the returned value is the number of bytes actually read. If the read/write position pointer of the object has reached the end of the object, read () returns zero.

If yes, read () returns a null value;

2. sysread () function:

This function is used to read data faster. Its implementation is equivalent to the read () function in Unix systems, which is the same as the read () function in Perl. The call syntax is as follows:

$ Bytestoread = sysread (filevariable, buffer, length, bytestoskip );

Filevariable: simple variable used to store file handles;

Buffer: A simple variable (OR array element) used to store read data );

Length: the maximum number of bytes to read;

Bytestoskip: optional, used to specify the number of bytes to be skipped before reading the file;

Returned value: the returned value is the number of bytes actually read. If the pointer to the file's read/write position has reached the end of the file, sysread () returns zero. If

If an error occurs, sysread () returns a null value;

3. syswrite () function:

This function is used to write data faster. Its implementation is equivalent to the write () function in Unix. It has four parameters:

$ Bytestowrite = syswrite (filevariable, databuffer, length, bytestoskip );

Filevariable: simple variable used to store file handles;

Databuffer: the buffer used to store the data to be written;

Length: the number of bytes to be written;

Bytestoskip: number of bytes to be skipped before writing a file;

Returned value: the number of bytes actually written. If an error occurs, syswrite () returns a null value;

4. GETC () function:

This function is used to read a character (in bytes) from a specified file. Its implementation is equivalent to the GETC () function in unix c language. The call syntax is as follows:

$ Char = GETC (filevariable );

Filevariable: simple variable used to store the file handle;

Returned value: the characters read;

5. binmode () function:

This function is used to read binary files. It is used when the system in which a file is located differs from a binary file,

Called before reading the file; call Syntax:

Binmode (filevariable );

Filevariable: simple variable used to store the file handle;

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.