Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Perl Golf
Search
ynonperek
April 09, 2012
Programming
4
2.2k
Perl Golf
An introduction to golfind
ynonperek
April 09, 2012
Tweet
Share
More Decks by ynonperek
See All by ynonperek
QtRuby for Qt Developers
ynonperek
0
320
Qt Hybrid Apps
ynonperek
1
280
QtRuby In Action
ynonperek
1
170
Cool CPAN Modules
ynonperek
2
640
Advanced Perl Moose
ynonperek
4
2.7k
Ruby Desktop Apps with Qt
ynonperek
1
620
git
ynonperek
3
830
Concurrency In Qt Applications
ynonperek
1
360
Moose Design Patterns
ynonperek
4
600
Other Decks in Programming
See All in Programming
CSC307 Lecture 02
javiergs
PRO
1
770
大規模Cloud Native環境におけるFalcoの運用
owlinux1000
0
260
dchart: charts from deck markup
ajstarks
3
970
Data-Centric Kaggle
isax1015
2
710
AI Agent Dojo #4: watsonx Orchestrate ADK体験
oniak3ibm
PRO
0
130
Patterns of Patterns
denyspoltorak
0
1.3k
高速開発のためのコード整理術
sutetotanuki
1
330
余白を設計しフロントエンド開発を 加速させる
tsukuha
7
2k
Pythonではじめるオープンデータ分析〜書籍の紹介と書籍で紹介しきれなかった事例の紹介〜
welliving
3
850
re:Invent 2025 トレンドからみる製品開発への AI Agent 活用
yoskoh
0
700
今こそ知るべき耐量子計算機暗号(PQC)入門 / PQC: What You Need to Know Now
mackey0225
3
350
GISエンジニアから見たLINKSデータ
nokonoko1203
0
190
Featured
See All Featured
Darren the Foodie - Storyboard
khoart
PRO
2
2.2k
Producing Creativity
orderedlist
PRO
348
40k
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
95
The SEO identity crisis: Don't let AI make you average
varn
0
57
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
1.8k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
116
100k
GraphQLの誤解/rethinking-graphql
sonatard
74
11k
The Cost Of JavaScript in 2023
addyosmani
55
9.4k
New Earth Scene 8
popppiees
1
1.4k
Everyday Curiosity
cassininazir
0
120
Designing for Timeless Needs
cassininazir
0
120
The agentic SEO stack - context over prompts
schlessera
0
600
Transcript
Let’s Golf Thursday, May 3, 12
About Me • Ynon Perek • http://ynonperek.com • http://speakerdeck.com/u/ ynonperek
Thursday, May 3, 12
The Game A Competition to find the shortest — fewest
keystrokes perl code to solve a given problem Thursday, May 3, 12
The Rules • Count keystrokes (newlines matter) • No modules
• No external tools Thursday, May 3, 12
Why Golf ? Thursday, May 3, 12
Example: Head use strict; use warnings; use v5.14; my $counter
= 0; while (<>) { $counter += 1; print; last if $counter > 10; } Thursday, May 3, 12
We Can Do Better while (<>) { print; last if
$. > 10; } Thursday, May 3, 12
Yet a golfer would • Remember perl opts: -pe •
Remember range operator Thursday, May 3, 12
Yet a golfer would #!/usr/bin/perl -pe last if $. >
10 Thursday, May 3, 12
Yet a golfer would perl -pe '11..exit' Thursday, May 3,
12
And a true golfer... perl -pe '11..&' Thursday, May 3,
12
Some More Tricks • y///c Abigail’s length horror • $_
x= Larry’s boolean • }{ Abigail’s END for -pe • $\ Van-der Pijll print Thursday, May 3, 12
Challenge • Use previous tricks to print: • characters count
• words count • line count Thursday, May 3, 12
Challenge # Characters count perl -lpe '$\+=y///c}{' # Words count
perl -lpe '$\+=split}{' # Lines count perl -pe '$\=$.}{' Thursday, May 3, 12
Example: tail • Tail is harder, because we need to
remember previous lines • Can use an array Thursday, May 3, 12
Example: tail # tail using an array perl -e '@a=<>;print
@a[-10..-1]' Thursday, May 3, 12
Example: tail # Removing the array perl -e 'print ((<>)[-10..-1])'
Thursday, May 3, 12
Example: tail # Removing the array perl -e 'print ((<>)[-10..-1])'
Surprisingly, A true hacker can do better Thursday, May 3, 12
Example: tail # Recall the ~ perl -e 'print+(<>)[~9..-1]' Surprisingly,
A true hacker can do better Thursday, May 3, 12
Some More Tricks • $= Holds only int values. $==10/3
is the same as int(10/3) • $- Holds only positive values Thursday, May 3, 12
Example: $- perl -pe '$-=eval or&' 10+20 20+3 30-10 40-50
-5 1 4 5 Thursday, May 3, 12
Golf Classics • Find all anagrams in a word list.
• An anagram is two words made up of the same letters • Example: underflow - wonderful Thursday, May 3, 12
Anagrams #!/usr/bin/perl sub normalized { my ($word) = @_; my
@letters = split //, $word; return join '', sort @letters; } while (<>) { chomp; my $letters = normalized( $_ ); $words{ $letters } ||= []; push $words{ $letters }, $_; } print "@$_\n" for grep { @$_ > 1} values %words; Thursday, May 3, 12
Anagrams • Remove the sub • Prefer text over array-refs
• Prefer switches over code • Use short variable names Thursday, May 3, 12
Anagrams #!/usr/bin/perl -ln $w{ join '', sort split // }
.= "$_ "; }{ print for grep / ./, values %w; Thursday, May 3, 12
Anagrams • No need to join hash keys • Can
use regexp instead of split • Can use regexp instead of grep • Can use %h instead of values %h Thursday, May 3, 12
Anagrams #!/usr/bin/perl -ln $w{ 1, sort/./g } .= "$_ ";
}{/ ./&&print for %w; Thursday, May 3, 12
Golf Classic sub b{[@b=(abs||No,bottle."s"x!!++$_,of,beer),on,the,wall]} print "@{+b},\n@b,\nTake one down, pass it
around,\n@{+b}.\n" for-pop||-99..-1 99 bottles of beer on the wall, 99 bottles of beer, Take one down, pass it around, 98 bottles of beer on the wall. Thursday, May 3, 12
Non-perl Golf • http://vimgolf.com/ • http://codegolf.com/ Thursday, May 3, 12
What’s Next • Go to http:// terje2.frox25.no- ip.org/ • Download
the book Thursday, May 3, 12