<figure class="swipe"><iframe src="https://www.swipe.to/embed/7299cm" allowfullscreen></iframe></figure><style>figure.swipe{display:block;position:relative;padding-bottom:56.25%;height:0;overflow:hidden;}figure.swipe iframe{position:absolute;top:0;left:0;width:100%;height:100%;border:none;}</style>
<figure><iframe width="600" height="338" src="https://www.swipe.to/embed/7299cm" allowfullscreen></iframe></figure>
坂本 昭
坂本 昭 (さかもと あきら) / @sakamoto_akira_
マジックザギャザリングがすごい好きです
余った時間に
Perlワンライナーを使う方が増えて人類の生産性が上がればいいと思っています
Perlの得意分野を使い倒している技術です
このたぐいの作業を3行以下のスクリプトでやっつけるなら無敵
よくある業務: さかもとの場合
$ git clone https://github.com/sakamossan/yapc_perl_oneliner_beginner.git
$ cd yapc_perl_oneliner_beginner
zombie
という単語を含む行を抽出します$ perl -nlE "/zombie/ and print" ios_app_names.tsv
$ perl -nlE "/zombie/ and print" ios_app_names.tsv
# 入力から1行づつ読み込んで $_ に入れる (-nオプション)
while ($_ = <ARGV>) {
# 改行文字をよしなに処理 (-lオプション)
chomp $_;
# 正規表現にマッチしたら出力
print $_ if $_ =~ /zombie/;
}
$ curl -s "http://decks.jp/" | \
perl -nlE ' /<a .*href="(\S+)"/ and say $1'
$ curl -s "http://decks.jp/" | \
perl -nlE ' /<a .*href="(\S+)"/ and say $1'
# パイプで渡されたhtmlを1行づつ読み込んで
while (defined($_ = <ARGV>)) {
chomp $_;
# aタグを引っ掛けて、リファラ属性だけキャプチャする
if ($_ =~ /<a .*href="(\S+)"/) {
# マッチした場合、キャプチャした部分を出力する
say $1;
}
}
$ curl -s "http://decks.jp/" | \
perl -Ilib -MURI::Encode=uri_decode \
-nlE ' /<a .*href="(\S+)"/ and say uri_decode($1)'
$ curl -s "http://decks.jp/" | \
perl -Ilib -MURI::Encode=uri_decode \
-nlE ' /<a .*href="(\S+)"/ and say uri_decode($1)'
use lib "lib"; # libディレクトリ以下をuseできるように
use URI::Encode "uri_decode"; # 目的の関数をuse
while (defined($_ = <ARGV>)) {
chomp $_;
if (/<a .*href="(\S+)"/) { # マッチしたら
say uri_decode($1); # デコードして出力
}
}
上記の条件に当てはまれば、Perlワンライナーが得意なことだと思います
たとえば
```
$ tail -F fast.log | perl -nlE 'rand() < 0.5 and say'
$ cat access.log | perl -Ilib -MHTTP::BrowserDetect \
-nlE '/" "(.+?)"$/ and say HTTP::BrowserDetect->new($1)->robot' \
| sort | uniq -c
$ curl -s "http://decks.jp/deck/1" \
| perl -nlE '/<script/ .. /<\/script>/ and say'
$ perl -MString::Trigram=compare \
-nlE 'say compare("magneto", $_), "\t", $_ ' musicians.txt \
| sort -n | tail -3
0.1 Marvin Gaye
0.111111111111111 Incognito <- 無事にインコグニートが!
0.117647058823529 Maroon 5
これにてネタ切れです
$ cat ltsv | tr "\t" "\n" | perl -nlE "/^path:/ and say $'"