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』

第5章. インプットとアウトプット

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

これまでの練習で、いくつかの入出力(I/O)を可能にする方法をすでに見てきただろう。しかし、これからは、ほとんどのプログラミングで必要となるI/Oの80%をカバーすることで、これらの演算子についてさらに学んでいくことになる。標準入力、標準出力、標準エラーストリームの仕組みにすでに慣れているのであれば、あなたはこのゲームの先を行っていることになる。そうでない場合は、この章の終わりまでには追いつけるだろう。とりあえず、「標準入力」は "キーボード"、「標準出力」は "ディスプレイ画面 "と考えてほしい。

標準入力からの入力

標準入力ストリームからの読み取りは簡単だ。<STDIN> この演算子をスカラー・コンテキストで評価すると、次の入力行が得られる:

$line = <STDIN>;                # read the next line
chomp($line);                   # and chomp it

chomp($line = <STDIN>);         # same thing, more idiomatically

ここで行入力演算子(<STDIN> )と呼んでいるのは、実際にはファイルハンドルを囲む行入力演算子(山括弧で表されている)である。ファイルハンドルについてはこの章の後半で学ぶ。

line-input演算子はファイルの終端に達するとundef 、ループから抜けるのに便利である:

while (defined($line = <STDIN>)) {
  print "I saw $line";
}

入力を変数に読み取り、その変数が定義されているかどうかをチェックし、定義されていれば(つまり入力が終わっていなければ)、while ループ本体を実行しているのだ。ループ本体内では、$line の各行が次々に表示される。Perlはこのショートカットを用意している。ショートカットは以下のようになる:

while (<STDIN>) {
  print "I saw $_";
}

さて、このショートカットを作るために、ラリーは無駄な構文を選んだ。つまり、これは文字どおり「入力行を読み取り、それが真かどうかを確認する。(通常は真だ。)もし真なら、while ループに入るが、その入力行は捨てる!」と言っているのだ。実際のPerlプログラミングでそんなことをする必要はない。そこでラリーは、この役に立たない構文を便利なものにした。

Perlに入力を変数に読み取り、(結果が定義されている限り、ファイルの終端に達していない限り)while のループに入るように指示するのだ。ただし、入力を$line に格納する代わりに、Perlはお気に入りのデフォルト変数$_ を使う:

while (defined($_ = <STDIN>)) {
  print "I saw $_";
}

このショートカットは、このように記述した場合にのみ機能する。line-input演算子を他の場所に置くと(特に、単独で文として)、デフォルトでは$_ 、行を読み取ってくれない。これは、while ループの条件付きに行入力演算子以外何もない場合にのみ機能する。条件式に他のものを入れると、このショートカットは適用されない。

and演算子(<STDIN>)とPerlの大好きなデフォルト変数($_)の間には、他に何のつながりもない。しかしこの場合、Perlが入力をその変数に保存しているだけだ。

一方、リストコンテキストでline-input演算子を評価すると、(残りの)すべての入力行がリストとして得られる(リストの各要素が1行): ...

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