yapc::na::2009 - command line perl

11
1 Command-line Perl June 22, 2009 3:25:46 PM file://localhost/YAPC_Talk/index.html Command-line Perl or: Perl one-liners saved my career or: `perldoc perlrun` brought to life or: Data Munging on PowerThirst (YAPC::NA::2009) Bruce Gray (Util) Any sufficiently advanced technology... ... will fail when used for even more advanced tasks. If you are viewing a printed version of this presentation, or seeing it on SlideShare, it probably looks awful. Instead, please get a packaged copy of the HTML version by emailing me ([email protected]), or downloading from: http://feather.perl6.nl/~util/ It should look great in Firefox, or even better in Opera's "Presentation Mode" (fullscreen). Larry == Comedic One-liners You want it in one line? Does it have to fit in 80 columns? :-) --Larry Wall in <[email protected]> Perl code on the command line Look Ma, no script! $ perl print Can't open perl script "print": No such file or directory -e (The `e` is for hysterical raisins) $ perl -e print (No output) Quoting on the command line

Upload: bruce-gray

Post on 18-Nov-2014

4.707 views

Category:

Technology


2 download

DESCRIPTION

Candidate talk for YAPC::NA::2009 on using Perl from the command-line.

TRANSCRIPT

Page 1: Yapc::NA::2009 - Command Line Perl

1Command-line Perl

June 22, 2009 3:25:46 PMfile://localhost/YAPC_Talk/index.html

Command-line Perlor: Perl one-liners saved my career

or: `perldoc perlrun` brought to life

or: Data Munging on PowerThirst

(YAPC::NA::2009)

Bruce Gray (Util)

Any sufficiently advanced technology...... will fail when used for even more advanced tasks.If you are viewing a printed version of this presentation, or seeing it on SlideShare, it probably looks awful.

Instead, please get a packaged copy of the HTML version by emailing me ([email protected]), or downloading from:

http://feather.perl6.nl/~util/

It should look great in Firefox, or even better in Opera's "Presentation Mode" (fullscreen).

Larry == Comedic One-liners

You want it in one line? Does it have to fit in 80 columns? :-)

--Larry Wall in <[email protected]>

Perl code on the command lineLook Ma, no script!$ perl print

Can't open perl script "print": No such file or directory

-e (The `e` is for hysterical raisins)$ perl -e print

(No output)

Quoting on the command line

Page 2: Yapc::NA::2009 - Command Line Perl

2Command-line Perl

June 22, 2009 3:25:46 PMfile://localhost/YAPC_Talk/index.html

Quoting on the command line...and you can quote me on that

$ perl -e ' print "Hello\n" ' # Unix

$ perl -e " print 'Hello' . $/ " # Win32

$ perl -e " print qq'Hello\n' " # Win32; sick

$ perl -e " print qq{Hello\n} " # Win32; better

Automatic end-of-line handlingComputing the meaning of 2 lives. `bc` not found

$ perl -e ' print 42 * 42 '1764$ perl -e ' print 42 * 42 * 42 '74088$

Automatic end-of-line handlingThe -l flag adds "\n" to `print`$ perl -le ' print 42 * 42 '

1764

$ perl -le ' print 42 * 42 * 42 '

74088

Page 3: Yapc::NA::2009 - Command Line Perl

3Command-line Perl

June 22, 2009 3:25:46 PMfile://localhost/YAPC_Talk/index.html

Unconventional conventionistsThe code formerly known as:

$ perl -e ' print "Hello\n"; '

with output:Hello

will now appear as:$ perl -e '...' any arguments here

print "Hello\n";

Hello

Defaults@ARGV is the array of command-line arguments.

shift takes @ARGV as its default.$ perl -le '...' foo bar baz

print @ARGV; $z=shift; print $z;

foobarbazfoo

Defaultsprint takes $_ as default, and for sets $_ as default.$ perl -le '...' foo bar baz

print for @ARGV

foobarbaz

Page 4: Yapc::NA::2009 - Command Line Perl

4Command-line Perl

June 22, 2009 3:25:46 PMfile://localhost/YAPC_Talk/index.html

WarningsDouble your money!$ perl -le '...' 15

$zz = shift; print $z . $z

(No output)

WarningsAlways pay for the cheap insurance$ perl -wle '...' 15

$zz = shift; print $z . $z

Name "main::zz" used only once: possible typo...Use of uninitialized value in concatenation...Use of uninitialized value in concatenation...

Corrected$ perl -wle '...' 15

$z = shift; print $z . $z

1515

Command-line Modules-M means `use`$ perl -MData::Dumper -wle 1

(No output)

$ perl -MData::Dumper::Dear::John-wle 1

Can't locate Data/Dumper/Dear/John.pm in @INC

Page 5: Yapc::NA::2009 - Command Line Perl

5Command-line Perl

June 22, 2009 3:25:46 PMfile://localhost/YAPC_Talk/index.html

Command-line Modules$FOO::VERSION is a CPAN standard$ perl -MData::Dumper -wle '...'

print $Data::Dumper::VERSION;

2.121_02

Cross WordsCapital of Portugal, 6 letters, ? i ? b o ?$ perl -wlne '...' /usr/share/dict/words

print if /^[A-Z]i.bo.$/

Lisbon

Hi, Cog!

PHPScrambled word: ulsacepoer$ perl -wlne '...' /usr/share/dict/words | wc

print if /^[ulsacpeeor]{10}$/

104

PHPScrambled word: ulsacepoer$ perl -wlne '...' /usr/share/dict/words

print if join("", sort split "") eq "aceeloprsu"

perlaceous

Per*la"ceous\, a. [See Pearl.] Pearly; resembling pearl.

Page 6: Yapc::NA::2009 - Command Line Perl

6Command-line Perl

June 22, 2009 3:25:46 PMfile://localhost/YAPC_Talk/index.html

Dizzy CamelsScrambling word: perlaceous$ perl -MList::Util=shuffle -wle '...' perlaceous

@z = split "", shift; print join "", shuffle @z;

ulsacepoer

Prized Doors$ perl -MList::Util=shuffle -wle '...' 27

@z = 1 .. shift; print for (shuffle @z)[0..9];

perl -MList::Util=shuffle -wle '@z = 1 .. shift ; print for (shuffle @z)[0..9];'

Coke?Don't run this just for this talk! Download svn.log.gz !$ svn -q log https://svn.parrot.org/parrot/ > svn.log

-------------------------------------------------------------------r39696 | petdance | 2009-06-21 23:12:08 -0500 (Sun, 21 Jun 2009)-------------------------------------------------------------------r39695 | japhb | 2009-06-21 23:10:39 -0500 (Sun, 21 Jun 2009)-------------------------------------------------------------------r39694 | cotto | 2009-06-21 21:49:35 -0500 (Sun, 21 Jun 2009)-------------------------------------------------------------------r39693 | japhb | 2009-06-21 21:23:28 -0500 (Sun, 21 Jun 2009)-------------------------------------------------------------------r39692 | moritz | 2009-06-21 17:08:28 -0500 (Sun, 21 Jun 2009)-------------------------------------------------------------------r39691 | japhb | 2009-06-21 13:03:24 -0500 (Sun, 21 Jun 2009)-------------------------------------------------------------------r39690 | coke | 2009-06-21 12:50:53 -0500 (Sun, 21 Jun 2009)-------------------------------------------------------------------r39689 | japhb | 2009-06-21 12:36:39 -0500 (Sun, 21 Jun 2009)-------------------------------------------------------------------r39688 | cotto | 2009-06-21 10:42:11 -0500 (Sun, 21 Jun 2009)-------------------------------------------------------------------r39687 | bacek | 2009-06-21 06:24:16 -0500 (Sun, 21 Jun 2009)-------------------------------------------------------------------r39686 | whiteknight | 2009-06-21 05:44:46 -0500 (Sun, 21 Jun 2009)

Page 7: Yapc::NA::2009 - Command Line Perl

7Command-line Perl

June 22, 2009 3:25:46 PMfile://localhost/YAPC_Talk/index.html

10 GOTO 10-p reads a line, runs your code, prints a line.$ perl -wlpe '...' svn.log

$_ = uc;

----------------------------------------------------------------R39696 | PETDANCE | 2009-06-21 23:12:08 -0500 (SUN, 21 JUN 2009)----------------------------------------------------------------R39695 | JAPHB | 2009-06-21 23:10:39 -0500 (SUN, 21 JUN 2009)----------------------------------------------------------------R39694 | COTTO | 2009-06-21 21:49:35 -0500 (SUN, 21 JUN 2009)----------------------------------------------------------------R39693 | JAPHB | 2009-06-21 21:23:28 -0500 (SUN, 21 JUN 2009)----------------------------------------------------------------R39692 | MORITZ | 2009-06-21 17:08:28 -0500 (SUN, 21 JUN 2009)

10 GOTO 10-n reads a line, runs your code.$ perl -wlne '...' svn.log

next if /---/;$_ = uc; print;

R39696 | PETDANCE | 2009-06-21 23:12:08 -0500 (SUN, 21 JUN 2009)R39695 | JAPHB | 2009-06-21 23:10:39 -0500 (SUN, 21 JUN 2009)R39694 | COTTO | 2009-06-21 21:49:35 -0500 (SUN, 21 JUN 2009)R39693 | JAPHB | 2009-06-21 21:23:28 -0500 (SUN, 21 JUN 2009)R39692 | MORITZ | 2009-06-21 17:08:28 -0500 (SUN, 21 JUN 2009)R39691 | JAPHB | 2009-06-21 13:03:24 -0500 (SUN, 21 JUN 2009)R39690 | COKE | 2009-06-21 12:50:53 -0500 (SUN, 21 JUN 2009)R39689 | JAPHB | 2009-06-21 12:36:39 -0500 (SUN, 21 JUN 2009)R39688 | COTTO | 2009-06-21 10:42:11 -0500 (SUN, 21 JUN 2009)R39687 | BACEK | 2009-06-21 06:24:16 -0500 (SUN, 21 JUN 2009)

Page 8: Yapc::NA::2009 - Command Line Perl

8Command-line Perl

June 22, 2009 3:25:46 PMfile://localhost/YAPC_Talk/index.html

HITBULLS$ perl -MO=Deparse -ne ' next if /---/; $_ = uc; print; '

LINE: while (defined($_ = <ARGV>)) {next if /---/;$_ = uc $_;print $_;

}

$ perl -MO=Deparse -pe ' $_ = uc; '

LINE: while (defined($_ = <ARGV>)) {$_ = uc $_;

}continue {

die "-p destination: $!\n" unless print $_;}

Home on the Range$ perl -ne '...' svn.log

print if /^r39688/ .. /^r39686/;

r39688 | cotto | 2009-06-21 10:42:11 -0500 (Sun, 21 Jun 2009)------------------------------------------------------------------------r39687 | bacek | 2009-06-21 06:24:16 -0500 (Sun, 21 Jun 2009)------------------------------------------------------------------------r39686 | whiteknight | 2009-06-21 05:44:46 -0500 (Sun, 21 Jun 2009)

print if ($. == 2) .. ($. == 4);

print if 2 .. 4;

r39696 | petdance | 2009-06-21 23:12:08 -0500 (Sun, 21 Jun 2009)------------------------------------------------------------------------r39695 | japhb | 2009-06-21 23:10:39 -0500 (Sun, 21 Jun 2009)

Page 9: Yapc::NA::2009 - Command Line Perl

9Command-line Perl

June 22, 2009 3:25:46 PMfile://localhost/YAPC_Talk/index.html

Laughing Buddha, Frowning PerlMonkXKCD w/o EDVO$ ls XKCD_mirror/xkcd*.html | tail -2

XKCD_mirror/xkcd0539.htmlXKCD_mirror/xkcd0540.html

$ perl -wlne '...' XKCD_mirror/xkcd*.html

push @L, $_ if not $h{$_}++;

END{ printf "%7d\t%s\n", $h{$_}, $_ for @L }

Laughing Buddha, Frowning PerlMonk...

539 <div id="contentContainer">539 <div id="middleContent" class="dialog">

1 <h1>Barrel - Part 1</h1><br/>1617 <br />1078 <div class="menuCont">1078 <ul>1078 <li><a href="/1/">|&lt;</a></li>

2 <li><a href="#" accesskey="p">&lt; Prev</a></li>539 <li><a href="http://dynamic.xkcd.com/comic/random/" id="rnd_btn_t">Random</a></li>

2 <li><a href="/2/" accesskey="n">Next &gt;</a></li>1078 <li><a href="/">&gt;|</a></li>1078 </ul>2156 </div>1617 <br/>

1 <img src="http://imgs.xkcd.com/comics/barrel_cropped_(1).jpg" title="Don't we all." alt="Barrel - Part 1" /><br/>

Page 10: Yapc::NA::2009 - Command Line Perl

10Command-line Perl

June 22, 2009 3:25:46 PMfile://localhost/YAPC_Talk/index.html

Awk$ perl -wlane '...' svn.log

print $F[0];

-------------------------------------r39696-------------------------------------r39695-------------------------------------r39694

Old McDonald had a farm...$ perl -wlnae '...' svn.log

{FAIL}

Respect My Authoritay$ cut -s -d ' ' -f 3 svn.log | tr "[:upper:]" "[:lower:]" | sort | uniq -c | sort -nr

$ perl -wlane '...' svn.log

$h{ lc $F[2] }++ if @F>1;

END{ printf "%7d\t%s\n", $h{$_}, $_ for sort { $h{$b} <=> $h{$a} } keys %h }

4215 leo3008 coke2850 bernhard2669 jkeenan2408 pmichaud1975 chromatic1814 allison

You have to believe we are magic...$ perl -MY -wlane '...' svn.log

$h{ lc $F[2] }++ if @F>1; END{ dhn }

Page 11: Yapc::NA::2009 - Command Line Perl

11Command-line Perl

June 22, 2009 3:25:46 PMfile://localhost/YAPC_Talk/index.html

And the hits keep coming-i -i.bak -0 -00 -0777 -E -l[octnum] -m -C PERLIO

$ perldoc perlrun

$ perldoc perlvar

http://perldoc.perl.org/perlrun.html

http://perldoc.perl.org/perlvar.html

Golf