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』

第10章. その他の制御構造

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

この章では、Perlのコードを書くための代替方法をいくつか紹介する。 ほとんどの場合、これらのテクニックはPerlをより強力にするものではないが、より簡単に、より便利に仕事をこなせるようにするものだ。自分のコードでこれらのテクニックを使う必要はないが、この章を読み飛ばそうという誘惑に駆られないでほしい。遅かれ早かれ、他の人のコードでこれらの制御構造を目にすることになるはずだからだ(実際、この本を読み終わる頃には、これらのテクニックが使われているのを目にすることになるだろう)。

支配構造がなければ

if 制御構造では、条件式が真のときだけコードのブロックが実行される。条件付きが偽のときだけコードブロックを実行したい場合は、 を に変更する:if unless

unless ($fred =~ /\A[A-Z_]\w*\z/i) {
  print "The value of \$fred doesn't look like a Perl identifier name.\n";
}

unless を使うと、この条件が真でない限り、コードのブロックを実行することになる。ちょうどif 、逆の条件付きでテストを行うようなものだ。別の言い方をすれば、else 節を単独で持っているようなものだ。つまり、理解できないunless を見たときはいつでも、(頭の中でも現実でも)それをif テストとして書き換えることができる:

if ($fred =~ /\A[A-Z_]\w*\z/i) {
  # Do nothing
} else {
  print "The value of \$fred doesn't look like a Perl identifier name.\n";
}

それ以上でもそれ以下でもなく、同じ内部バイトコードにコンパイルできるはずだ。あるいは、否定演算子(! )を使って条件式を否定する方法もある:

if ( ! ($fred =~ /\A[A-Z_]\w*\z/i) ) {
  print "The value of \$fred doesn't look like a Perl identifier name.\n";
}

一般化すれば、あなたにとって最も理にかなったコードの書き方を選ぶべきだが、それはおそらくメンテナンス・プログラマにとっても最も理にかなった書き方だろうからだ。if を否定で書くのが一番理にかなっているなら、そうすればいい。しかし、多くの場合、unless を使うのが自然だと発見するだろう。

else句とunless句の関係

else unlessこの構文はサポートされているが、混乱を招く可能性がある:

unless ($mon =~ /\AFeb/) {
  print "This month has at least thirty days.\n";
} else {
  print "Do you see what's going on here?\n";
}

特に、最初の節が非常に短く(おそらく1行だけ)、2番目の節が数行のコードになる場合、これを使いたいと思う人もいるだろう。しかし、これを否定if 、あるいは単に節を入れ替えて普通のif :

if ($mon =~ /\AFeb/) { print "Do you see what's going on here?\n"; } else { print ...
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