Essential One-Liners: Walter C. Mankowski
Essential One-Liners: Walter C. Mankowski
Essential One-Liners: Walter C. Mankowski
Essential One-Liners
Walter C. Mankowski
Department of Computer Science Drexel University Philadelphia, PA
Walt Mankowski
Essential One-Liners
Why One-Liners?
Walt Mankowski
Essential One-Liners
Why One-Liners?
Perls got a reputation for producing unreadable, unmaintainable code. A lot of work has been done on tools and techniques to get around that.
Walt Mankowski
Essential One-Liners
Why One-Liners?
Perls got a reputation for producing unreadable, unmaintainable code. A lot of work has been done on tools and techniques to get around that. Perls still great for throwing together quick and dirty little programs.
Walt Mankowski
Essential One-Liners
Why One-Liners?
Perls got a reputation for producing unreadable, unmaintainable code. A lot of work has been done on tools and techniques to get around that. Perls still great for throwing together quick and dirty little programs. Nothings quicker and dirtier than the one-liner.
Walt Mankowski
Essential One-Liners
Text les are everywhere. The Unix command-line environment is incredibly powerful:
everything is ASCII create new programs by connecting small, simple existing programs in pipelines less, wc, sort, xargs, sed, tr, cut, tee, etc.
Walt Mankowski
Essential One-Liners
Walt Mankowski
Essential One-Liners
Walt Mankowski
Essential One-Liners
Walt Mankowski
Essential One-Liners
Hello, world
Walt Mankowski
Essential One-Liners
Hello, world
Walt Mankowski
Essential One-Liners
Hello, world
% perl -e print "Hello, world.\n" Hello, world. % -e to enter the program on the command line
Walt Mankowski
Essential One-Liners
Hello, world
% perl -e print "Hello, world.\n" Hello, world. % -e to enter the program on the command line enclose program in single quotes to avoid shell expansion
Walt Mankowski
Essential One-Liners
Hello, world
% perl -e print "Hello, world.\n" Hello, world. % -e to enter the program on the command line enclose program in single quotes to avoid shell expansion you dont need the nal semicolon
Walt Mankowski
Essential One-Liners
Hello, world
% perl -e print "Hello, world.\n" Hello, world. % -e to enter the program on the command line enclose program in single quotes to avoid shell expansion you dont need the nal semicolon no strict
Walt Mankowski
Essential One-Liners
Hello, world
% perl -e print "Hello, world.\n" Hello, world. % -e to enter the program on the command line enclose program in single quotes to avoid shell expansion you dont need the nal semicolon no strict no warnings
Walt Mankowski
Essential One-Liners
Hello, world
% perl -e print "Hello, world.\n" Hello, world. % -e to enter the program on the command line enclose program in single quotes to avoid shell expansion you dont need the nal semicolon no strict no warnings no tests
Walt Mankowski
Essential One-Liners
Thats it!
Walt Mankowski
Essential One-Liners
Thats it!
Thats all you need to know. The rest of this talk is all about syntactic sugar to make one-liners easier to write.
Walt Mankowski
Essential One-Liners
Walt Mankowski
Essential One-Liners
The -l ag automatically adds a newline to whatever you print. Without -l perl -e print "Hello, world.\n"
Walt Mankowski
Essential One-Liners
The -l ag automatically adds a newline to whatever you print. Without -l perl -e print "Hello, world.\n" With -l perl -le print "Hello, world."
Walt Mankowski
Essential One-Liners
say what?
Perl 5.10 introduced a new builtin function, say, that works just like print except that it automatically adds a newline.
Walt Mankowski
Essential One-Liners
say what?
Perl 5.10 introduced a new builtin function, say, that works just like print except that it automatically adds a newline. Sadly, say doesnt work with -e: % perl -e say "Hello, world." String found where operator expected at -e line 1, near "say "Hello, world."" (Do you need to predeclare say?) syntax error at -e line 1, near "say "Hello, world."" Execution of -e aborted due to compilation errors. %
Walt Mankowski
Essential One-Liners
Walt Mankowski
Essential One-Liners
To avoid breaking backward compatibility, say is turned o by default in 5.10. To turn it on from the command line, use -E instead of -e: % perl -E say "Hello, world." Hello, world. %
Walt Mankowski
Essential One-Liners
Writing Loops
Suppose you want to see if your team is following your new coding standards that lines cant be longer than 80 characters. Heres one way to write that: perl -e while (<>) {print if length > 80} *.pl
Walt Mankowski
Essential One-Liners
The -n ag
That gets tedious to write that all the time, so perl has a -n ag that automatically puts a loop around your code. Its equivalent to while {<>} { ... # your code goes here }
Walt Mankowski
Essential One-Liners
The -n ag
That gets tedious to write that all the time, so perl has a -n ag that automatically puts a loop around your code. Its equivalent to while {<>} { ... # your code goes here } Without -n perl -e while (<>) {print if length > 80} *.pl
Walt Mankowski
Essential One-Liners
The -n ag
That gets tedious to write that all the time, so perl has a -n ag that automatically puts a loop around your code. Its equivalent to while {<>} { ... # your code goes here } Without -n perl -e while (<>) {print if length > 80} *.pl With -n perl -ne print if length > 80 *.pl
Walt Mankowski
Essential One-Liners
If you want to do pre- or post-processing when using the -n ag, use BEGIN and END blocks. For example, if the le nums contains 1 2 3 4
Walt Mankowski
Essential One-Liners
Walt Mankowski
Essential One-Liners
Sum % perl -lne $s += $_; END{print $s} nums 10 % Product % perl -lne BEGIN{$p = 1} $p *= $_; END{print $p} \ nums 24 %
Walt Mankowski
Essential One-Liners
Suppose you want to convert an existing le to lowercase. Now that you know about the -n ag, you might try writing it like this: perl -ne tr/A-Z/a-z/; print foo
Walt Mankowski
Essential One-Liners
Suppose you want to convert an existing le to lowercase. Now that you know about the -n ag, you might try writing it like this: perl -ne tr/A-Z/a-z/; print foo perl -ne tr/A-Z/a-z/; print foo >foo.out
Walt Mankowski
Essential One-Liners
The -p ag
Printing each line is a common enough operation that perl has a special ag for it, the -p ag. Its equivalent to while {<>} { ... # your code goes here } continue { print or die "-p destination: $!\n"; }
Walt Mankowski
Essential One-Liners
The -p ag
Printing each line is a common enough operation that perl has a special ag for it, the -p ag. Its equivalent to while {<>} { ... # your code goes here } continue { print or die "-p destination: $!\n"; } Its similar to the -n ag, except -p prints out each line: Without -p perl -ne tr/A-Z/a-z/; print foo
Walt Mankowski
Essential One-Liners
The -p ag
Printing each line is a common enough operation that perl has a special ag for it, the -p ag. Its equivalent to while {<>} { ... # your code goes here } continue { print or die "-p destination: $!\n"; } Its similar to the -n ag, except -p prints out each line: Without -p perl -ne tr/A-Z/a-z/; print foo With -p perl -pe tr/A-Z/a-z/ foo
Walt Mankowski Essential One-Liners
Walt Mankowski
Essential One-Liners
Walt Mankowski
Essential One-Liners
Walt Mankowski
Essential One-Liners
Walt Mankowski
Essential One-Liners
Walt Mankowski
Essential One-Liners
Obviously the -i ag is dangerous since it clobbers whatever was originally in the le. So Perl lets you specify a backup le when using -i.
Walt Mankowski
Essential One-Liners
Obviously the -i ag is dangerous since it clobbers whatever was originally in the le. So Perl lets you specify a backup le when using -i. Edit a.pl in-place perl -pi -e s/foo/bar/ a.pl
Walt Mankowski
Essential One-Liners
Obviously the -i ag is dangerous since it clobbers whatever was originally in the le. So Perl lets you specify a backup le when using -i. Edit a.pl in-place perl -pi -e s/foo/bar/ a.pl Original le saved in a.pl.bak perl -p -i.bak -e s/foo/bar/ a.pl
Walt Mankowski
Essential One-Liners
Use the -a ag to automatically split each line (like AWK). Default is to split on ; use the -F ag to split on something else.
Walt Mankowski
Essential One-Liners
Use the -a ag to automatically split each line (like AWK). Default is to split on ; use the -F ag to split on something else. Print processes whose parents are init ps axl | perl -ane print if $F[3] == 1
Walt Mankowski
Essential One-Liners
Use the -a ag to automatically split each line (like AWK). Default is to split on ; use the -F ag to split on something else. Print processes whose parents are init ps axl | perl -ane print if $F[3] == 1 Print all userids and user names perl -aln -F: -e print "$F[2]\t$F[0]" /etc/passwd
Walt Mankowski
Essential One-Liners
Using modules
Instead of explicitly useing a module, you can load a module from the command line with the -M ag. The following programs both do the same thing: Use module perl -e use LWP::Simple; getprint "http://pghpw.org"
Walt Mankowski
Essential One-Liners
Using modules
Instead of explicitly useing a module, you can load a module from the command line with the -M ag. The following programs both do the same thing: Use module perl -e use LWP::Simple; getprint "http://pghpw.org" -M ag perl -MLWP::Simple -e getprint "http://pghpw.org"
Walt Mankowski
Essential One-Liners
Perl normally reads input until it hits the input record separator, which defaults to \n and can be changed by setting $/. But in addition to setting $/ in a BEGIN block, you can also change it on the command line with the -0 ag. It sets $/ to an octal or hex number:
Walt Mankowski
Essential One-Liners
Perl normally reads input until it hits the input record separator, which defaults to \n and can be changed by setting $/. But in addition to setting $/ in a BEGIN block, you can also change it on the command line with the -0 ag. It sets $/ to an octal or hex number: -0x0d carriage returns
Walt Mankowski
Essential One-Liners
Perl normally reads input until it hits the input record separator, which defaults to \n and can be changed by setting $/. But in addition to setting $/ in a BEGIN block, you can also change it on the command line with the -0 ag. It sets $/ to an octal or hex number: -0x0d -0 carriage returns null character (find -print0)
Walt Mankowski
Essential One-Liners
Perl normally reads input until it hits the input record separator, which defaults to \n and can be changed by setting $/. But in addition to setting $/ in a BEGIN block, you can also change it on the command line with the -0 ag. It sets $/ to an octal or hex number: -0x0d -0 -00 carriage returns null character (find -print0) paragraph mode (useful for Postx logs)
Walt Mankowski
Essential One-Liners
Perl normally reads input until it hits the input record separator, which defaults to \n and can be changed by setting $/. But in addition to setting $/ in a BEGIN block, you can also change it on the command line with the -0 ag. It sets $/ to an octal or hex number: -0x0d -0 -00 -0777 carriage returns null character (find -print0) paragraph mode (useful for Postx logs) slurp in entire le
Walt Mankowski
Essential One-Liners
Summary of ags
Flag -e -l -n -p -i -a -M -0
Result Execute program on command line Automatically add newlines Automatically loop Automatically loop and print each line Edit les in-place Automatically split input Use module Change input record separator
Walt Mankowski
Essential One-Liners
More Information
perldoc perlrun
many more features than I covered
Walt Mankowski
Essential One-Liners
More Information
perldoc perlrun
many more features than I covered
Walt Mankowski
Essential One-Liners
Walt Mankowski
Essential One-Liners
Examples!
Walt Mankowski
Essential One-Liners
Palindromes
Walt Mankowski
Essential One-Liners
Line Numbers
Print lines preceded by line number perl -ne print "$. $_" $. is a special Perl variable that contains the input line number.
Walt Mankowski
Essential One-Liners
Computing Averages
Sum lines, then divide by total number of lines % perl -lne $s += $_; END{print $s/$.} nums 2.5 %
Walt Mankowski
Essential One-Liners
Print lines 1020 perl -ne print if 10..20 The .. operator is magic when used in scalar context. Read the Range Operators section in perlop.
Walt Mankowski
Essential One-Liners
Walt Mankowski
Essential One-Liners
Walt Mankowski
Essential One-Liners
Walt Mankowski
Essential One-Liners
Random Numbers
Does rand() return the same sequence with the same seed on dierent platforms?
Walt Mankowski
Essential One-Liners
Random Numbers
Does rand() return the same sequence with the same seed on dierent platforms? Look at the rst 5 % perl -le srand(42); print rand for 1..5 0.744525000061007 0.342701478718908 0.111085282444161 0.422338957988309 0.0811111711783106
Walt Mankowski
Essential One-Liners
Random Numbers
Does rand() return the same sequence with the same seed on dierent platforms? Look at the rst 5 % perl -le srand(42); print rand for 1..5 0.744525000061007 0.342701478718908 0.111085282444161 0.422338957988309 0.0811111711783106 Try a whole bunch % perl -le srand(42); print rand for 1..100_000 \ | md5sum ac18d07f40c858bf4b23090177f6a685 Walt Mankowski Essential One-Liners
One-liners can also be used in shell scripts #!/bin/bash SEED=perl -le print int rand 0xffffffff for ((n = 10; n <= 400; n += 10)) do cmd="./wn_path_seq $n $SEED" echo $cmd $cmd done
Walt Mankowski
Essential One-Liners
Add lines to le
Walt Mankowski
Essential One-Liners
Add lines to le
Add line to beginning of le perl -0777 -i -ne print "first\n$_" test.txt Same thing perl -0777 -i -pe $_= "first\n$_" test.txt
Walt Mankowski
Essential One-Liners
Add lines to le
Add line to beginning of le perl -0777 -i -ne print "first\n$_" test.txt Same thing perl -0777 -i -pe $_= "first\n$_" test.txt Same thing, more obfuscated perl -0777 -i -pe s//first\n/ test.txt
Walt Mankowski
Essential One-Liners
Thank you!
Walt Mankowski
Essential One-Liners