Essential One-Liners: Walter C. Mankowski

Download as pdf or txt
Download as pdf or txt
You are on page 1of 77

One-Liners 101 Advanced Perl One-Liners Summary Examples

Essential One-Liners
Walter C. Mankowski
Department of Computer Science Drexel University Philadelphia, PA

YAPC::Chicago June 16, 2008

Walt Mankowski

Essential One-Liners

One-Liners 101 Advanced Perl One-Liners Summary Examples

Why One-Liners?

Perls got a reputation for producing unreadable, unmaintainable code.

Walt Mankowski

Essential One-Liners

One-Liners 101 Advanced Perl One-Liners Summary Examples

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

But Im a Java programmer!

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.

Perl ts really well into this niche.

Walt Mankowski

Essential One-Liners

One-Liners 101 Advanced Perl One-Liners Summary Examples

Freeing your time for more important things . . .

Walt Mankowski

Essential One-Liners

One-Liners 101 Advanced Perl One-Liners Summary Examples

Hello, world Newlines Loops

Perl One-Liners 101

Walt Mankowski

Essential One-Liners

One-Liners 101 Advanced Perl One-Liners Summary Examples

Hello, world Newlines Loops

Perl One-Liners 101


(Cell phones o?)

Walt Mankowski

Essential One-Liners

One-Liners 101 Advanced Perl One-Liners Summary Examples

Hello, world Newlines Loops

Hello, world

% perl -e print "Hello, world.\n"

Walt Mankowski

Essential One-Liners

One-Liners 101 Advanced Perl One-Liners Summary Examples

Hello, world Newlines Loops

Hello, world

% perl -e print "Hello, world.\n" Hello, world. %

Walt Mankowski

Essential One-Liners

One-Liners 101 Advanced Perl One-Liners Summary Examples

Hello, world Newlines Loops

Hello, world

% perl -e print "Hello, world.\n" Hello, world. % -e to enter the program on the command line

Walt Mankowski

Essential One-Liners

One-Liners 101 Advanced Perl One-Liners Summary Examples

Hello, world Newlines Loops

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Hello, world Newlines Loops

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Hello, world Newlines Loops

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Hello, world Newlines Loops

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Hello, world Newlines Loops

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Hello, world Newlines Loops

Thats it!

Thats all you need to know.

Walt Mankowski

Essential One-Liners

One-Liners 101 Advanced Perl One-Liners Summary Examples

Hello, world Newlines Loops

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Hello, world Newlines Loops

Perl programmers love syntactic sugar.

Walt Mankowski

Essential One-Liners

One-Liners 101 Advanced Perl One-Liners Summary Examples

Hello, world Newlines Loops

Automatic newlines with -l

The -l ag automatically adds a newline to whatever you print. Without -l perl -e print "Hello, world.\n"

Walt Mankowski

Essential One-Liners

One-Liners 101 Advanced Perl One-Liners Summary Examples

Hello, world Newlines Loops

Automatic newlines with -l

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Hello, world Newlines Loops

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Hello, world Newlines Loops

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Hello, world Newlines Loops

say what? (continued)

To avoid breaking backward compatibility, say is turned o by default in 5.10.

Walt Mankowski

Essential One-Liners

One-Liners 101 Advanced Perl One-Liners Summary Examples

Hello, world Newlines Loops

say what? (continued)

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Hello, world Newlines Loops

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Hello, world Newlines Loops

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Hello, world Newlines Loops

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Hello, world Newlines Loops

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Hello, world Newlines Loops

BEGIN and END blocks

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Hello, world Newlines Loops

BEGIN and END blocks (continued)

Sum % perl -lne $s += $_; END{print $s} nums 10 %

Walt Mankowski

Essential One-Liners

One-Liners 101 Advanced Perl One-Liners Summary Examples

Hello, world Newlines Loops

BEGIN and END blocks (continued)

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Hello, world Newlines Loops

Writing Loops (continued)

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Hello, world Newlines Loops

Writing Loops (continued)

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Hello, world Newlines Loops

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Hello, world Newlines Loops

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Hello, world Newlines Loops

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Editing les in-place Autosplit Using modules Input record separators

Advanced Perl One-Liners

Walt Mankowski

Essential One-Liners

One-Liners 101 Advanced Perl One-Liners Summary Examples

Editing les in-place Autosplit Using modules Input record separators

Editing les in-place


Instead of just printing out the le, one thing you might want to do with the -p ag is make the changes directly to the le. Perls -i ag does exactly that:

Walt Mankowski

Essential One-Liners

One-Liners 101 Advanced Perl One-Liners Summary Examples

Editing les in-place Autosplit Using modules Input record separators

Editing les in-place


Instead of just printing out the le, one thing you might want to do with the -p ag is make the changes directly to the le. Perls -i ag does exactly that: Print to stdout perl -pe s/foo/bar/ a.pl

Walt Mankowski

Essential One-Liners

One-Liners 101 Advanced Perl One-Liners Summary Examples

Editing les in-place Autosplit Using modules Input record separators

Editing les in-place


Instead of just printing out the le, one thing you might want to do with the -p ag is make the changes directly to the le. Perls -i ag does exactly that: Print to stdout perl -pe s/foo/bar/ a.pl Edit a.pl in-place perl -pi -e s/foo/bar/ a.pl

Walt Mankowski

Essential One-Liners

One-Liners 101 Advanced Perl One-Liners Summary Examples

Editing les in-place Autosplit Using modules Input record separators

Editing les in-place


Instead of just printing out the le, one thing you might want to do with the -p ag is make the changes directly to the le. Perls -i ag does exactly that: Print to stdout perl -pe s/foo/bar/ a.pl Edit a.pl in-place perl -pi -e s/foo/bar/ a.pl Editing multiple les perl -pi -e s/foo/bar/ *.pl

Walt Mankowski

Essential One-Liners

One-Liners 101 Advanced Perl One-Liners Summary Examples

Editing les in-place Autosplit Using modules Input record separators

Editing les in-place

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Editing les in-place Autosplit Using modules Input record separators

Editing les in-place

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Editing les in-place Autosplit Using modules Input record separators

Editing les in-place

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Editing les in-place Autosplit Using modules Input record separators

Automatically splitting les

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Editing les in-place Autosplit Using modules Input record separators

Automatically splitting les

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Editing les in-place Autosplit Using modules Input record separators

Automatically splitting les

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Editing les in-place Autosplit Using modules Input record separators

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Editing les in-place Autosplit Using modules Input record separators

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Editing les in-place Autosplit Using modules Input record separators

Input record separators

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Editing les in-place Autosplit Using modules Input record separators

Input record separators

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Editing les in-place Autosplit Using modules Input record separators

Input record separators

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Editing les in-place Autosplit Using modules Input record separators

Input record separators

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Editing les in-place Autosplit Using modules Input record separators

Input record separators

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

More Information

perldoc perlrun
many more features than I covered

Walt Mankowski

Essential One-Liners

One-Liners 101 Advanced Perl One-Liners Summary Examples

More Information

perldoc perlrun
many more features than I covered

Google for perl one liners


Tom Christiansen one-liners article by Je Bay in The Perl Review One-liners 101 on IBM developerWorks many others

Walt Mankowski

Essential One-Liners

One-Liners 101 Advanced Perl One-Liners Summary Examples

Java doesnt have one-liners

Walt Mankowski

Essential One-Liners

One-Liners 101 Advanced Perl One-Liners Summary Examples

Examples!

Walt Mankowski

Essential One-Liners

One-Liners 101 Advanced Perl One-Liners Summary Examples

Palindromes

Find palindromes perl -lne print if $_ eq reverse \ /usr/share/dict/words

Walt Mankowski

Essential One-Liners

One-Liners 101 Advanced Perl One-Liners Summary Examples

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Printing selected lines

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Grep with Perl regular expressions


Poor mans grep perl -ne print if /^foobar/

Walt Mankowski

Essential One-Liners

One-Liners 101 Advanced Perl One-Liners Summary Examples

Grep with Perl regular expressions


Poor mans grep perl -ne print if /^foobar/ . . . but with the full power of Perls regular expressions! Lines with foo not followed by bar perl -ne print if /foo(?!bar)/

Walt Mankowski

Essential One-Liners

One-Liners 101 Advanced Perl One-Liners Summary Examples

Grep with Perl regular expressions


Poor mans grep perl -ne print if /^foobar/ . . . but with the full power of Perls regular expressions! Lines with foo not followed by bar perl -ne print if /foo(?!bar)/ Lines with bar not preceded by foo perl -ne print if /(?<!foo)bar/

Walt Mankowski

Essential One-Liners

One-Liners 101 Advanced Perl One-Liners Summary Examples

Grep with Perl regular expressions


Poor mans grep perl -ne print if /^foobar/ . . . but with the full power of Perls regular expressions! Lines with foo not followed by bar perl -ne print if /foo(?!bar)/ Lines with bar not preceded by foo perl -ne print if /(?<!foo)bar/ Grep on paragraphs perl -00 -ne print if /foo(?!bar)/
Walt Mankowski Essential One-Liners

One-Liners 101 Advanced Perl One-Liners Summary Examples

Random Numbers
Does rand() return the same sequence with the same seed on dierent platforms?

Walt Mankowski

Essential One-Liners

One-Liners 101 Advanced Perl One-Liners Summary Examples

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

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 101 Advanced Perl One-Liners Summary Examples

Stacking the deck

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Add lines to le

Add line to beginning of le perl -0777 -i -ne print "first\n$_" test.txt

Walt Mankowski

Essential One-Liners

One-Liners 101 Advanced Perl One-Liners Summary Examples

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

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

One-Liners 101 Advanced Perl One-Liners Summary Examples

Thank you!

Walt Mankowski

Essential One-Liners

You might also like