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』

第16章. いくつかの高度なPerlテクニック

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

ここまで見てきたのはPerlの核心部分であり、Perlユーザとして理解しておくべき部分である。しかし、必須ではないが、ツールボックスに入れておくと便利なテクニックは他にもたくさんある。この章では、その中でも特に重要なものを集めた。また、この章は本書の続きであるPerl中級編にもつながっており、Perlの次のステップとなる。

しかし、章のタイトルに惑わされてはいけない。ここに書かれているテクニックは、すでに見たテクニックよりも理解するのが特に難しいものではない。単に初心者には必要ないという意味で「上級者向け」なのだ。この本を初めて読むときは、この章を飛ばして(あるいは読み飛ばして)Perlをすぐに使えるようにするといいだろう。1、2ヵ月後、Perlをもっと使いこなしたいと思ったときに、この章に戻ってくればいい。 この章全体は、大きな脚注だと思ってほしい。

スライス

与えられたリストの中からいくつかの要素だけを扱う必要があることはよくある。例えば、ベッドロック図書館は利用者の情報を大きなファイルに保存している。ファイルの各行は、コロンで区切られた6つのフィールド(名前、図書館カード番号、自宅住所、自宅電話番号、勤務先電話番号、現在貸し出し中のアイテムの数)で1人の利用者を記述している。ファイルの少しは次のようになる:

fred flintstone:2168:301 Cobblestone Way:555-1212:555-2121:3
barney rubble:709918:299 Cobblestone Way:555-3333:555-3438:0

図書館のあるアプリケーションでは、カード番号と貸し出し点数のみが必要で、他のデータは使わない。このようなものを使えば、必要なフィールドだけを取得することができる:

while (<$fh>) {
  chomp;
  my @items = split /:/;
  my($card_num, $count) = ($items[1], $items[5]);
  ...  # now work with those two variables
}

しかし、@items という配列は他に必要ない。もったいない。split の結果をスカラー・リストに代入する方がいいのかもしれない:

my($name, $card_num, $addr, $home, $work, $count) = split /:/;

これで不要な配列@itemsは回避できるが、実際には必要のないスカラー変数が4つもできてしまう。このような状況のために、$dummy_1 のようなダミーの変数名をいくつも作り、split のその要素には本当に関心がないことを示す人もいる。しかし、Larryはそれでは面倒だと考え、undef の特殊な使い方を追加した。undef を代入先のリストの項目として使用すると、Perlは単純にソース・リストの対応する要素を無視する:

my(undef, $card_num, undef, undef, undef, $count) = split /:/;

この方がいいのだろうか?まあ、不要な変数を使わないという利点はある。しかし欠点もある。どの要素が$count なのかを知るために、undefsをカウントしなければならないのだ。そして、リストの要素が多くなると、かなり扱いにくくなる。例えば、 ...

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