In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail what the commonly used Perl command line parameters are, and the editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Introduction to the Application of Perl Command Line
There are many Perl command line arguments in the Perl language. Through them, we have the opportunity to write simpler programs, and 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 Perl command line, read from external files, or data from CGI programs.
These data will be dropped by Tainted in-T mode.
The second part: Perl command line Perl parameter: you can let short Perl programs run on the Perl command line.
1Mure
You can have the Perl program run on the Perl command line.
For example, we can run the HelloWorld program on the Perl command line without writing it to a file and running it again.
$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 Perl 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 Perl 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
We can write a very effective Perl command line program using the existing parameters. 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.
A classic example of perl
Question:
There is a problem:
Aaa@domain.com2
Aaa@domain.com111
Bbb@home.com2222
Bbb@home.com1
Similar to this kind of output, I want to transform them into the following form:
Aaa@domain.com113
Bbb@home.com2223
Is to add the numbers after the same mailbox name. Can you give me some ideas on how to use perl to achieve it?
Answer: perl-anle'$cnt {$F [0]} + = $F [1]; END {print "$_\ t$cnt {$_}" forkeys%cnt} 'urfile
If you are familiar with the use of the above Perl command line arguments, the above command should be easy to understand:
Each time a line of urfile is read, automatic detach (split) mode is turned on because-an is used. The space is the default separator. The input is separated according to the separation number and put into the default array @ F
Take the example of the * * behavior of the file $F [0] is aaa@domain.com,$F [1] is 2
$cnt {$F [0]} + = $F [1] is a hash array, with $F [0] as key,$F [1] as value, adding up the values of the same key. Then do this to every line of the file.
END {} is processed after the loop is finished. It means to print this% cnt hash array. The key of this hash array is the mailbox name, and value is the superimposed number.
The following is the text form of the command above:
#! / usr/bin/perl usestrict; usewarnings; my%hash; while () {chomp; my@array=split; $hash {$array [0]} + = $array [1];} END {foreach (keys%hash) {print "$_\ t$hash {$_}\ n" }} this is the end of this article on "what are the commonly used Perl command line parameters?". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.