【Perl】ファイルロック(排他処理)について語ろうat PHP
【Perl】ファイルロック(排他処理)について語ろう - 暇つぶし2ch813:nobodyさん
05/06/07 22:14:04
質問です。かの有名なperlメモで以下のように書きました。
my_flockのサブルーチンの中で、戻り値を「\%lfh」のようにリファレンスで返す理由がわかりません。どなたか教えてください

$lfh = my_flock() or die 'Busy!';
open(FILE,"+<$logfile");
chop($count = <FILE>);
$count++;
seek(FILE,0,0);
print FILE "$count\n";
close(FILE);
my_funlock($lfh);

sub my_flock {
  my %lfh = (dir => './lockdir/', basename => 'lockfile', timeout => 60, trytime => 5, @_);
  $lfh{path} = $lfh{dir} . $lfh{basename};
  for (my $i = 0; $i < $lfh{trytime}; $i++, sleep 1) {
    return \%lfh if (rename($lfh{path}, $lfh{current} = $lfh{path} . time));    
  }    # ■↑戻り値がなぜ\%lfh?
  opendir(LOCKDIR, $lfh{dir});
  my @filelist = readdir(LOCKDIR);
  closedir(LOCKDIR);
  foreach (@filelist) {
    if (/^$lfh{basename}(\d+)/) {
      return \%lfh if (time - $1 > $lfh{timeout} and rename($lfh{dir} . $_, $lfh{current} = $lfh{path} . time));
      last;    # ■↑戻り値がなぜ\%lfh?
    }
  }
  undef;
}
sub my_funlock {
  rename($_[0]->{current}, $_[0]->{path});
}


次ページ
続きを表示
1を表示
最新レス表示
レスジャンプ
類似スレ一覧
スレッドの検索
話題のニュース
おまかせリスト
オプション
しおりを挟む
スレッドに書込
スレッドの一覧
暇つぶし2ch