Skip to content

Instantly share code, notes, and snippets.

View xtetsuji's full-sized avatar

OGATA Tetsuji xtetsuji

View GitHub Profile
@xtetsuji
xtetsuji / fib2.pl
Last active March 22, 2019 09:47
samples for getting Fibonacci function from closure function.
#!/usr/bin/perl
use strict;
use warnings;
sub get_fib_with_initvals {
my ($first, $second) = @_;
# Perl の代入は右結合なので、 my $fib = sub { ... } で右辺に $fib があってはいけない。
# この場合、宣言と代入を分けて書く必要がある
my $fib;
@xtetsuji
xtetsuji / inurl
Created March 18, 2019 07:49
inurl - open URLs in the default browser, URLs are choiced from STDIN by peco
#!/usr/bin/env perl
# 2019/03/14
# inurl - 標準入力を peco に渡して、URL がある行を選択するとその URL をブラウザで開く
use strict;
use warnings;
use IPC::Open2;
my (@url, $child_out, $child_in);
open2 $child_out, $child_in, 'peco';
@xtetsuji
xtetsuji / backup-clipboard.pl
Created February 22, 2019 07:39
Clipboard contained markdown dumper for backup
#!/usr/bin/env perl
# backup-clipboard.pl
# 実行するとクリップボードを監視して、対象とする Markdown だと思ったら
# 雑に /tmp 以下にダンプする
use strict;
use warnings;
use AnyEvent;
use AnyEvent::Mac::Pasteboard;
@xtetsuji
xtetsuji / redmine-wiki.pl
Created February 15, 2019 02:42
Redmine wiki very simple access command line utility
#!/usr/bin/env perl
use v5.14;
use strict;
use warnings;
use utf8;
binmode 'STDOUT', ':utf8';
# JSON::PP はバイト列ではなく内部文字を返すので decode が必要
# ただ perldoc JSON::PP を読むと枝葉が自動的に内部文字になるようにしてくれるオプションはありそう(あんまり読んでいない)
@xtetsuji
xtetsuji / dice-exp-calc.pl
Created February 14, 2019 14:53
ダイスギャンブル、乱数で総当たり版と、パターン全部から期待値洗い出し版 https://twitter.com/xtetsuji/status/1088390124751056897
#!/usr/bin/perl
# N回サイコロを振ったときの出る目の全ての場合を割り出して期待値を計算する
use strict;
use warnings;
use List::Util qw(sum);
use constant BONUS_TRIO1 => 10_000;
use constant BONUS_TRIO456 => 2_000;
use constant DEBUG => $ENV{DEBUG};
@xtetsuji
xtetsuji / factor-output-more.txt
Last active October 9, 2018 15:47
Is 333...31 prime? Easy shell one-liner checker.
$ perl -e 'for (1..50) { my $n = "3" x $_ . "1"; system "factor", $n; }'
31: 31
331: 331
3331: 3331
33331: 33331
333331: 333331
3333331: 3333331
33333331: 33333331
333333331: 17 19607843
3333333331: 673 4952947
@xtetsuji
xtetsuji / smtp-connect-bench.pl
Created August 31, 2018 04:54
measure script for SMTP connection establish times
#!/usr/bin/perl
# smtp-connect-bench.pl [HOSTNAME|IPADDR]
# SMTP 接続の速度を確認する
use strict;
use warnings;
use Net::SMTP;
use Time::HiRes qw(time sleep); # ミリ秒化
use constant WAIT_SECONDS => 15;
use constant SMTP_TIMEOUT_SECONDS => 120;
@xtetsuji
xtetsuji / zipcode.pl
Created August 26, 2018 07:48
Japanese zipcode serarch by KEN_ALL.CSV plain text. It is written at Perl-Entrance in Tokyo, 2018/08/25 for entertainment live coding show.
#!/usr/bin/env perl
use strict;
use warnings;
use utf8; # このスクリプトは UTF-8 で書かれている、文字列リテラルはそういう文字として評価する
use Encode qw(encode decode);
binmode STDOUT, ':utf8';
# https://www.post.japanpost.jp/zipcode/dl/kogaki-zip.html
my $file = 'KEN_ALL.CSV';
@xtetsuji
xtetsuji / rotate-stick.pl
Created July 22, 2018 13:01
Rotate 1 character "stick" by carriage return (\r).
#!/usr/bin/perl
# \r を使って文字のパラパラまんが的なもの
use strict;
use warnings;
use Time::HiRes qw(sleep);
# 標準出力は \n を送らないとバッファリングして画面に表示してくれないことがあるので、バッファリングしないでねという指示
$| = 1; # STDOUT のバッファリングをオフにしてほしい
@xtetsuji
xtetsuji / gihyo-pdf-duplicate-check.pl
Created June 21, 2018 14:01
File duplicate check between directories iCloud and Dropbox. File duplicate state explains by color.
#!/usr/bin/perl
# 2018/06/21 xtetsuji
# Dropbox と iCloud で片方にしか無い、または両方にあるファイルを色分け
# このスクリプトでは WEB+DB PRESS と Software Design で調査する
#
# iCloud で、クラウド側にはファイルはあるけれど手元にデータを
# 同期していない場合、foo.txt の代わりに .foo.txt.icloud というファイルが
# 置かれることに注意
use strict;