SAK 図書館
CGI-Perl 基礎編 (その三) 標準ファイル I/O open、unlink、opendir、readdir
■open (ファイルオープン)
$rcd = open(FILE, "path/filename");
| | |_ オープンモード省略 = シーケンシャル入力
| | ">path/filename" = シーケンシャル出力
| | "+>path/filename" = シーケンシャル入出力
| | ">>path/filename" = シーケンシャル追加出力
| |_ ファイルハンドル
|_ オープン結果
1 (真) = 正常
その他 = エラー
■close (ファイルクローズ)
close(FILE);
|_ ファイルハンドル
■unlink (ファイル削除)
$rcd = unlink("path/filename");
| |_ ファイル名
|_ 0 (偽) = エラー
その他 = 削除したファイルの数
■-e (ファイル存在確認)
$rcd = -e "path/filename";
| |_ ファイル or ディレクトリ
|_ 1 (真) = 存在する
その他 = 存在しない
■-s (ファイルサイズ)
$rcd = -s "path/filename";
| |_ ファイル
|_ 0 (偽) = エラー
その他 = バイト数
■テキストストリーム I/O サンプル
【例1】入力
if (!open(FILE, "test.txt")) {
die
}
while (!eof(FILE)) {
$rec = <FILE>;
}
close(FILE);
【例2】入力
open(FILE, "test.txt") or die;
while (<FILE>) {
$rec = $_;
}
close(FILE);
【例3】入力
open(FILE, "test.txt") or die;
@rec = <FILE>;
close(FILE);
【例4】出力
open(FILE, ">test.txt") or die;
print FILE, "aaaaa", "bbbbb", "\n";
close(FILE);
■排他制御
どうも独自に行うのが普通らしい。
sub filelock {
my($ct);
for ($ct = 1; $ct < 11; $ct++) {
if (-e "filelock.dat") {
sleep 1;
}
else {
if (open(FILE, ">filelock.dat")) {
close(FILE);
return 1;
}
}
}
return 0;
}
sub unfilelock {
my($ct);
for ($ct = 1; $ct < 11; $ct++) {
if (unlink "filelock.dat") {
return 1;
}
sleep 1;
}
return 0;
}
■opendir (ディレクトリオープン)
$rcd = opendir(DIR, "path");
| | |_ ディレクトリ
| |_ ファイルハンドル
|_ オープン結果
1 (真) = 正常
その他 = エラー
■closedir (ディレクトリクローズ)
closedir(DIR);
|_ ファイルハンドル
■ディレクトリ参照サンプル(正規表現使用例あり)
【例】昇順
opendir(DIR, "w_test") or die;
@file = sort readdir(DIR);
closedir(DIR);
** @file には、『.』と『..』も含まれるで注意。
【例】降順
opendir(DIR, "w_test") or die;
@file = sort {$b cmp $a} readdir(DIR);
closedir(DIR);
** @file には、『.』と『..』も含まれるで注意。
【例】サンプル
#!/usr/local/bin/perl
$dir = '../w_test';
opendir(DIR, $dir) or die;
@file = sort readdir(DIR);
closedir(DIR);
print << "END_OF_HTML";
Content-type: text/html
<HTML>
<HEAD>
</HEAD>
<BODY>
END_OF_HTML
foreach(@file) {
print "$_<BR>";
}
print << "END_OF_HTML";
</BODY>
</HTML>
END_OF_HTML
exit(0);
** @file には、『.』と『..』も含まれるで注意。
【例】ファイルだけ
$dir = '../w_test';
opendir(DIR, $dir) or die;
@file = sort grep { -f "$dir/$_"} readdir(DIR);
closedir(DIR);
【例】ディレクトリだけ
$dir = '../w_test';
opendir(DIR, $dir) or die;
@file = sort grep { -d "$dir/$_"} readdir(DIR);
closedir(DIR);
** @file には、『.』と『..』も含まれるで注意。
【例】te で始まるファイルのみ (i で大文字小文字区別なし)
$dir = '../w_test';
opendir(DIR, $dir) or die;
@file = sort grep { /^te/i && -f "$dir/$_"} readdir(DIR);
closedir(DIR);
【例】te*.cgi にマッチ (i で大文字小文字区別なし)
$dir = '../w_test';
opendir(DIR, $dir) or die;
@file = sort grep { /^te.*\.cgi/i && -f "$dir/$_"} readdir(DIR);
closedir(DIR);
【例】te が含まれるファイル名にマッチ (i で大文字小文字区別なし)
$dir = '../w_test';
opendir(DIR, $dir) or die;
@file = sort grep { /te/i && -f "$dir/$_"} readdir(DIR);
closedir(DIR);
【例】te*.cgi にマッチサンプル
#!/usr/local/bin/perl
$dir = '../w_test';
opendir(DIR, $dir) or die;
@file = sort grep { /^te.*\.cgi/i && -f "$dir/$_"} readdir(DIR);
closedir(DIR);
print << "END_OF_HTML";
Content-type: text/html
<HTML>
<HEAD>
</HEAD>
<BODY>
END_OF_HTML
foreach(@file) {
print "$_<BR>";
}
print << "END_OF_HTML";
</BODY>
</HTML>
END_OF_HTML
exit(0);
■CGI-Perl 基礎編資料
■CGI-Perl 基礎実地編資料
■CGI-Perl 応用実地編資料
■PHP 基礎編資料
■PHP + MySQL 編資料