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
Use Perl as Better Shell Script
Search
karupanerura
May 31, 2025
Programming
0
700
Use Perl as Better Shell Script
Houtou.pm #1
karupanerura
May 31, 2025
Tweet
Share
More Decks by karupanerura
See All by karupanerura
GoでParserを書く
karupanerura
4
910
マイ隙間家具OSSたちのご紹介
karupanerura
2
260
Mustache Template 話 2024
karupanerura
0
25
1つの言語を原点に色々な言語を学ぶということ
karupanerura
4
2.9k
Javaの実装をPerlでテストする
karupanerura
0
1.2k
Go Generics Overview
karupanerura
0
1.3k
Japan.pm 2021 開催まで至る道
karupanerura
0
2.1k
Perl MongersのためのAWS_CDK入門
karupanerura
0
140
Webシステムのパフォーマンス・チューニング
karupanerura
2
830
Other Decks in Programming
See All in Programming
事業戦略を理解してソフトウェアを設計する
masuda220
PRO
22
6.2k
ドメインモデリングにおける抽象の役割、tagless-finalによるDSL構築、そして型安全な最適化
knih
11
1.9k
AIネイティブなプロダクトをGolangで挑む取り組み
nmatsumoto4
0
120
GoのWebAssembly活用パターン紹介
syumai
3
10k
型付きアクターモデルがもたらす分散シミュレーションの未来
piyo7
0
800
Cursor AI Agentと伴走する アプリケーションの高速リプレイス
daisuketakeda
1
120
Claude Codeの使い方
ttnyt8701
1
130
DroidKnights 2025 - 다양한 스크롤 뷰에서의 영상 재생
gaeun5744
3
300
A comprehensive view of refactoring
marabesi
0
970
機械学習って何? 5分で解説頑張ってみる
kuroneko2828
0
220
つよそうにふるまい、つよい成果を出すのなら、つよいのかもしれない
irof
1
290
AIコーディング道場勉強会#2 君(エンジニア)たちはどう生きるか
misakiotb
1
240
Featured
See All Featured
Rails Girls Zürich Keynote
gr2m
94
14k
For a Future-Friendly Web
brad_frost
179
9.8k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
32
5.9k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
30
2.1k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
31
1.2k
Adopting Sorbet at Scale
ufuk
77
9.4k
Code Review Best Practice
trishagee
68
18k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.5k
The Cost Of JavaScript in 2023
addyosmani
51
8.4k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
48
2.8k
Producing Creativity
orderedlist
PRO
346
40k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
667
120k
Transcript
Use Perl as Better Shell Script @karupanerura Houtou.pm #1
@karupanerura • Perl / Go / Java / TypeScript /etc..
• PAUSE: KARUPA • Software Engineer @ DeNA, Co,. LTD. • Chief Director @ Japan Perl Association
ࢁསͱࢲ • όϯυ • Live @ KAZOO HALL • ٢ా͏ͲΜ
• ͖ • ࢁསେֶϫΠϯ • ͏·͍ https://www.yamanashi.ac.jp/social/3131
None
Shell Script
Shell Script • ShellߏจΛͦͷ··ར༻ͨ͠εΫϦϓτ • ίϚϯυΛॻ͍ͯͦͷ··࣮ߦͰ͖ΔͷͰײత • CLIϕʔεͷૢ࡞ΛࣗಈԽ͢Δͷʹศར • WebܥͷਓͳΒ1ॻ͍ͨ͜ͱ͋Δͣʁ
None
؆୯ʂศརʂ
؆୯……ʁ
Shell Scriptͷ͠͞ • ίϚϯυͷڥࠩҟ • macOSͱLinuxͰsedawkͳͲͰBSD/GNUҧ͍͕͋ΔʢΦϓγϣϯ͕ҟͳΔʣ • ref. https://speakerdeck.com/mackee/perl-for-shell-awk-and-sed-programmers •
࣮ࡍͷίϚϯυͱϏϧτΠϯίϚϯυͱγΣϧߏจ͕ࠞࡏ͢Δ • bashͷtestͱ[ͱ[[ͷҧ͍Λઆ໌Ͱ͖ΔͬͯਓͲΕ͘Β͍͍·͔͢ʁ • ෳࡶͳσʔλߏΛѻ͏ͷ͕ۤख
খωλ: testͱ[ͱ[[ͷҧ͍ bash-5.2$ type [ [ γΣϧΈࠐΈؔͰ͢ bash-5.2$ type test
test γΣϧΈࠐΈؔͰ͢ bash-5.2$ type [[ [[ γΣϧͷ༧ޠͰ͢
খωλ: testͱ[ͱ[[ͷҧ͍ bash-5.2$ type [ [ γΣϧΈࠐΈؔͰ͢ bash-5.2$ type test
test γΣϧΈࠐΈؔͰ͢ bash-5.2$ type [[ [[ γΣϧͷ༧ޠͰ͢ ಉ͡ΈࠐΈؔ(alias) ߏจʢਖ਼نදݱͳͲαϙʔτʣ
ෳࡶͳσʔλߏͬͯͨͱ͑ • HashMap(Dict)ͷதʹ͞ΒʹHashMap(Dict)Λ࣋ͭͱ͔ • {"user1":{"user2":true}} • ͦͷͳ͔ʹ͞ΒʹArrayΛ࣋ͭͱ͔ • {"user1":{"user2":[100,200]}}
ෳࡶͳσʔλߏ in Shell Script $ declare -A data=(["user1"]=(["user2"]=true)) bash: ༧ظ͠ͳ͍τʔΫϯ
`(' पลʹߏจΤϥʔ͕͋Γ·͢
ෳࡶͳσʔλߏ in Shell Script $ declare -A data=(["user1"]=(["user2"]=true)) bash: ༧ظ͠ͳ͍τʔΫϯ
`(' पลʹߏจΤϥʔ͕͋Γ·͢ ʢૉʹʣ Ͱ͖ͳ͍
ෳࡶͳ͜ͱΛ͢Δͷ͍͠
͍͜͠ͱΛ͍ͨ͠ͱ͖……?
ಠஅͱภݟʹΑΓϓϩάϥϛϯάݴޠΛൺֱ • Ruby/Go/JavaScript/PHP: ڥʹΑͬͯೖ͍ͬͯͳ͍ • Python • ͍·Ͱ͍͍ͩͨͷڥʹPython3ܥ͕ೖ͍ͬͯΔ • Shellͱߏจ͕͍ͩͿҧ͏ͷͰॻ͖͕͑ͪΐͬͱखؒ
• Perl • ͍͍ͩͨͷڥʹೖ͍ͬͯΔ͠Shellͱߏจ͍ۙ
PerlͱbashͷߏจΛൺֱ #!/usr/bin/perl use strict; use warnings; # コマンドライン引数の取得 my $arg1
= shift @ARGV; # 配列の宣言 my @array; # コマンドの実行 system 'grep status:500 /var/log/ access.log | ...'; #!/bin/bash set -ue -o pipefail # コマンドライン引数の取得 arg1=$1; shift # 配列の宣言 declare -a array # コマンドの実行 grep status:500 /var/log/access.log | ...
PerlͱbashͷߏจΛൺֱ # ίϚϯυΛ࣮ߦͯ݁͠ՌΛมʹೖΕ͍ͨ my $result = `grep status:500 file`; #
݅ذ if ($x > 0) { ... } # ϧʔϓ while ($x > 0) { ... } # ίϚϯυΛ࣮ߦͯ݁͠ՌΛมʹೖΕ͍ͨ result=`grep status:500 file` # ݅ذ if [[ "$x" -gt 0 ]]; then ... fi # ϧʔϓ while [[ "$x" -gt 0 ]]; do ... done
Perl͡ΌΜ (?)
࣮ྫհͷίʔφʔ
ྫ: json-diff https://github.com/karupanerura/dot fi les/blob/master/bin/json-diff
ྫ: jwt https://github.com/karupanerura/dot fi les/blob/master/bin/jwt
༨ஊ: zxͱ͍͏ͭ͋Δ https://github.com/google/zx ׳ΕͯखʹೃછΊΑ͍͔
·ͱΊ
·ͱΊ • Shell Scriptศར͚ͩͲෳࡶͳ͜ͱΛΖ͏ͱ͢Δͱ͍͠ • ෳࡶͳ͜ͱΛΓ͍ͨͱ͖ϓϩάϥϛϯάݴޠʹஔ͖͑ΔͱΑ͍ • PerlShell Scriptͱߏจ͕͍ۙͷͰҠ২ϋʔυϧ͕͍ʢͱࢥ͏ʣ •
ਖ਼ղ͕͋ΔΘ͚Ͱͳ͍ͷͰɺΓ͍͢ํ๏Λࡧ͠·͠ΐ͏