Perl Language Primer--3--file read and write

Source: Internet
Author: User

The existing file Test.txt, the content is: "123\n456"

1, Open Text test.txt

#!/usr/bin/perl

Open D, "test.txt";

D is the file handle, pointing to the open file

2, read text line by row Test.txt

#!/usr/bin/perl

Open D, "< test.txt";

while (<d>) {

Print $_;

}

Close D;

Results: 123

456

Or: Print <d>;

Results: Ibid.

<> is the fetch operator.

3, write content to Test111.txt

#!/usr/bin/perl

Open D, ">test111.txt";

Print F "Hello,world\nsee you\n";

Close D;

If Test111.txt original content, then the original content will be erased, the content of Test111.txt is

Hello,world

See you

4, append content to Test111.txt

#!/usr/bin/perl

Open D, ">>test111.txt";

Print d "Thank you\n";

Close D;

The content of Test111.txt is

Hello,world

See you

Thank

Print is equivalent to writing a command, do not lose the end of the statement ";"

Perl Language Primer--3--file read and write

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.