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』

第13章 ディレクトリの操作 ディレクトリ演算子

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

第12章で作成したファイルは、一般的にプログラムと同じ場所にあった。しかし、最近のオペレーティングシステムでは、ファイルをディレクトリにまとめることができる。Perlでは、オペレーティング・システム間でかなり移植性の高い方法で、これらのディレクトリを直接操作することができる。

Perlは、どのシステム上で動作しても同じように動作するよう、懸命に努力している。にもかかわらず、この章ではPerlがUnixの歴史を好んでいることを確かに示している。Windowsを使っているなら、Win32 。これらのモジュールはWin32 APIへのフックを提供している。

現在の作業ディレクトリ

あなたのプログラミングは作業ディレクトリで実行される。これは、あなたのプログラムが実行するすべてのデフォルト・ディレクトリである。

Cwd モジュール(スタンダード・ライブラリーの一部)を使えば、そのディレクトリが何であるかを知ることができる。show_my_cwdと呼ぶこのプログラミングを試してみよう:

use v5.10;
use Cwd;
say "The current working directory is ", getcwd();

これは、Unixシェルでpwdを実行したり、Windowsコマンドシェルでcd(引数なし)を実行した場合と同じディレクトリになるはずだ。この本でPerlを練習している間は、ほとんどの場合、プログラムを格納しているのと同じディレクトリで作業しているはずだ。

相対パス(ファイルシステムツリーの先頭からの完全なパスを示さないパス)を使ってファイルを開くと、Perlはその相対パスをカレントディレクトリから解釈する。現在の作業ディレクトリが/home/fredだとする。ファイルを読み取るためにこのコードを実行すると、Perlは/home/fred/相対パス/path.txtを探す:

# relative to the current working directory
open my $fh, '<:utf8', 'relative/path.txt'

シェルやターミナル・プログラムを使っていない場合、プログラミングを実行するものは、現在の作業ディレクトリについて異なる考えを持っているかもしれない。エディターからプログラムを実行できる場合、そのアプリケーションの現在の作業ディレクトリは、あなたがファイルを保存したディレクトリとは異なるかもしれない。cronのようなものでプログラミングをスケジューリングしても、おそらく同じことができるだろう。

現在の作業ディレクトリは、必ずしもプログラムを格納しているディレクトリとは限らない。これらのコマンドはどちらもカレント・ディレクトリにあるmy_programを探す:

$ ./show_my_cwd
$ perl show_my_cwd

しかし、プログラムのフルパスを与えれば、別のディレクトリからそのプログラムを実行することができる:

$ /home/fred/show_my_cwd
$ perl /home/fred/show_my_cwd

シェルがプログラムを発見するために検索するディレクトリの1つにプログラミングを置けば、好きなディレクトリからパスヒントなしでプログラムを実行できる:

$ show_my_cwd
注

File::Specモジュール(標準ライブラリーの一部)を使えば、相対パスと絶対パスを変換できる。 ...

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