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 fully analyze the Application of Perl Command Line

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is about how to fully analyze the Perl command line application, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

And you focus on the concept of Perl command line, here from five aspects to introduce to you, Perl has a lot of command line parameters, let's learn about some commonly used parameters.

Introduction to the Perl command line

Perl has many command line arguments. Through them, we have the opportunity to write simpler programs. In this article, we will learn about some commonly used parameters.

* part: parameters of SafetyNetOptions safety net

Mistakes are inevitable when trying some clever (or stupid) ideas with Perl. Experienced Perl programmers often use three parameters to find the error in advance.

1PUR C

This parameter compiles the Perl program but does not actually run it. This checks for all grammatical errors. Every time I modify the perl program, I use it immediately to find any syntax errors.

$perl-cprogram.pl

2PUR W

It will alert you to any potential problems. Perl versions after 5.6.0 have been replaced with usewarnings;-w. You should use usewarnings because it is more flexible than-w.

3Murt

It puts perl in tain mode. In this mode, Perl questions any data sent outside the program. For example, read from the command line, read from an external file, or data from a CGI program.

These data will be dropped by Tainted in-T mode.

The second part: command line Perl parameter: you can let short Perl programs run on the command line.

1Mure

You can have the Perl program run on the command line.

For example, we can run a HelloWorld program on the command line without writing it to a file and running it.

$perl-e'print "HelloWorld\ n"

Multiple-es can also be used at the same time, running in order according to where they appear.

$perl-e'print "Hello";'- e'print "World\ n"'

Like all Perl programs, only the * line of the program does not need to end with;.

2Murm

Modules can be referenced as usual

$perl-MLWP::Simple-e'getstore ("http://www.163.com/","163.html")'## downloads the entire web page

-the M+ module name is the same as the use module name.

Part III: implicit loops

3Murn

Added the ability to loop so that you can process files one by one

$perl-n-e'print;'1.txt#$perl-ne'print;'1.txt

This is the same as the following procedure.

LINE:

While (;) {

Print

}

Open the file on the command line and read it line by line. Each line is saved by default at $_

$perl-n-e'print "$.-$_" 'file

The above line can be written as

LINE:

While (;) {

Print "$.-$_"

}

Output the current number of lines $. And the current line $_.

4Remop, which is the same as-n, but also prints the contents of $_

If you want to do some processing before and after the loop, you can use BEGIN or ENDblock. The following line calculates the number of words in the file.

$perl-ne'END {print$t} @ wicked / (\ w+) / gposit alternate files. Txt

Put all matching words on each line into the array @ w, and then add the number of elements of @ w to the total number of words in the print*** output file in $t.ENDblock.

There are two more parameters that can make the program easier.

5Mura

Turn on automatic detach (split) mode. The space is the default separator. The input is separated according to the separation number and put into the default array @ F

Using-a, the above command can be written as follows:

$perl-ane'END {print$x} $x+=@F'file.txt## uses-a

6Murp F

Change the default separation number to what you want. For example, if the separator is not a character, the above command can be changed to:

$perl-F'\ W'-ane'END {print$x} $x+=@F'file.txt

Here is a complex example through the Unixpassword file. Unixpassword is a text file with a user record on each line.

Separated by colon. Line 7 is the user's login shell path. We can figure out how many users are using each different shell path:

$perl-F':'-ane'$s {$F [6]} +;'\ >;-e'END {print "$_: $s {$_}" forkeys%s}'/ etc/passwd

Although it is not a line now, you can see what problems can be solved by using parameters.

Part IV: RecordSeparators data separator

$/ and $\-input and output separator.

$/ is used to separate data read from the file handle. The default $/ separator is\ n, so that line by line is read from the file handle each time.

$\ is an empty character by default and is automatically added to the end of the data to be print. That's why most of the time print has to add\ n at the end.

$/ and $\ can be used with-nmurp. On the command line, they correspond to-0 (zero) and-l (this is L).

-0 can be followed by a hexadecimal or octal value, which is used to pay $/.

-0777 turns on paragraph mode, and-00 turns on slurp mode (that is, you can read the whole file in at once), which is the same as setting $/ to empty characters and undef.

Using-l alone has two effects:

*: enter the separator number automatically by chomp

Second: pay the $/ value to $\ (so that print will be automatically added at the end\ n)

The parameter\ n\ n is used to add to each output. For example

$perl-le'print "HelloWorld"

Part V: in-situ editing

Using the existing parameters, we can write a very effective command line program. Common UnixI/O redirects:

$perl-pe'somecode' > output.txt

This program reads data from input.txt, then does some processing and then outputs it to output.txt. Of course, you can also redirect the output to the same file.

The above program can be made easier with the-I parameter.

2Muri

Rename the source file and read from the renamed source file. * write the processed data to the source file.

If-I is followed by another string, the string is combined with the source file name to generate a new file name.

This file will be used to store the original file so as not to be overwritten by the-I parameter.

This example replaces all php characters with perl:

$perl-i-pe's/\ bPHP\ b/Perl/g'file.txt

The program reads every line of the file, then replaces characters, and the processed data is rewritten (that is, overwritten) to the source file.

If you do not want to overwrite the source file, you can use

$perl-i.bak-pe's/\ bPHP\ b/Perl/g'file.txt

The data processed here is written to file.txt,file.txt.bak as a backup of the source file.

The above is how to fully analyze the Perl command line application, the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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