You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

483 lines
9.0 KiB

<!DOCTYPE html>
<html lang=en>
<head>
<meta charset="utf-8">
<title>perl</title>
<link rel=stylesheet href="slides.css" />
<link rel="alternate" type="application/atom+xml" title="changes" href="//p1k3.com/userland-book/feed.xml" />
<link rel="stylesheet" href="js/styles/sunburst.css">
<script src="js/highlight.pack.js"></script>
<script src="js/jquery.js" type="text/javascript"></script>
<script>hljs.initHighlightingOnLoad();</script>
</head>
<body>
<section>
<p style="font-size: 3em; margin-bottom: 0; text-align: center;">
All the Perl you need
</p>
<p style="font-size: 2em; text-align: center;">
for slicing and dicing text files
</p>
<p style="text-align: center;">
Brennen Bearnes &lt;bbearnes@wikimedia.org&gt;
</p>
<p style="text-align: center;"><small>
Source:
<a href="https://code.p1k3.com/gitea/brennen/wmf-engprod-offsite-slides">
code.p1k3.com/gitea/brennen/wmf-engprod-offsite-slides
</a><br />
Slides:
<a href="https://squiggle.city/~brennen/perl/">
squiggle.city/~brennen/perl/
</a>
</small></p>
</section>
<section>
<h2><a name=Agenda href=#Agenda></a> Agenda</h2>
<ol>
<li> A capsule history</li>
<li> What is Perl actually like?</li>
<li> Basic syntax and techniques</li>
<li> Examples</li>
</ol>
</section>
<section>
<h2><a name=A-capsule-history href=#A-capsule-history></a> A capsule history</h2>
<ul>
<li>1987 - Started by Larry Wall</li>
<li>1991 - <em>Programming Perl</em> - the Camel Book</li>
<li>1994-95 - Perl 5, CPAN</li>
<li>late 1990s - Perl CGI unerpins much of the web</li>
<li>2000ish - Massive derail into Perl 6 development</li>
</ul>
</section>
<section>
<h2><a name=What-rsquo-s-Perl-actually-like href=#What-rsquo-s-Perl-actually-like></a> What&rsquo;s Perl actually like?</h2>
<ul>
<li>Backronym: &ldquo;Practical Extraction and Report Language&rdquo;.</li>
<li>&ldquo;There&rsquo;s more than one way to do it&rdquo;</li>
<li>&ldquo;Executable line-noise&rdquo;</li>
<li><a href="https://lobste.rs/s/odi5eu/93_paint_splatters_are_valid_perl">&ldquo;93% of Paint Splatters are Valid Perl Programs&rdquo;</a></li>
</ul>
</section>
<section>
<h2><a name=What-rsquo-s-Perl-actually-like href=#What-rsquo-s-Perl-actually-like></a> What&rsquo;s Perl actually like?</h2>
<p>A general-purpose, multi-paradigm programming language.</p>
<p>That also has a lot in common with the Unix shell.</p>
</section>
<section>
<h2><a name=What-rsquo-s-Perl-actually-like href=#What-rsquo-s-Perl-actually-like></a> What&rsquo;s Perl actually like?</h2>
<p>General-purpose, multi-paradigm programming language.</p>
<p>That also has a lot in common with the Unix shell.</p>
<p>So: <strong>A widely-installed environment where you can combine small tools to work
with data.</strong></p>
</section>
<section>
<p>A sample data file, tab-separated names of some authors:</p>
<!-- exec -->
<pre><code>$ column -t examples/authors.tsv
Robinson Eden
Waring Gwendolyn L.
Brunner John
Tolkien John Ronald Reuel
Walton Jo
Toews Miriam
Cadigan Pat
Le Guin Ursula K.
Veselka Vanessa
Wells Martha
Leckie Ann
</code></pre>
<!-- end -->
</section>
<section>
<p>Perl is often described as a superset of <code>grep</code>, <code>sed</code>, and <code>awk</code>.</p>
<p>Combines filtering, sorting, and transforming strings with stuff that&rsquo;s hard
(or missing) in Bash:</p>
<ul>
<li>Functions</li>
<li>Clean basic data structures</li>
<li>Robust quoting</li>
<li>Sensible control structures</li>
<li>Variable scope</li>
</ul>
</section>
<section>
<p>Like <code>grep</code>:</p>
<!-- exec -->
<pre><code>$ perl -ne 'print if m/^T/;' examples/authors.tsv | column -t
Tolkien John Ronald Reuel
Toews Miriam
</code></pre>
<!-- end -->
</section>
<section>
<p>Like <code>cut</code> or <code>awk</code>:</p>
<!-- exec -->
<pre><code>$ perl -anE 'say @F[1];' examples/authors.tsv
Eden
Gwendolyn
John
John
Jo
Miriam
Pat
Guin
Vanessa
Martha
Ann
</code></pre>
<!-- end -->
</section>
<section>
<p>Like <code>sed</code> or <code>tr</code>:</p>
<!-- exec -->
<pre><code>$ perl -pe 'tr/[a-z]/[A-Z]/' examples/authors.tsv | column -t
ROBINSON EDEN
WARING GWENDOLYN L.
BRUNNER JOHN
TOLKIEN JOHN RONALD REUEL
WALTON JO
TOEWS MIRIAM
CADIGAN PAT
LE GUIN URSULA K.
VESELKA VANESSA
WELLS MARTHA
LECKIE ANN
</code></pre>
<!-- end -->
</section>
<section>
<p>I&rsquo;m not here to convince you to write large programs in Perl.</p>
<p>I want to gesture at a portion of the language useful for:</p>
<ul>
<li>One-liners</li>
<li>Short utility scripts</li>
<li>Exploratory data work &amp; transforms</li>
</ul>
<p>Let&rsquo;s go over some basic syntax and techniques, and then look at a few
examples from my toolkit.</p>
</section>
<section>
<h2><a name=Basics href=#Basics></a> Basics</h2>
<ul>
<li>There&rsquo;s a <em>lot</em> of syntax</li>
<li>Much of it recognizable from elsewhere:
<ul>
<li>PHP syntax is basically Perl-lite</li>
<li>Perl regexps: <code>grep</code>, <code>sed</code>, <code>vi</code>, PCRE, etc.</li>
<li>Idioms from the shell, C, and even BASIC</li>
</ul>
</li>
</ul>
</section>
<section>
<h2><a name=Basics href=#Basics></a> Basics</h2>
<!-- exec-raw cat examples/hello.pl -->
<pre><code>#!/usr/bin/env perl
print "Hello EngProd.\n";
</code></pre>
<!-- end -->
<!-- exec -->
<pre><code>$ ./examples/hello.pl
Hello EngProd.
</code></pre>
<!-- end -->
</section>
<section>
<h2><a name=Basics-boilerplate-edition href=#Basics-boilerplate-edition></a> Basics - boilerplate edition</h2>
<!-- exec-raw cat examples/hello_boilerplate.pl -->
<pre><code>#!/usr/bin/env perl
use warnings;
use strict;
use 5.10.0;
say greet($ARGV[0]);
sub greet {
my ($greetee) = @_;
return "Hello $greetee.";
}
</code></pre>
<!-- end -->
<!-- exec -->
<pre><code>$ ./examples/hello_boilerplate.pl 'EngProd'
Hello EngProd.
</code></pre>
<!-- end -->
</section>
<section>
<h2><a name=A-basic-filter href=#A-basic-filter></a> A basic filter</h2>
<!-- exec-raw cat examples/filter_authors.pl -->
<pre><code>#!/usr/bin/env perl
use warnings;
use strict;
use 5.10.0;
# Extract name where given name matches "John":
while (&lt;&gt;) {
say "$2 $1" if m/^(.*)\t(Jo.*?)(\t|$)/i;
}
</code></pre>
<!-- end -->
<!-- exec -->
<pre><code>$ ./examples/filter_authors.pl examples/authors.tsv
John Brunner
John Tolkien
Jo Walton
</code></pre>
<!-- end -->
</section>
<script>
$(document).ready(function () {
// ☜ ☝ ☞ ☟ ☆ ✠ ✡ ✢ ✣ ✤ ✥ ✦ ✧ ✩ ✪
// get all the "slides", hide them
$sections = $('section');
$sections.hide();
var section_number = 0;
var $cur_section = $( $sections.get(section_number) );
$cur_section.toggle(); // show first slide
var transit = function (jump) {
$('button').hide();
$cur_section.hide();
section_number += jump;
if (section_number > ($sections.length - 1)) {
section_number = 0;
}
$cur_section = $( $sections.get(section_number) );
$cur_section.toggle({
duration: 200,
done: function () { $('button').show(); }
});
};
var $fwd_button = $('<button class=clicker-button>next</button>');
var $bwd_button = $('<button class=clicker-button>prev</button>');
$fwd_button.click(function (e) {
e.preventDefault();
transit(1);
});
$bwd_button.click(function (e) {
e.preventDefault();
transit(-1);
});
$(document).keydown(function(e) {
switch(e.which) {
case 8: // backspace
case 37: // left
$bwd_button.click();
break;
case 32: // spacebar
case 39: // right
$fwd_button.click();
break;
case 90:
toggleFullScreen();
break;
default:
return;
}
e.preventDefault();
});
$button_group = $('<div class=buttons/>');
$button_group.append($bwd_button);
$button_group.append($fwd_button);
$('body').append($button_group);
var toggleFullScreen = function () {
if (!document.fullscreenElement && // alternative standard method
!document.mozFullScreenElement && !document.webkitFullscreenElement && !document.msFullscreenElement ) { // current working methods
if (document.documentElement.requestFullscreen) {
document.documentElement.requestFullscreen();
} else if (document.documentElement.msRequestFullscreen) {
document.documentElement.msRequestFullscreen();
} else if (document.documentElement.mozRequestFullScreen) {
document.documentElement.mozRequestFullScreen();
} else if (document.documentElement.webkitRequestFullscreen) {
document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
}
} else {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.msExitFullscreen) {
document.msExitFullscreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
}
}
};
});
</script>
</body>
</html>