Several ways to use file manipulation handles in Perl-Basic edition

Source: Internet
Author: User

In Perl there are several ways to use the handle, where open is the most basic use of the method, the following I encountered the use of several handles to make a small summary:

Use of the Open method:

The Open method is the most basic opening file handle method in the tutorial, which is more common

There are three ways to use it in the tutorial

Open filehandle,expr

Open Filehandle,mode,list

Open FileHandle

I'm only here to introduce a simple way to use, others can expand their own learning.

Read and write the contents of the Open file:

Use strict;

#准备进行读取的文件

My $my _file = "D:/file/file.txt";

#FILEHANDLE as a famous handle, capital,< can be omitted

Open (FileHandle, "< $my _file") or die "Can ' t open $my _file";

#打印句柄读取内容

Print <FILEHANDLE>;

#需要写入信息的文件, can exist or not exist

My $print _file = "D:/file/file2.txt";

#一个 > Delete the original file write, two > indicates append write

Open (FILEHANDLE2, "> $print _file") or die "Can ' t open $print _file";

#将读取的数据写入到需要写入的文件中, of course, the information to be written here is not necessarily a handle, it can be anything, and the handle to the file is required to exist.

Print <FILEHANDLE2> <FILEHANDLE>;

close filehandle; #关闭读取文件句柄

close FILEHANDLE2;#关闭写入文件的句柄

Use of the Io::file module:

The Io::file module needs to create a new file handle: The Open method opens the file, and it works in a similar way to the Open function:

Use strict;

Use Io::file; #导入IO模块

#创建新的文件句柄, the file handle name here needs to be named according to your own requirements, not uppercase

My $filehandle = new Io::file;

#用句柄打开文件file .txt,< indicates that the file is read

$filehandle->open ("< file.txt") or die "Can ' t open file.txt";

#打印读取的文件内容

Print < $filehandle >;

#创建一个新的文件句柄

My $filehandle 1 = new Io::file;

#该文件句柄打开需要写入的文件

$filehandle 1->open ("> file1.txt") or DIW "Can ' t open file1.txt";

#将hello Word information is printed into the file handle

$filehandle 1->print ("Hello word");

$filehandle->close; #将读取文件的句柄关闭

$filehandle->close; #将写入文件的句柄关闭

Use of the FileHandle module:

The filehandle method should be derived from the methods in the IO module.

Use strict;

Use filehandle; #导入FileHandle模块

#创建读取文件的句柄

My $filehandle = new FileHandle ("< file.txt") or die "Can ' t open file.txt";

#打印该文件内的内容

Print < $filehandle >;

#创建写入文件的文件句柄

My $filehandle 1 = new FileHandle ("> file1.txt") or die "Can ' t open file1.txt";

#将hello Word information is written to the handle

Print < $filehandle 1> "Hello word";

Close $filehandle; #关闭读取文件的句柄

close $filehandle 1; #关闭写入文件的句柄

The above three methods in the process of development have tried, the most commonly used for the last method, using the FileHandle module method, relatively concise, compared to the second method, open and create a step to solve. But for the performance of these three methods and other aspects of the advantages and disadvantages of the temporary I can not be answered, but the three kinds of methods to create a handle summed up for your reference and use, in this summary if there is an incorrect place, also please give me a message to amend, if you know more about this aspect, You can also put forward more valuable comments, just to stimulate.

In the following chapters, I will discuss the file handle in more depth, and will be more in-depth research and analysis of the last two modules.

Several ways to use file manipulation handles in Perl-Basic edition

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.