In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the application of Perl command line parameters, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.
Safety net parameters
There are three parameters that I think can act as a "safety net" because they allow you to avoid making mistakes, especially when you are using Perl to try some particularly smart (or stupid) ideas. Experienced Perl programmers often use these three parameters to find errors in advance.
-C is *. This parameter compiles the Perl program but does not actually run it. This checks for all syntax errors. Every time I modify the perl program, I use it immediately to find any syntax errors.
$perl-cprogram.pl
This ensures that the program can still be compiled. Obviously, it's a lot easier to check immediately after you type a small piece of code than to type in hundreds of lines of code at once and start debug.
-W is the second parameter. It will alert you to any potential bug. Versions after Perl5.6.0 have replaced-w with usewarnings;. You should use usewarnings because it is more flexible than-w.
-T is the third parameter. It takes perl out of taint mode. In this mode, Perl questions any data sent outside the program. For example, read from the Perl command line, read from an external file, or data from a CGI program. These data will be contaminated by Tainted in the-T mode.
Tainted data cannot be used to interact with the outside world. For example, use the file name that is called in system and used as open. For information about what data will be Tainted, see the perlsec documentation, where there is a complete list.
If you want to use Tainted's data, you must untaint this data. Untaint is implemented through regular expressions, and there is enough about taint itself to write a separate article, so I won't talk too much about taint patterns here. If you are writing a program (such as a CGI program) that needs to accept unknowable input from the user, I recommend using taint mode.
Another parameter worth mentioning is-d, which puts Perl in Debugger mode. There is a lot of content on this topic, and I recommend reading the document 'perldocperldebug' or RichardFoley's PerlDebuggerPocketReference.
Command-LinePrograms
The following Perl parameters make it easy for short Perl programs to run on the Perl command line. -e allows Perl code to be executed directly by the compiler on the Perl command line. For example, we can run the "HelloWorld" program directly on the Perl command line without having to write it as a Perl program.
$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;.
Although you can use-e to reference a module,-M makes it easier.
$perl-MLWP::Simple-e'printhead "http://www.example.com"'
The-M module name is the same as the use module name. Some modules have default module imports, and if you don't want to import them, you can use-m. The-m module name, like usemodule (), turns off the default import. For example, the following example, because the head function is imported by default and does not execute when-m is used, the result is no output.
$perl-mLWP::Simple-e'printhead "http://www.example.com"'
-m and-M have a lot of convenient syntax to help you use them, and you can list the various parameters for use after =.
$perl-MCGI=:standard-e'printheader'
Here, CGI.pm 's: standard is introduced, so the header function can be used. Multiple parameters can be introduced by using quotes and commas.
$perl-MCGI='header,start_html'-e'printheader,start_html'
Here we introduce the header and start_html functions.
ImplicitLoops
-n and-p add loops so that you can process files one by one.
$perl-n-e'somecode'file1
This is the same as the following procedure.
LINE:
While () {# yourcodegoeshere}
Note: open the file on the Perl command line and read it line by line. Each line will be saved by default at $_
$perl-n-e'print "$.-$_" 'file
The above line can be written as LINE:while () {print "$.-$_"} to output the current number of lines $. And the current line $_.
2006-12-2522 purl 03 reply
221.221.206.2nd floor
-p makes the above program easier.-p outputs the contents of $_, like this:
LINE:
While () {# yourcodegoeshere} continue {printordie "- PdestinationVariations!\ n";}
Continue is here to ensure that print is called in each loop.
Using-p, our print line count program can be changed to
$perl-p-e'$_= "$.-$_"'
In this case, we do not need to explicitly call the print function, because the-p option has already called it.
Note that the LINE: tag allows us to jump directly to the next input record, no matter how many layers of loop you enter. Use nextLINE.
$perl-n-e'nextLINEunless/pattern/;print$_'
Of course, you can also write: $perl-n-e'printunless/pattern/'
In more complex situations, nextLINE can make your code easier to understand.
If you want to do some processing before and after the loop, you can use BEGIN or ENDblock. The following line of code calculates the number of words in the text file.
$perl-ne'END {print$t} @ wicked / (\ w+) / gposit alternate files. Txt
Put all matching words in each line into the array @ w, and then increment the number of elements of @ w to $t. The total number of words in the print*** output file in ENDblock.
There are two more parameters that can make the program easier. -a turns 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. Therefore, we can rewrite the above program as
$perl-ane'END {print$x} $x+=@F'file.txt
You can also use-F to change the default separation number to what you want. For example, set the separation number to a non-character:
$perl-F'\ W'-ane'END {print$x} $x+=@F'file.txt
Let's introduce a complex example through the Unixpassword file. Unixpassword is a text file, and each line is a user record, separated by a colon. Number one? The line 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.
Data separator
I mentioned $/ and $\-input and output separator in my previous article. $/ is used to separate data read from the file handle, and 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 assigned to $/. -0777 turns on paragraph mode, and-00 turns on slurp mode (that is, you can read the entire file in at once), which is the same as setting $/ to empty characters and undef.
Using-l alone has two effects: * * automatically chomp the separator, and the second $/ value is paid to $\ (so that print will automatically add\ n at the end)
Personally, I often use the-l parameter to add\ n to each output. For example
$perl-le'print "HelloWorld"
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. -I rename the source file and read it 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.
Thank you for reading this article carefully. I hope the article "how to apply Perl command line parameters" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.