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

4 years ago
  1. <!DOCTYPE html>
  2. <html lang=en>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>perl</title>
  6. <link rel=stylesheet href="slides.css" />
  7. <link rel="alternate" type="application/atom+xml" title="changes" href="//p1k3.com/userland-book/feed.xml" />
  8. <link rel="stylesheet" href="js/styles/sunburst.css">
  9. <script src="js/highlight.pack.js"></script>
  10. <script src="js/jquery.js" type="text/javascript"></script>
  11. <script>hljs.initHighlightingOnLoad();</script>
  12. </head>
  13. <body>
  14. <section>
  15. <p style="font-size: 3em; margin-bottom: 0; text-align: center;">
  16. All the Perl you need
  17. </p>
  18. <p style="font-size: 2em; text-align: center;">
  19. for slicing and dicing text files
  20. </p>
  21. <p style="text-align: center;">
  22. Brennen Bearnes &lt;bbearnes@wikimedia.org&gt;
  23. </p>
  24. <p style="text-align: center;"><small>
  25. Source:
  26. <a href="https://code.p1k3.com/gitea/brennen/wmf-engprod-offsite-slides">
  27. code.p1k3.com/gitea/brennen/wmf-engprod-offsite-slides
  28. </a><br />
  29. Slides:
  30. <a href="https://squiggle.city/~brennen/perl/">
  31. squiggle.city/~brennen/perl/
  32. </a>
  33. </small></p>
  34. </section>
  35. <section>
  36. <h2><a name=Agenda href=#Agenda></a> Agenda</h2>
  37. <ol>
  38. <li> A capsule history</li>
  39. <li> What is Perl actually like?</li>
  40. <li> Basic syntax and techniques</li>
  41. <li> Examples</li>
  42. </ol>
  43. </section>
  44. <section>
  45. <h2><a name=A-capsule-history href=#A-capsule-history></a> A capsule history</h2>
  46. <ul>
  47. <li>1987 - Started by Larry Wall</li>
  48. <li>1991 - <em>Programming Perl</em> - the Camel Book</li>
  49. <li>1994-95 - Perl 5, CPAN</li>
  50. <li>late 1990s - Perl CGI unerpins much of the web</li>
  51. <li>2000ish - Massive derail into Perl 6 development</li>
  52. </ul>
  53. </section>
  54. <section>
  55. <h2><a name=What-rsquo-s-Perl-actually-like href=#What-rsquo-s-Perl-actually-like></a> What&rsquo;s Perl actually like?</h2>
  56. <ul>
  57. <li>Backronym: &ldquo;Practical Extraction and Report Language&rdquo;.</li>
  58. <li>&ldquo;There&rsquo;s more than one way to do it&rdquo;</li>
  59. <li>&ldquo;Executable line-noise&rdquo;</li>
  60. <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>
  61. </ul>
  62. </section>
  63. <section>
  64. <h2><a name=What-rsquo-s-Perl-actually-like href=#What-rsquo-s-Perl-actually-like></a> What&rsquo;s Perl actually like?</h2>
  65. <p>A general-purpose, multi-paradigm programming language.</p>
  66. <p>That also has a lot in common with the Unix shell.</p>
  67. </section>
  68. <section>
  69. <h2><a name=What-rsquo-s-Perl-actually-like href=#What-rsquo-s-Perl-actually-like></a> What&rsquo;s Perl actually like?</h2>
  70. <p>General-purpose, multi-paradigm programming language.</p>
  71. <p>That also has a lot in common with the Unix shell.</p>
  72. <p>So: <strong>A widely-installed environment where you can combine small tools to work
  73. with data.</strong></p>
  74. </section>
  75. <section>
  76. <p>A sample data file, tab-separated names of some authors:</p>
  77. <!-- exec -->
  78. <pre><code>$ column -t examples/authors.tsv
  79. Robinson Eden
  80. Waring Gwendolyn L.
  81. Brunner John
  82. Tolkien John Ronald Reuel
  83. Walton Jo
  84. Toews Miriam
  85. Cadigan Pat
  86. Le Guin Ursula K.
  87. Veselka Vanessa
  88. Wells Martha
  89. Leckie Ann
  90. </code></pre>
  91. <!-- end -->
  92. </section>
  93. <section>
  94. <p>Perl is often described as a superset of <code>grep</code>, <code>sed</code>, and <code>awk</code>.</p>
  95. <p>Combines filtering, sorting, and transforming strings with stuff that&rsquo;s hard
  96. (or missing) in Bash:</p>
  97. <ul>
  98. <li>Functions</li>
  99. <li>Clean basic data structures</li>
  100. <li>Robust quoting</li>
  101. <li>Sensible control structures</li>
  102. <li>Variable scope</li>
  103. </ul>
  104. </section>
  105. <section>
  106. <p>Like <code>grep</code>:</p>
  107. <!-- exec -->
  108. <pre><code>$ perl -ne 'print if m/^T/;' examples/authors.tsv | column -t
  109. Tolkien John Ronald Reuel
  110. Toews Miriam
  111. </code></pre>
  112. <!-- end -->
  113. </section>
  114. <section>
  115. <p>Like <code>cut</code> or <code>awk</code>:</p>
  116. <!-- exec -->
  117. <pre><code>$ perl -anE 'say @F[1];' examples/authors.tsv
  118. Eden
  119. Gwendolyn
  120. John
  121. John
  122. Jo
  123. Miriam
  124. Pat
  125. Guin
  126. Vanessa
  127. Martha
  128. Ann
  129. </code></pre>
  130. <!-- end -->
  131. </section>
  132. <section>
  133. <p>Like <code>sed</code> or <code>tr</code>:</p>
  134. <!-- exec -->
  135. <pre><code>$ perl -pe 'tr/[a-z]/[A-Z]/' examples/authors.tsv | column -t
  136. ROBINSON EDEN
  137. WARING GWENDOLYN L.
  138. BRUNNER JOHN
  139. TOLKIEN JOHN RONALD REUEL
  140. WALTON JO
  141. TOEWS MIRIAM
  142. CADIGAN PAT
  143. LE GUIN URSULA K.
  144. VESELKA VANESSA
  145. WELLS MARTHA
  146. LECKIE ANN
  147. </code></pre>
  148. <!-- end -->
  149. </section>
  150. <section>
  151. <p>I&rsquo;m not here to convince you to write large programs in Perl.</p>
  152. <p>I want to gesture at a portion of the language useful for:</p>
  153. <ul>
  154. <li>One-liners</li>
  155. <li>Short utility scripts</li>
  156. <li>Exploratory data work &amp; transforms</li>
  157. </ul>
  158. <p>Let&rsquo;s go over some basic syntax and techniques, and then look at a few
  159. examples from my toolkit.</p>
  160. </section>
  161. <section>
  162. <h2><a name=Basics href=#Basics></a> Basics</h2>
  163. <ul>
  164. <li>There&rsquo;s a <em>lot</em> of syntax</li>
  165. <li>Much of it recognizable from elsewhere:
  166. <ul>
  167. <li>PHP syntax is basically Perl-lite</li>
  168. <li>Perl regexps: <code>grep</code>, <code>sed</code>, <code>vi</code>, PCRE, etc.</li>
  169. <li>Idioms from the shell, C, and even BASIC</li>
  170. </ul>
  171. </li>
  172. </ul>
  173. </section>
  174. <section>
  175. <h2><a name=Basics href=#Basics></a> Basics</h2>
  176. <!-- exec-raw cat examples/hello.pl -->
  177. <pre><code>#!/usr/bin/env perl
  178. print "Hello EngProd.\n";
  179. </code></pre>
  180. <!-- end -->
  181. <!-- exec -->
  182. <pre><code>$ ./examples/hello.pl
  183. Hello EngProd.
  184. </code></pre>
  185. <!-- end -->
  186. </section>
  187. <section>
  188. <h2><a name=Basics-boilerplate-edition href=#Basics-boilerplate-edition></a> Basics - boilerplate edition</h2>
  189. <!-- exec-raw cat examples/hello_boilerplate.pl -->
  190. <pre><code>#!/usr/bin/env perl
  191. use warnings;
  192. use strict;
  193. use 5.10.0;
  194. say greet($ARGV[0]);
  195. sub greet {
  196. my ($greetee) = @_;
  197. return "Hello $greetee.";
  198. }
  199. </code></pre>
  200. <!-- end -->
  201. <!-- exec -->
  202. <pre><code>$ ./examples/hello_boilerplate.pl 'EngProd'
  203. Hello EngProd.
  204. </code></pre>
  205. <!-- end -->
  206. </section>
  207. <section>
  208. <h2><a name=A-basic-filter href=#A-basic-filter></a> A basic filter</h2>
  209. <!-- exec-raw cat examples/filter_authors.pl -->
  210. <pre><code>#!/usr/bin/env perl
  211. use warnings;
  212. use strict;
  213. use 5.10.0;
  214. # Extract name where given name matches "John":
  215. while (&lt;&gt;) {
  216. say "$2 $1" if m/^(.*)\t(Jo.*?)(\t|$)/i;
  217. }
  218. </code></pre>
  219. <!-- end -->
  220. <!-- exec -->
  221. <pre><code>$ ./examples/filter_authors.pl examples/authors.tsv
  222. John Brunner
  223. John Tolkien
  224. Jo Walton
  225. </code></pre>
  226. <!-- end -->
  227. </section>
  228. <script>
  229. $(document).ready(function () {
  230. // ☜ ☝ ☞ ☟ ☆ ✠ ✡ ✢ ✣ ✤ ✥ ✦ ✧ ✩ ✪
  231. // get all the "slides", hide them
  232. $sections = $('section');
  233. $sections.hide();
  234. var section_number = 0;
  235. var $cur_section = $( $sections.get(section_number) );
  236. $cur_section.toggle(); // show first slide
  237. var transit = function (jump) {
  238. $('button').hide();
  239. $cur_section.hide();
  240. section_number += jump;
  241. if (section_number > ($sections.length - 1)) {
  242. section_number = 0;
  243. }
  244. $cur_section = $( $sections.get(section_number) );
  245. $cur_section.toggle({
  246. duration: 200,
  247. done: function () { $('button').show(); }
  248. });
  249. };
  250. var $fwd_button = $('<button class=clicker-button>next</button>');
  251. var $bwd_button = $('<button class=clicker-button>prev</button>');
  252. $fwd_button.click(function (e) {
  253. e.preventDefault();
  254. transit(1);
  255. });
  256. $bwd_button.click(function (e) {
  257. e.preventDefault();
  258. transit(-1);
  259. });
  260. $(document).keydown(function(e) {
  261. switch(e.which) {
  262. case 8: // backspace
  263. case 37: // left
  264. $bwd_button.click();
  265. break;
  266. case 32: // spacebar
  267. case 39: // right
  268. $fwd_button.click();
  269. break;
  270. case 90:
  271. toggleFullScreen();
  272. break;
  273. default:
  274. return;
  275. }
  276. e.preventDefault();
  277. });
  278. $button_group = $('<div class=buttons/>');
  279. $button_group.append($bwd_button);
  280. $button_group.append($fwd_button);
  281. $('body').append($button_group);
  282. var toggleFullScreen = function () {
  283. if (!document.fullscreenElement && // alternative standard method
  284. !document.mozFullScreenElement && !document.webkitFullscreenElement && !document.msFullscreenElement ) { // current working methods
  285. if (document.documentElement.requestFullscreen) {
  286. document.documentElement.requestFullscreen();
  287. } else if (document.documentElement.msRequestFullscreen) {
  288. document.documentElement.msRequestFullscreen();
  289. } else if (document.documentElement.mozRequestFullScreen) {
  290. document.documentElement.mozRequestFullScreen();
  291. } else if (document.documentElement.webkitRequestFullscreen) {
  292. document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
  293. }
  294. } else {
  295. if (document.exitFullscreen) {
  296. document.exitFullscreen();
  297. } else if (document.msExitFullscreen) {
  298. document.msExitFullscreen();
  299. } else if (document.mozCancelFullScreen) {
  300. document.mozCancelFullScreen();
  301. } else if (document.webkitExitFullscreen) {
  302. document.webkitExitFullscreen();
  303. }
  304. }
  305. };
  306. });
  307. </script>
  308. </body>
  309. </html>