Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Perl Golf
Search
ynonperek
April 09, 2012
Programming
2.3k
4
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Perl Golf
An introduction to golfind
ynonperek
April 09, 2012
More Decks by ynonperek
See All by ynonperek
QtRuby for Qt Developers
ynonperek
0
350
Qt Hybrid Apps
ynonperek
1
330
QtRuby In Action
ynonperek
1
200
Cool CPAN Modules
ynonperek
2
680
Advanced Perl Moose
ynonperek
4
2.7k
Ruby Desktop Apps with Qt
ynonperek
1
660
git
ynonperek
3
880
Concurrency In Qt Applications
ynonperek
1
440
Moose Design Patterns
ynonperek
4
650
Other Decks in Programming
See All in Programming
学びを記事・スライド・Xへ高速展開する― AI-assisted Learning Loopで回すコンテンツ生成術
ochtum
0
200
flutter_test のその先へ — Flutter アプリの LLM 機能に「評価」という品質保証を
kosukesaigusa
0
200
作れるエンジニアから、価値を届けるエンジニアへ
ryunakayama
1
240
GraphQL の onError と Nullability のこれから
quramy
0
210
まずはコードより業務と向き合う:テーブル解体のためにPdMを呼んだ話
sakawe_ee
0
190
はじめてのPLATEAU
nokonoko1203
1
620
AIで開発が速くなった先で、開発チームは何を目指すか
shihochan
1
2k
PumaからFalconに移行したRailsアプリケーションでの実践
falcon8823
0
370
複数ドメインを一つのアプリに統合するFlutterの設計と運用_FlutterKaigi2026登壇資料(須永高弘)
takahirosunaga
0
160
高橋メソッドでCVE解説シリーズ 〜CVE-2026-112816〜
hakkadaikon
0
230
小さなアプリを作って公開するまで
onozaty
0
150
LLM 101
dalinaum
0
470
Featured
See All Featured
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
600
Building Applications with DynamoDB
mza
96
7.3k
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
2
760
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
3
3.9k
GitHub's CSS Performance
jonrohan
1033
470k
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.6k
Code Reviewing Like a Champion
maltzj
529
40k
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
68
60k
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
290
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
330
Typedesign – Prime Four
hannesfritz
42
3.2k
Tell your own story through comics
letsgokoyo
1
1.1k
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