[perlメモ]ファイルの情報を取得するstat関数

よくファイル情報を取得するのに利用するperl内臓のstat関数についてのメモ。

詳しくは下のページを参照の英語(英語)

NAME function – search.cpan.org
http://search.cpan.org/~nwclark/perl-5.8.9/pod/perlfunc.pod#stat

文法は下の通り、基本的には、ファイルパスやディレクトリパスを文字列で渡せばよい。

stat FILEHANDLE
stat EXPR
stat DIRHANDLE

 

実際のサンプルは、下記の通り。

($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($filename);

取得される情報の詳細は以下の通り、一般的に使うところしか訳してないので、詳細は参照元ドキュメントを見てください。

一般的には、modeとuid,gid,size,atime,mtimeくらいしか使わないと思う。たいていの場合、ファイルの最終変更時刻を取得するのに使うと思うし。

perlの場合「-M ファイルパス」(プログラム起動時の時刻より何時間前に作成されたかが日単位(小数点あり)で返ってくる。半日前なら0.5)である程度のファイルを作成してからの時間を取得することができる。

0 dev      ファイルシステムのデバイス番号
1 ino      inode番号
2 mode     ファイルモード  (タイプとパーミッション)
3 nlink    ファイルのハードリンク数(number of (hard) links to the file)
4 uid      numeric user ID of file’s owner
5 gid      numeric group ID of file’s owner
6 rdev     the device identifier (special files only)
7 size     ファイルサイズ(バイト単位)(total size of file, in bytes)
8 atime    最終アクセス時刻(last access time in seconds since the epoch)
9 mtime    最終変更時刻(last modify time in seconds since the epoch)
10 ctime    inode change time in seconds since the epoch (*)
11 blksize  preferred block size for file system I/O
12 blocks   actual number of blocks allocate

(Visited 104 times, 1 visits today)

タグ :