Examples of perl file operations

Source: Internet
Author: User
Tags scalar

Delete an object

Use unlinke functions, such as unlink $ file, unlink $ file1, $ file2, $ file3

Open a file

Open the file with three parameters, which makes it easy to differentiate the mode and file name. This method is supported in Versions later than perl 5.6.

Copy codeThe Code is as follows: # Open the 'txt 'file for reading
Open FH, '<', "file_name.txt" or die "Error: $! \ N ";
# Open the 'txt 'file for writing. Creates the # file_name if it doesn' t already exist # and will delete/overwrite a pre-existing file of the same name
Open FH, '>', "file_name.txt" or die "Error: $! \ N ";
# Open the 'txt 'file for appending. Creates the # file_name if it doesn' t already exist
Open FH, '>', "file_name.txt" or die "Error: $! \ N ";
# Open the 'txt 'file for a' read/write '. # Will not create the file if it doesn't # already exist and will not delete/overwrite # a pre-existing file of the same name
Open FH, '+ <', "file_name.txt" or die "Error: $! \ N ";
# Open the 'txt 'file for a' read/write '. will create # the file if it doesn' t already exist and will # delete/overwrite a pre-existing file # of the same name
Open FH, '+>', "file_name.txt" or die "Error: $! \ N ";
# Open the 'txt 'file for a' read/append '. will create # the file if it doesn't already exist and will # not delete/overwrite a pre-existing file # of the same name
Open FH, '+>', "file_name.txt" or die "Error: $! \ N ";

Read the entire file at one time

Use <> to read a row at a time in the scalar environment, while read all rows at a time in the list environment. $/stores the row separator. The default value is the line break. We will change $/first, in this way, all rows can be read at a time in a scalar environment (there is no concept of rows, that is, reading the entire file ), you can also read all rows in the list and then splice all rows together, but the speed is very slow. Remember to change $/back.

Copy codeThe Code is as follows :#! /Usr/bin/perl
Use strict;
Use warnings;
Sub test {
Open FILE, '<', "d:/code/test.txt" or die $! ;
My $ olds =$ /;
$/= Undef;
My $ slurp = <FILE>;
Print $ slurp, "\ n ";
$/= $ Olds;
Close FILE;
}
& Test ();

You can also use the local keyword to set $/as a local variable. In this way, after jumping out of the scope, $/restores the original value.

Copy codeThe Code is as follows :#! /Usr/bin/perl
Use strict;
Use warnings;
Sub test {
Local $ /;#??? Local $/= undef;
Open FILE, '<', "d:/code/zdd.txt" or die $! ;
My $ slurp = <FILE>;
Print $ slurp, "\ n ";
}
& Test ();
1;

The best way is to use the module, which is safer than writing by yourself. File: Slurp, IO: All are acceptable.

Use double quotation marks to open a file.

When you open a file, if the file name has a variable to replace, it is best to use double quotation marks instead of single quotes, because single quotes ignore variable interpolation.

Copy codeThe Code is as follows: open FILE "<$ file" or die $! .
Open file' <$ file' or die $! ; # This is not acceptable, because $ file will not be interpreted as variable interpolation. Similarly, <is not interpreted as an input symbol.

File handle as a parameter

Suppose there is a function test with a parameter, which is a file handle. How can we pass this parameter?

Method 1: When passing parameters, add *

Copy codeThe Code is as follows: sub main {
Open FILE, '+ <', 'test. data' or die $ !;
& Test (* FILE );
Close FILE;
}

Method 2: open my $ FILE

Copy codeThe Code is as follows: sub main {
Open my $ FILE, '+ <', 'test. data' or die $ !;
& Test ($ FILE );
Close $ FILE;
}
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.