I’m running
perl -ple '$_=length' datafile
The datafile contains the following:
algorithm
student government
Fiumichino
The result is that it prints
9
18
10
What do the -p and -l options do? Also, what is $_?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
$_is the default input and pattern-searching variable.-pis a command line switch that puts an implicitwhile(<>)loop around your program, with a print statement at the end. The-lswitch sets$/and$\to “\n” (newline) andchomps your input, or in layman’s terms, it handles newlines for you.The diamond operator
<>is “magic” in that it automatically chooses your input channel. If your script has arguments, it will interpret it as a file name, and open that file and read it. If not, it checks STDIN. In your case, it opens the file “datafile”.What that oneliner does is read each line of datafile and sets
$_to the length of$_(sincelengthuses$_if no argument is supplied), then prints that number.You can deparse the one-liner and see what the code looks like: