Skip to Content
『Learning Perl, 8th Edition』
book

『Learning Perl, 8th Edition』

by Randal L. Schwartz, brian d foy, Tom Phoenix
March 2025
Beginner to intermediate
398 pages
5h 33m
Japanese
O'Reilly Media, Inc.
Content preview from 『Learning Perl, 8th Edition』

第12章. ファイルテスト

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

先に、出力のためにファイルハンドルを開く方法を紹介した。通常、この操作で新しいファイルが作成され、同じ名前の既存のファイルは消去される。 その名前のファイルが存在しないことを確認したい場合もあるだろう。指定されたファイルの古さを知りたいかもしれない。あるいは、ファイルのリストを調べて、あるバイト数より大きく、ある時間アクセスされていないファイルを発見したいかもしれない。Perlには、ファイルに関する情報を発見するために使用できるテストの完全なセットがある。

ファイルテスト演算子

Perlにはファイルに関する特定の情報を得るためのファイルテスト演算子がある。これらはすべて-X の形をとり、X は特定のテストを表す(少し混乱させるために、-X というリテラルなファイルテスト演算子もある)。ほとんどの場合、これらの演算子は真か偽を返す。我々はこれらを演算子と呼んでいるが、その説明はperlfuncで発見できるだろう。

注

リストを取得するには、perldoc -f-Xというコマンドラインを使う。この-X はリテラルで、コマンドラインスイッチではない。perldocを使って個別に調べることができないので、これはすべてのファイル・テスト演算子の代わりとなる。

新しいファイルを作成するプログラミングを開始する前に、そのファイルがすでに存在しないことを確認し、重要な表計算データファイルや重要な誕生日カレンダーを誤って上書きしてしまわないようにしたい。そのためには、-e ファイル・テストを使って、ファイル名が存在するかどうかをテストすることができる:

die "Oops! A file called '$filename' already exists.\n"
  if -e $filename;

この場合、システムがリクエストを拒否したことを報告しているわけではないので、このdie メッセージに$! を含めていないことに注意してほしい。以下は、ファイルが最新に保たれているかどうかをチェックする例である。この場合、文字列のファイル名ではなく、すでに開いているファイルハンドルをテストしている。あなたのプログラミングの構成ファイルは、1週間か2週間ごとに更新されるべきだとしよう。(過去28日間にファイルが変更されていなければ、何かが間違っている。-M ファイル・テストは、プログラミングを開始する前のファイルの変更時間を日単位で返す:

warn "Config file is looking pretty old!\n"
  if -M CONFIG > 28;

3つ目の例はもっと複雑だ。ディスクスペースが一杯になってきたので、ディスクを買い足す代わりに、大きくて役に立たないファイルはバックアップに移すことにしたとする。しかし、たとえファイルが大きくても、過去90日間アクセスされていない(つまり、あまり頻繁に使われていないことがわかる)のでなければ、倉庫に入れるべきでない。-s file test演算子は、trueかfalseを返す代わりに、ファイル・サイズをバイト単位で返す(既存のファイルは0バイトかもしれない):

my @original_files = qw/ fred barney betty wilma pebbles dino bamm-bamm /; my @big_old_files; # The ones we want to ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Learning Perl, 8th Edition

Learning Perl, 8th Edition

Randal L. Schwartz, brian d foy, Tom Phoenix
The AWK Programming Language, 2nd Edition

The AWK Programming Language, 2nd Edition

Alfred V. Aho, Brian W. Kernighan, Peter J. Weinberger

Publisher Resources

ISBN: 9798341636231