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』

第15章 プロセス管理 プロセス管理

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

プログラマとしての醍醐味のひとつは、自分で書かなくてもいいように他人のコードを起動することだ。Perlから直接他のプログラムを起動して、子プロセスを管理する方法を学ぼう。

そして、Perlの他のすべてのものと同じように、重複やバリエーション、特殊化など、やり方は1つではない。だから、もし最初のやり方が気に入らなければ、もう1、2ページ読み進めれば、あなたの好みに合った解決法が見つかるだろう。

Perlは非常に移植性が高い。本書の残りの大部分では、Unixシステムではこのように動作し、Windowsではこのように動作し、VMSでは別の方法で動作するというような注釈は必要ない。しかし、マシンで他のプログラミングを発見するとき、Macintoshでは古いCray(かつて「スーパー」コンピュータだった)とは異なるプログラムが利用できる。この章の例は、主にUnixをベースにしている。Unix以外のシステムを使っている場合は、いくつかの違いが見られることが予想される。

システム 機能

Perlで子プロセスを起動してプログラミングを実行する最も簡単な方法は、system 関数を使うことだ。例えば、PerlからUnixのdateコマンドを呼び出すには、system 、実行したいプログラミングを指定する:

system 'date';

これらのコマンドは、あなたのシステム、そのシステムが提供するもの、そしてそのコマンドがどのように実装されているかに依存する。これらはPerlではなく、Perlがあなたのプログラムのためにシステムに依頼するものなのだ。同じUnixコマンドでも、オペレーティングシステムのバージョンが違えば、呼び出し規約やオプションが異なるかもしれない。

Windowsを使用している場合、このコードは日付を表示するが、新しい日付を入力するよう促す。プログラミングは、あなたが新しい日付を入力するのを待つ。おそらく、/T:

system 'date /T';

これを親プロセスから実行する。実行すると、system コマンドは、子プロセスと呼ばれる、あなたのPerlプログラムの同一コピーを作成する。子プロセスは、Perlの標準入力、標準出力、標準エラーを共有しながら、即座にdateのような実行したいコマンドに自身を変更する。つまり、dateが生成する通常の短い日付と時刻の文字列は、PerlのSTDOUT がすでにGoしていたところであれば、どこにでも行き着くことになる。

system関数のパラメータは、一般的にシェルで通常入力するものである。例えば、ls -l $HOMEでホーム・ディレクトリの内容をリストアップするような複雑なコマンドの場合、パラメータにその内容をすべて入力することができる:

system 'ls -l $HOME';

$HOME はシェル変数で、あなたのホーム・ディレクトリへのパスを知っている。Perlの変数ではないので、補間する必要はない。もし二重引用符で囲んでいたら、$ をエスケープして補間を防ぐ必要がある:

system "ls -l \$HOME";

Windowsでは、同じタスクはdirコマンドを使う。% 記号はPerl変数ではなく、コマンドに属する。しかし、ハッシュは二重引用符で囲まれた文字列では補間されないので、エスケープする必要はない:

system "cmd /c dir %userprofile%" ...
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