俺々Perl Advent Calendar Japan2011

my $root   = "/tmp/test/aaa/";
my $subdir = "aaa/";
my $file   = "str";

my $path = "$root/$subdir/$file";
warn $path;

while ($path =~ s#//#/#g) {}; # ここがミソ
warn $path;
__DATA__
/tmp/test/aaa//aaa//str at a.pl line 7. # // がある
/tmp/test/aaa/aaa/str at a.pl line 10.  # / 置換後!
    • エラー箇所を分かりやすく特定する方法
# loggerはログファイルに書き出すサブルーチンとか。
sub test1
{
   my $err = 1;
   if ($err) {
       logger(sprintf("file=%s line=%d", basename(__FILE__), (caller)[2]));
       # file=b.pl line=10
   }
   $err = 1;
   if ($err) {
       logger(sprintf("file=%s line=%d", basename(__FILE__), (caller)[2]));
       # file=b.pl line=14
   }
}
    • FTPとかSSHが使えない、root権限無いサーバーへのファイル転送方法
      • HTTPServerを立てて転送する
# host
$ ls 
$ b.pl
$ python -m CGIHTTPServer 8080

# remote
$ curl xxxxx:8080
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html>
<title>Directory listing for /</title>
<body>
<h2>Directory listing for /</h2>
<hr>
<ul>
<li><a href="a.pl">a.pl</a>
<li><a href="b.pl">b.pl</a>
</ul>
<hr>
</body>
</html>
$ curl xxxxx:8080/b.pl -O
$ ls
b.pl
$ curl xxxx:8080 -d name=test -d value=aaa
      • nohupって便利
# 以下のようにすると端末抜けてもhttpサーバー起動したままにできる
$ nohup python -m CGIHTTPServer 8080 >/dev/null 2>&1
    • そのファイルを編集する前に! bash展開簡単
# .o ファイル作って元ファイルは保持しておく
$ ls
b.pl
$ cp b.pl{,.o}   // cp b.pl b.pl.o に展開される
$ ls
b.pl b.pl.o
    • warningをhookしてバグを防ぐ
# warnが出るのは何か予定外のことなのでログとかに残す。時刻も書くとなおよし。
local $SIG{__WARN__} = sub{ 
   warn @_;
   open O, ">>/tmp/w";
   print O "@_\n"; 
   close O;
};
    • vsftpdのポートを変更する方法
      • どこかで書いた
    • perlモジュールのrpmがどうなってるか見てみる
      • いつか書く。。
      • サブルーチンの引数をハッシュで渡す
      • 標準モジュールを使おう! File::Temp, File::Basename
      • mkpathをevalしてない人はいないよね? File::Path
      • コマンドのエラーを取得する方法 $err=`cmd 2>&1`; if ($?) { warn $err }


…息切れ