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』

第4章 サブルーチン サブルーチン

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

chomp 、 、 などの組み込みシステム関数はすでに見てきたし、使ってきただろう。しかし、他の言語と同様に、Perlにもユーザ定義関数であるreverse printサブルーチンを作成する機能がある。 これによって、1つのプログラムの中で1つのコードの塊を何度も再利用することができる。サブルーチンの名前は、Perlの識別子(アルファベット、数字、アンダースコア、ただし数字で始まることはできない)の前にアンパサンド( )を付けることができる。アンパサンドを省略できるときとできないときの規則がある。この章の終わりにはその規則がわかるだろう。今のところ、禁止されていないときは常にアンパサンドを使うようにしよう。もちろん、アンパサンドが禁止されている場所については、その都度説明する。&

サブルーチン名は別の名前空間から来るので、同じプログラミングの中に&fred というサブルーチンと$fred というスカラーがあってもPerlが混乱することはない-ただし、通常の状況でそうする理由はない。

サブルーチンの定義

独自のサブルーチンを定義するには、キーワードsub 、サブルーチンの名前(アンパサンドなし)、そしてサブルーチン本体を構成する波括弧内のコード・ブロックを使用する。こんな感じだ:

sub marine {
  $n += 1;  # Global variable $n
  print "Hello, sailor number $n!\n";
}

サブルーチン定義はプログラム・テキストのどこに書いてもよいが、CやPascalのようなプログラミング言語出身のプログラマは、ファイルの先頭に書くことを好む。また、サブルーチン定義をファイルの末尾に置いて、プログラムの主要部分が最初に来るようにすることを好む人もいるだろう。それはあなた次第だ。いずれにせよ、通常は前方宣言は必要ない。同じ名前の2つのサブルーチン定義がある場合、後のものが前のものを上書きする。ただし、警告を有効にしている場合は、Perlがそれを教えてくれる。一般化すると、これは悪い形式、あるいは混乱したメンテナンス・プログラマのサインとみなされる。

注

Perl中級者までは、異なるパッケージの同じ名前のサブルーチンについて話すことはない。

前の例でお気づきかもしれないが、サブルーチン本体内でグローバル変数を使用することができる。実際、これまで見てきた変数はすべてグローバル変数である。これは言語純粋主義者にとっては恐ろしいことだが、Perl開発チームは数年前に松明を持った怒れる暴徒を結成し、街から追い出した。プライベート変数の作り方は、「サブルーチンにおけるプライベート変数」のセクションで説明する。

サブルーチンを呼び出す

式の中からサブルーチンを呼び出すには、サブルーチン名(アンパサンド付き)を使う:

&marine;  # says Hello, sailor number 1!
&marine;  # says Hello, sailor number 2!
&marine;  # says Hello, sailor number 3!
&marine;  # says Hello, sailor number 4!

ほとんどの場合、呼び出しのことを単にサブルーチンの呼び出しと呼ぶ。この章を読んでいくと、サブルーチンを呼び出す他の方法も出てくるだろう。

戻り値

式の結果を使用しない場合でも、サブルーチンは常に式の一部として呼び出される。先ほど ...

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