X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=slides.html;h=4ebcc71e3d1b49cff66dc4e3aa0881512d466c47;hb=0218bcdfa64147930d60d2140446285cd55247ad;hp=4f97d8d11078cfa1c7af9f08688409b8997bcc47;hpb=de803521052265c492f81daf0c6a3e78c1b5309d;p=chaz%2Ftalk-level-up-your-perl diff --git a/slides.html b/slides.html index 4f97d8d..4ebcc71 100644 --- a/slides.html +++ b/slides.html @@ -60,98 +60,6 @@ Deparse supports other flags that may be useful. Read the pod to find out more. Aside: Be careful about mixing high and low-precedence logical operators. ---- -class: ex-shellpod - -## Document your shell scripts with pod. - -```bash -#!/bin/sh - -: <<'=cut' -=head1 NAME - -therapist.sh - This script can save you a bunch of cash - -=cut - -echo 'Hello. Now, tell me about your problems.' - -while read response -do - printf "Patient said: %s" "$response" >>therapist_notes.txt - - echo 'And how did that make you feel?' -done - -echo "Goodbye! Let's meet again in two weeks." -``` - ---- -class: ex-shellpod - -## Document your shell scripts with pod. - -```bash -#!/bin/sh - -: <<'=cut' -*=head1 NAME -* -*therapist.sh - This script can save you a bunch of cash -* -*=cut - -echo 'Hello. Now, tell me about your problems.' - -while read response -do - printf "Patient said: %s" "$response" >>therapist_notes.txt - - echo 'And how did that make you feel?' -done - -echo "Goodbye! Let's meet again in two weeks." -``` - -??? -It's pod! In a shell script. - ---- -class: ex-shellpod - -## Document your shell scripts with pod. - -```bash -#!/bin/sh - -*: <<'=cut' -=head1 NAME - -therapist.sh - This script can save you a bunch of cash - -=cut - -echo 'Hello. Now, tell me about your problems.' - -while read response -do - printf "Patient said: %s" "$response" >>therapist_notes.txt - - echo 'And how did that make you feel?' -done - -echo "Goodbye! Let's meet again in two weeks." -``` - -??? -This is the key right here. Anybody know what this is? - -It's a heredoc. - -The colon command in bourne shell is just noop. Sometimes you'll see the colon used as a type of comment, but unlike -a comment the shell does parse the arguments to colon. - --- class: center, middle @@ -465,6 +373,97 @@ By the way, Vim supports embedded interpreters for other languages like Python, --- class: center, middle +## Document your shell scripts with pod. + +--- +class: ex-shellpod + +```bash +#!/bin/sh + +: <<'=cut' +=head1 NAME + +therapist.sh - This script can save you a bunch of cash + +=cut + +echo 'Hello. Now, tell me about your problems.' + +while read response +do + printf "Patient said: %s" "$response" >>therapist_notes.txt + + echo 'And how did that make you feel?' +done + +echo "Goodbye! Let's meet again in two weeks." +``` + +--- +class: ex-shellpod + +```bash +#!/bin/sh + +: <<'=cut' +*=head1 NAME +* +*therapist.sh - This script can save you a bunch of cash +* +*=cut + +echo 'Hello. Now, tell me about your problems.' + +while read response +do + printf "Patient said: %s" "$response" >>therapist_notes.txt + + echo 'And how did that make you feel?' +done + +echo "Goodbye! Let's meet again in two weeks." +``` + +??? +It's pod! In a shell script. + +--- +class: ex-shellpod + +```bash +#!/bin/sh + +*: <<'=cut' +=head1 NAME + +therapist.sh - This script can save you a bunch of cash + +=cut + +echo 'Hello. Now, tell me about your problems.' + +while read response +do + printf "Patient said: %s" "$response" >>therapist_notes.txt + + echo 'And how did that make you feel?' +done + +echo "Goodbye! Let's meet again in two weeks." +``` + +??? +This is the key right here. Anybody know what this is? + +It's a heredoc. + +The colon command in bourne shell is just noop. Sometimes you'll see the colon used as a type of comment, but unlike +a comment the shell does parse the arguments to colon. + +--- +class: center, middle + ## Understand calling context. ??? @@ -1413,57 +1412,66 @@ sub launch_missile ( $silo_id = get_default_silo_id() ) { --- class: ex-newfeatures -## Try some newer perl5 features. +### Postfix dereferencing ```perl -*use v5.24; -*use feature qw(signatures); # available in v5.20 -*no warnings qw(experimental::signatures); - -my $missile_inventory = { - ID => 20, - WY => 25, - CA => 195, -}; +use v5.24; -*sub get_default_silo_id () { - return $_ if 0 < $missile_inventory->{$_} for (sort keys $missile_inventory->%*); - die "No more missiles. :-(\n"; +my $person = { + name => 'Bob', + associates => [ + { + name => 'Karen', + }, + { + name => 'Doug', + }, + ], } -*sub launch_missile ( $silo_id = get_default_silo_id() ) { - die "Silo is empty.\n" if $missile_inventory->{$silo_id} <= 0; - $missile_inventory->{$silo_id} -= 1; - say "Missile launched from silo $silo_id."; -} +my @others = $person->{associates}->@*; +my %associate_attributes = $person->{associates}->@[0]->%*; ``` +### `@{...}` can be `->@*` at the end +### `%{...}` can be `->%*` at the end + --- -class: ex-newfeatures +class: ex-newfeatures2 -## Try some newer perl5 features. +### Subroutine signatures ```perl -use v5.24; -use feature qw(signatures); # available in v5.20 +use v5.20; +use feature qw(signatures); no warnings qw(experimental::signatures); -my $missile_inventory = { - ID => 20, - WY => 25, - CA => 195, -}; +sub only_one_arg($foo) { +} -sub get_default_silo_id () { -* return $_ if 0 < $missile_inventory->{$_} for (sort keys $missile_inventory->%*); - die "No more missiles. :-(\n"; +sub other_args_allowed_and_ignored($foo, @) { } -sub launch_missile ( $silo_id = get_default_silo_id() ) { - die "Silo is empty.\n" if $missile_inventory->{$silo_id} <= 0; - $missile_inventory->{$silo_id} -= 1; - say "Missile launched from silo $silo_id."; +sub positional_and_named($args, %other) { } + +# etc. +``` + +--- +class: ex-newfeatures2 + +### Strip leading space in heredocs + +```perl +use v5.26; + +do { + print <<~'HERE'; + Hi. The text here is indented in the source code, + but not in the console! + HERE; +}; ``` --- @@ -1668,10 +1676,10 @@ As this example shows, you can also do nifty stuff like destructure. .col[ - Use [`B::Deparse`](https://metacpan.org/pod/B::Deparse). -- Document your shell scripts with pod. - Know regular expressions. - Use [`Regexp::Debugger`](https://metacpan.org/pod/Regexp::Debugger). - Write Vim plugins in Perl. +- Document your shell scripts with pod. - Understand calling context. - Understand execution phases. - Know when to be terse and when not to.