Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to read and write Files with Perl

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how Perl can read and write files. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Open and close Perl files

The syntax is open (filevar,filename), where filevar is the handle to the Perl file, or the code used to represent a Perl file in the program, and filename is the name of the Perl file, and its path can be either relative or absolute.

Open (FILE1, "file1")

Open (FILE1, "/ u/jqpublic/file1")

The access mode must be determined when opening the Perl file, and there are three access modes in PERL: read, write, and add. The difference between the latter two modes is that the write mode overwrites the original Perl file and the original content is lost in the form of open (outfile, "> outfile"), while the add mode continues to add content at the end of the original Perl file in the form of open (appendfile, "> > appendfile"). Note that you cannot read and write / add Perl files at the same time.

The return value of open is used to determine whether the operation of opening the Perl file is successful. It returns a non-zero value when it succeeds and zero if it fails, so you can judge as follows:

If (open (MYFILE, "myfile")) {

# here'swhattodoifthefileopenedsuccessfully

}

End the program when the Perl file fails to open:

Unless (open (MYFILE, "file1")) {

Die ("cannotopeninputfilefile1\ n")

}

It can also be represented by logic or operators as follows:

Open (MYFILE, "file1") | | die ("Couldnotopenfile")

When the Perl file is finished, use close (MYFILE); to close the Perl file.

Second, read the Perl file

The statement $line=; reads a row of data from the Perl file and stores it in the simple variable $line and moves the Perl file pointer back one line. Standard input Perl file, usually keyboard input, does not need to be opened.

The statement @ array=; reads the entire contents of the Perl file into each line of the array @ array,Perl file (including the carriage return) as an element of @ array.

Third, write Perl files

The form is:

Open (OUTFILE, "> outfile")

PrintOUTFILE ("Hereisanoutputline.\ n")

Note: STDOUT and STDERR are standard output and standard error Perl files, usually on screen, and do not need to be opened.

4. Judge the status of Perl files

1. Perl file test operator

The syntax is:-opexpr, such as:

If (- e "/ path/file1") {

PrintSTDERR ("Filefile1exists.\ n")

}

Command line parameters

Like C, PERL has an array @ ARGV that stores command-line arguments, which can be used to process individual command-line arguments; unlike C, $ARGV [0] is * * arguments, not the program name itself.

$var=$ARGV [0]; # * parameters

Number of $numargs=@ARGV;# parameters

In PERL, the operator is actually an implicit reference to the array @ ARGV, which works as follows:

1. When the PERL interpreter sees it for the first time, open the Perl file with the name of $ARGV [0] as the Perl file.

2. Execute the action shift (@ ARGV); that is, move the elements of the array @ ARGV forward one, and the number of elements decreases by one.

3. The operator reads all the lines in the Perl file opened in the * * step.

4. After reading it, the interpreter returns to the * * step and repeats it.

Example:

@ ARGV= ("myfile1", "myfile2"); # is actually assigned by the command line argument

While ($line=) {

Print ($line)

}

The contents of the Perl files myfile1 and myfile2 will be printed.

6. Open the pipe

You can also open and use pipes like the command line (ex:ls > tempfile) in the form of a program. For example, the statement open (MYPIPE, "| cat > hello"); opens a pipe, and the output sent to MYPIPE becomes the input to the command "cat > hello". Because the cat command displays the contents of the input Perl file, this statement is equivalent to open (MYPIPE, "> hello"); send the message through the pipe as follows:

Open (MESSAGE, "| maildave")

PrintMESSAGE ("Hippin Davegoat YourPerl programmatic sentthis!\ n")

Close (MESSAGE)

Thank you for reading! This is the end of the article on "how to read and write documents in Perl". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report