File read/write

Source: Internet
Author: User

1. Open and Close files

Open (Filevar, filename);

Close (Filevar);

2. Read the file

$line = <MYFILE>; #读取一行

@lines = <MYFILE>; #读取一个文件, each of the items in the array is each row of the file

3. Writing files

Open (OUTFILE, ">outfile");
Print OUTFILE ("Here are an output line.\n");

4. Determine file status

The syntax is:-op exp

5. Command-line arguments

$var = $ARGV [0]; # The first parameter
$numargs = @ARGV; # Number of parameters

1) When the Perl interpreter first sees <>, open the file with $argv[0] as the filename;
2) Perform the action Shift (@ARGV); That is to move the elements of the array @argv forward one, the number of its elements is reduced by one.
3) The <> operator reads all the lines in the file opened in the first step.
4) After reading, the interpreter returns to the first step to repeat.

6. Open the pipe

You can also open and use pipelines (Ex:ls > Tempfile) like a command line programmatically, for example,

Statement Open (Mypipe, "| Cat >hello "); Open a pipe and send the output to mypipe as input to the command "cat >hello". Because the cat command will display the contents of the input file, the statement is equivalent to open (Mypipe, ">hello")

File read/write

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.