Perl several file manipulation examples _perl

Source: Internet
Author: User
Tags scalar

The most common use of Perl is file processing, and here's a summary of some of the things that Perl files do, and a concrete example that reinforces our understanding of Perl file operations with the example below.

deleting files

Use the Unlinke function, such as unlink $file, unlink $file 1, $file 2, $file 3

Open File

Opening the file in the form of three parameters makes it easy to differentiate between the schema and the file name, which is supported by the version after Perl 5.6.

Copy Code code 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'll 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 would 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 '. Would create #the file if it doesn ' t already exist and'll #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 '. Would create #the file if it doesn ' t already exist and'll #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 once

Use <> read one row at a time in a scalar environment, and in the list environment the next time read all rows, $/store is the row separator, the default is the line break, we will first change the $/, so you can in the scalar environment to read all the line next time (at this time there is no line concept, is to read the entire file), You can also use a list to read all the lines and then spell all the lines together, but that's slow. Remember to change the $/back after use.

Copy Code code as follows:

#!/usr/bin/perl
Use strict;
Use warnings;
Sub test{
Open FILE, ' < ', ' d:/code/test.txt ' or Die $!;
my $olds = $/;
$/= undef;
my $slurp =;
Print $slurp, "n";
$/= $olds;
Close FILE;
}
&test ();

You can also use the local keyword to set $/as a partial variable, so that when you jump out of scope, $/restores the original value.
Copy Code code as follows:

#!/usr/bin/perl
Use strict;
Use warnings;
Sub test{
Local $/; #??? Local $/= undef;
Open FILE, ' < ', ' d:/code/zdd.txt ' or Die $!;
my $slurp =;
Print $slurp, "n";
}
&test ();

The best way is to use the module, which is better than writing security, File::slurp, Io::all.

Open file with double quotes

Open file, if the filename has a variable substitution, it is best to use double quotes instead of single quotes because single quotes ignore variable interpolation.

Copy Code code as follows:

Open FILE "< $file" or Die $!; #这样可以.
Open FILE ' < $file ' or Die $!; #这样就不可以, because $file will not be interpreted as variable interpolation. Similarly < will not be interpreted as input

File handle as parameter

Suppose there is a function test, which has a parameter and is a file handle, how do you pass this argument?

Method one, when passing parameters, in front of the handle plus *

Copy Code code as follows:

Sub Main {
Open FILE, ' +< ', ' test.data ' or Die $!;
&test (*file);
Close FILE;
}

Method Two, open the file using the form open my $FILE
Copy Code code 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.