Perl file system

Source: Internet
Author: User
Tags flock local time lstat perl interpreter symlink

I. file input/output functions

1. Basic I/O functions

Open: Allow programs to access files

Close: Terminate File Access

Print: file write string

Write: Write the formatting information to the file.

Printf: format the string and output it to the file.

1) Open Functions

The open function Associates file variables with a file and provides interfaces for accessing the file, for example, open (myvar, "/u/file"). If the file is opened successfully, a non-zero value is returned, otherwise, zero is returned. By default, open files are used to read the content. If you want to open the file to write the content, add a greater than sign before the file name: open (myvar, ">/u/file "); add content to the end of an existing file with two major signs: open (myvar, ">/u/file"); to open the file as a data-oriented command, add the Pipeline character (|): Open (Mail, "| mail Dave") before the command ");

2) Redirect input with open

You can use the opened file handle as a command to input data to the program by adding a pipe character (|) after the command, such:

Open (CAT, "Cat file * | ");

Run the command cat file * on open. This command creates a temporary file. The content of this file is connected to the content of all files with file headers. This file is considered as an input file, cat access to available file variables.

3) file redirection

The Perl implementation redirects both the standard output file (stdout) and the standard error file (stderr) to the same file:

Open (stdout, "> file1") | die ("Open stdout failed ");

Open (stderr, "> & stdout") | die ("Open stderr failed ");

To solve the cache latency of UNIX files, you can tell the perl interpreter not to buffer files by using the following methods:

1. Select a file using the select function

2. assign value 1 to the system variable $ |

System variable $ | specifies whether the file is buffered, regardless of whether it should be buffered. If $ | is a non-zero value, no buffer is used. $ | With system variables $ ~ Works with $ ^. When the select function is not called, $ | affects the current default file.

4) Specify the Read and Write Permissions

To open a readable and writable file, add "+>" before the file name, as shown below:

Open (readwrite, "+> file1 ");

This statement opens the readable and writable file file1, which can be rewritten. File read/write operations are best used with the database functions seek and tell, so that you can jump to any point in the file.

Note: The prefix "+ <" can also be used to specify the read/write permission.

5) Close Function

Used to close open files. When close is used to close the pipeline, that is, the redirection command, the program waits until the redirection command ends, for example:

Open (mypipe, "Cat file * | ");

Close (mypipe );

When the file variable is closed, the program stops running until the command cat file * is completed.

6) print, printf, and write Functions

Print is the simplest of the three functions. It outputs data to the specified file. If it is not specified, the data is output to the current default file.

The printf function first formats the string and then outputs it to the specified file or the current default file.

The Write function outputs information to a file in the output format.

7) Select Function

The select function specifies the file variable passed by the parameter as the new default file.

8) EOF Function

The EOF function checks whether the last object read operation is the last record of the object. If yes, a non-zero value is returned. If the object still contains content, zero is returned.

Generally, EOF is called without brackets, because EOF and EOF () are equivalent, but EOF and EOF () are different when used together with the <> operator.

When multiple files are used as input parameters, only when all files have been read, EOF () returns true. If only the first few ends of multiple files are returned, the returned value is false, because there are other input to be read.

9) Indirect file variables

For the open, close, print, printf, write, select, and EOF functions described above, simple variables can be used to replace the file variables, strings stored in simple variables are considered as file variable names. The following is an example. This example is very simple and will not be explained. It should be noted that the open, close, write, select, and EOF functions also allow expressions to replace file variables. The expression value must be a string and used as the file variable name.

2. Skip and repeat data

Function Name seek

Call the syntax seek (filevar, distance, relative_to );

There are three parameters for moving forward/backward in the file:

1. filevar, file variable

2. Distance: the number of moving bytes. Positive values move forward and negative values move back.

3. reletive_to. The value can be 0, 1, or 2. When the value is 0, the object starts to move from the file header. When the value is 1, it moves relative to the current position (the next row to be read). When the value is 2, it moves relative to the end of the object.

True (non-zero value) is returned for successful running, zero is returned for failure, and is often used with the tell function.

Function name tell

Call the syntax tell (filevar );

Returns the distance from the file header to the current position.

Note:

1. Seek and tell cannot be used to point to file variables in the pipeline.

2. expressions can be used for file variable parameters in seek and tell.

3. system read/write Functions

Function name read

Call the syntax read (filevar, result, length, skipval );

The READ function is designed to be equivalent to the fread function of Unix. It can read characters (bytes) of any length and store them into a simple variable. There are four parameters:

1. filevar: file variable

2. Result: simple variable (OR array element) for storing the result)

3. Length: number of bytes read

4. skipval: Optional. It specifies the number of bytes skipped before reading the file.

The returned value is the number of bytes actually read. If the number of bytes has reached the end of the file, zero is returned. If an error occurs, an empty string is returned.

Function Name: sysread

Call the syntax sysread (filevar, result, length, skipval );

Description: faster data reading, which is equivalent to the Unix function read and has the same parameters as read.

Function Name: syswrite

Call the syntax syswrite (filevar, Data, length, sk1_al );

Describe faster data writing, which is equivalent to the Unix function write. parameter:

1. filevar: file to be written

2. Data: stores the variables to write data.

3. Length: number of bytes to be written

4. Number of bytes skipped before the skinial write operation.

4. Use GETC to read characters

Function Name GETC

Call syntax $ char = GETC (infile );

Description: reads a single character from a file.

5. Use binmode to read binary files

Function Name binmode

Call the syntax binmode (filevar );

Describe this function when your system (such as a doslike System) is different from binary files. It must be used after opening the file and before reading the file.

Ii. Directory processing functions

Function Name mkdir

Call the syntax mkdir (dirname, permissions );

Create a new directory with the following parameters:

1. dirname: name of the directory to be created. It can be a string or expression.

2. Permissions: an octal number that specifies the access permission for a directory. The values and meanings of these permissions are shown in the table below. The combination of permissions is to add the corresponding values.

Value permission

4000 Set User ID during runtime

2000 set the group ID during running

1000 paste bits

0400 read permission of the owner

0200 owner write permission

0100 execution permission of the owner

0040 Group Read Permissions

0020 write permissions

0010 group execution Permissions

0004 everyone's read permission

0002 everyone's write permission

0001 everyone's execution permission

Function Name chdir

Call the syntax chdir (dirname );

Modify the current working directory. The dirname parameter can be a string or expression.

Function Name: opendir

Call the syntax opendir (dirvar, dirname );

Open the Directory and use the following functions to view the file list in a directory. Parameters:

1. dirvar: Directory variable, similar to the file variable

2. dirname: directory name, which can be a string or expression

True values are returned successfully, and false values are returned if the request fails.

Note: Directory variables and file variables with the same name can be used in the program. The component is determined based on the environment.

Function Name closedir

Call the syntax closedir (mydir );

Describe the directory that is opened.

Function Name readdir

Call the syntax readdir (mydir );

When a simple variable is assigned, a file or subdirectory name is assigned each time, and all files and subdirectories are assigned to the array.

Function Name: telldir

Call the syntax location = telldir (mydir );

As if the file is moved before and after, telldir and the following seekdir are used to move before and after the directory list.

Function Name: seekdir

Call the syntax seekdir (mydir, location );

Location must be the value returned by telldir.

Function Name rewinddir

Call the syntax rewinddir (mydir );

The description resets the position of the read directory back to the beginning to repeat the directory list.

Function Name rmdir

Call the syntax rmdir (dirname );

Describe how to delete an empty directory. True (non-zero value) is returned if the call succeeds, and false (zero value) is returned if the call fails ).

Iii. File Attribute Functions

1. File relocation Function

Function Name rename

Call the syntax Rename (oldname, newname );

The description changes the file name or moves it to another directory. The parameter can be a string or expression.

Function Name unlink

Call syntax num = unlink (filelist );

Describe how to delete a file. The parameter is the file name list, and the return value is the number of objects actually deleted.

This function is called unlink instead of Delete because it actually deletes the file link.

2. Link and symbolic link functions

Function Name Link

Call the syntax Link (newlink, file );

Describe the link for creating an existing file-hard link. file is the linked file and newlink is the created link.

Success returns true, failure returns false.

When you delete one of the two links, you can also use the other link to access the file.

Function Name symlink

Call the syntax symlink (newlink, file );

Creates a symbolic link to an existing file, that is, to the file name, rather than to the file itself. The parameters and return values are the same as the preceding values.

When the original file is deleted (for example, the file is deleted by the unlinke function), The created link is unavailable unless you create another file with the same name as the original file.

Function Name readlink

Call syntax filename = readlink (linkname );

If linkname is a symbolic link file, it returns the file to which it actually points. Otherwise, an empty string is returned.

3. File Permission Function

Function Name chmod

Call the syntax chmod (permissions, filelist );

Describe how to change the access permission of an object. Parameters:

1. permissions is the permission to be set. For more information, see the permission table in mkdir.

2. filelist indicates the list of files whose permissions are to be changed.

Function Name chown

Call the syntax chown (userid, groupid, filelist );

The owner of the modified file has three parameters:

1. userid: ID of the new owner (number)

2. groupid: the new group (number) ID number.-1 indicates that the original group is retained.

3. filelist: to change the file list of the owner

Function Name umask

Call the syntax oldmaskval = umask (maskval );

Describe how to set the file access permission mask. The returned value is the current mask.

4. other attribute functions

Function Name truncate

Call the syntax truncate (filename, length );

Reduce the file length to length bytes. If the file length is smaller than the length, do not do anything. The filename can be a file name or a file variable.

Function Name stat

Call the syntax Stat (File );

Describe how to obtain the File status. The parameter file can be a file name or a file variable. The returned list elements are as follows:

Device where the file is located

Internal Reference No. (inode)

Access permission

Hard link count

Owner's (number) ID

Group ID

Device type (if file is a device)

File size (bytes)

Last access time

Last modification time

Best block size for I/O operations

Number of parts allocated to the file

Function Name lstat

Call the syntax lstat (File );

The commentary is similar to stat. The difference is that file is regarded as a symbolic link.

Function Name time

Call the syntax currtime = Time ();

Returns the cumulative number of seconds from January 1, January 1, 1970.

Function Name gmtime

Call the syntax timelist = gmtime (timeval );

The commentary converts the time returned by the test operator in the time, stat or-A and-M files to the Greenwich Mean Time. The returned list elements are as follows:

Seconds

Minutes

Hour, 0 ~ 23

Date

Month, 0 ~ January ~ December)

Year

Week, 0 ~ 6 (Sunday ~ Saturday)

Date of the Year, 0 ~ 364

Indicates whether to renew the instance.

For details, see the Unix gmtime help.

Function Name: localtime

Call the syntax timelist = localtime (timeval );

Similar to gmtime, the difference is that the time value is converted to the local time.

Function Name utime

Call the syntax utime (acctime, modtime, filelist );

Describe how to change the last access time and last modification time of a file. For example:

$ Acctime =-a "file1 ";

$ Modtime =-M "file1 ";

@ Filelist = ("file2", "file3 ");

Utime ($ acctime, $ modtime, @ filelist );

Function Name fileno

Call the syntax filedesc = fileno (filevar );

Describe the internal UNIX file description of the returned file. The filevar parameter is a file variable.

Function Name fcntl

Flock

Call syntax fcntl (filevar, fcntlrtn, value );

Flock (filevar, flockop );

For more information, see the help for Unix functions with the same name.

4. Use dBm files

Perl can use an associated array to access the DBM file. The functions used are dbmopen and dbmclose, which are replaced by tie and untie in perl5.

Function Name dbmopen

Call the syntax dbmopen (array, dbmfilename, permissions );

Describe how to associate an array with a dBm file. Parameters:

1. array: associated array used

2. dbmfilename: name of the DBM file to be opened

3. Access Permissions. For more information, see mkdir.

Function Name dbmclose

Call the syntax dbmclose (array );

Describe how to close the DBM file and remove the relationship between the associated array.

Post from: http://apps.hi.baidu.com/share/detail/16811545
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.