<?xml version='1.0' encoding='utf-8'?>
<pod xmlns="http://axkit.org/ns/2000/pod2xml">
<head>
	<title>NAME
<index>syntax</index></title>
</head>
<sect1>
<title>NAME
<index>syntax</index></title>
<para>
perlsyn - Perl syntax
</para>
<para>
perlsyn - Perl の文法
</para>
</sect1>
<sect1>
<title>DESCRIPTION</title>
<para>
A Perl program consists of a sequence of declarations and statements
which run from the top to the bottom.  Loops, subroutines and other
control structures allow you to jump around within the code.
</para>
<para>
Perl プログラムは、宣言と文の並びから構成され、上から下へと実行されます。
ループ、サブルーチン、その他の制御機構でコードの色々なところに
ジャンプできます。
</para>
<para>
Perl is a <strong>free-form</strong> language, you can format and indent it however
you like.  Whitespace mostly serves to separate tokens, unlike
languages like Python where it is an important part of the syntax.
</para>
<para>
Perl は <strong>自由書式</strong> 言語ですが、好きなように整形したりインデントしたり
できます。
空白が文法の重要な要素である Python のような言語と異なり、
空白はほとんどトークンの分割の役目です。
</para>
<para>
Many of Perl's syntactic elements are <strong>optional</strong>.  Rather than
requiring you to put parentheses around every function call and
declare every variable, you can often leave such explicit elements off
and Perl will figure out what you meant.  This is known as <strong>Do What I
Mean</strong>, abbreviated <strong>DWIM</strong>.  It allows programmers to be <strong>lazy</strong> and to
code in a style with which they are comfortable.
</para>
<para>
Perl の多くの文法要素は <strong>省略可能</strong> です。
全ての関数をかっこで括ったり、全ての変数を宣言したりすることを
要求するのではなく、しばしばそのような明示的な要素を置いておいて、
Perl にあなたが意味しているところを見つけ出させることができます。
これは <strong>Do What I Mean</strong> と知られ、頭文字を取って <strong>DWIM</strong> と呼ばれます。
これによって、プログラマを <strong>怠惰</strong> にでき、彼らが快適だと思うスタイルで
コーディングできるようにします。
</para>
<para>
Perl <strong>borrows syntax</strong> and concepts from many languages: awk, sed, C,
Bourne Shell, Smalltalk, Lisp and even English.  Other
languages have borrowed syntax from Perl, particularly its regular
expression extensions.  So if you have programmed in another language
you will see familiar pieces in Perl.  They often work the same, but
see <link xref='perltrap'>perltrap</link> for information about how they differ.
</para>
<para>
Perl は、awk, sed, C, Bourne Shell, Smalltalk, Lisp, 果ては英語といった、
多くの言語からコンセプトと <strong>文法を借用</strong> しています。
他の言語も Perl から文法を借用しています; 特に正規表現拡張をです。
従って、他の言語でプログラミングしていたなら、Perl にも見たことがあるような
ものがあるでしょう。
それらはしばしば同じように動作しますが、違う点についての情報は
<link xref='perltrap'>perltrap</link> を参照してください。
</para>
<sect2>
<title>Declarations
<index>declaration</index> <index>undef</index> <index>undefined</index> <index>uninitialized</index></title>
<para>
(宣言)
</para>
<para>
The only things you need to declare in Perl are report formats and
subroutines (and sometimes not even subroutines).  A variable holds
the undefined value (<code>undef</code>) until it has been assigned a defined
value, which is anything other than <code>undef</code>.  When used as a number,
<code>undef</code> is treated as <code>0</code>; when used as a string, it is treated as
the empty string, <code>&quot;&quot;</code>; and when used as a reference that isn't being
assigned to, it is treated as an error.  If you enable warnings,
you'll be notified of an uninitialized value whenever you treat
<code>undef</code> as a string or a number.  Well, usually.  Boolean contexts,
such as:
</para>
<para>
Perl で宣言が必要なものはレポートフォーマットとサブルーチンだけです
(サブルーチンすら宣言が不要な場合もあります)。
変数は、<code>undef</code> 以外の定義された値を代入されるまでは未定義値(<code>undef</code>)と
なります。
数値として使われる場合、<code>undef</code> は <code>0</code> として扱われます;
文字列として使われる場合、これは空文字列 <code>&quot;&quot;</code> として扱われます;
リファレンストして使われる場合、これは何も代入されていないので、エラーとして
扱われます。
警告を有効にしているなら、<code>undef</code> を文字列や数値として扱おうとすると
未初期価値を指摘されます。
ええ、普通は。
次のような真偽値コンテキストなら:
</para>
<verbatim><![CDATA[
my $a;
if ($a) {}
]]></verbatim>
<para>
are exempt from warnings (because they care about truth rather than
definedness).  Operators such as <code>++</code>, <code>--</code>, <code>+=</code>,
<code>-=</code>, and <code>.=</code>, that operate on undefined left values such as:
</para>
<para>
(定義済みかどうかではなく、真かどうかを考慮するので)警告から免れます。
未定義の左辺値を操作する、<code>++</code>, <code>--</code>, <code>+=</code>, <code>-=</code>, <code>.=</code> のような
演算子でも:
</para>
<verbatim><![CDATA[
my $a;
$a++;
]]></verbatim>
<para>
are also always exempt from such warnings.
</para>
<para>
とすることでもそのような警告から免れます。
</para>
<para>
A declaration can be put anywhere a statement can, but has no effect on
the execution of the primary sequence of statements--declarations all
take effect at compile time.  Typically all the declarations are put at
the beginning or the end of the script.  However, if you're using
lexically-scoped private variables created with <code>my()</code>, you'll
have to make sure
your format or subroutine definition is within the same block scope
as the my if you expect to be able to access those private variables.
</para>
<para>
宣言は、文が置けるところであればどこにでも置くことができますが、
基本的な文の並びは実行時には何の効果も持ちません -- 宣言はコンパイル時に
すべての効果が表れます。
典型的なすべての宣言は、スクリプトの先頭か終端に置かれます。
しかしながら、局所変数を <code>my()</code> を使って作成してレキシカルなスコープを
使っているのであれば、フォーマットやサブルーチンの定義を、同じブロックの
スコープの中でその局所変数にアクセスすることが可能であるようにしておく
必要があるでしょう。
</para>
<para>
Declaring a subroutine allows a subroutine name to be used as if it were a
list operator from that point forward in the program.  You can declare a
subroutine without defining it by saying <code>sub name</code>, thus:
<index>subroutine, declaration</index>
</para>
<para>
サブルーチンの宣言は、プログラムの後のほうにあるサブルーチン名を
リスト演算子のように使うことを許します。
定義されていないサブルーチンの宣言を、<code>sub name</code> と記述することで
宣言できるので、以下のようにできます。
<index>subroutine, declaration</index>
</para>
<verbatim><![CDATA[
sub myname;
$me = myname $0 		or die "can't get myname";
]]></verbatim>
<para>
Note that myname() functions as a list operator, not as a unary operator;
so be careful to use <code>or</code> instead of <code>||</code> in this case.  However, if
you were to declare the subroutine as <code>sub myname ($)</code>, then
<code>myname</code> would function as a unary operator, so either <code>or</code> or
<code>||</code> would work.
</para>
<para>
myname() 関数は、リスト演算子のように働くのであり、単項演算子としてでは
ないということに注意してください。
ですから、こういった場合に <code>||</code> の代わりに <code>or</code> を使うことには
注意してください。
しかし、サブルーチンを <code>sub myname ($)</code> のように宣言しているのであれば、
<code>or</code> でも <code>||</code> でもうまく行きます。
</para>
<para>
Subroutines declarations can also be loaded up with the <code>require</code> statement
or both loaded and imported into your namespace with a <code>use</code> statement.
See <link xref='perlmod'>perlmod</link> for details on this.
</para>
<para>
サブルーチンの宣言は <code>require</code> 文を使って詰め込むこともできますし、
<code>use</code> 文を使って自分の名前空間にロードしたりインポートしたりすることが
できます。
これに関する詳細は <link xref='perlmod'>perlmod</link> を参照してください。
</para>
<para>
A statement sequence may contain declarations of lexically-scoped
variables, but apart from declaring a variable name, the declaration acts
like an ordinary statement, and is elaborated within the sequence of
statements as if it were an ordinary statement.  That means it actually
has both compile-time and run-time effects.
</para>
<para>
文の並びはレキシカルスコープを持った変数の宣言を含むことができますが、
変数名の宣言とは切り離され、その宣言は通常の文のように振る舞い、
それが通常の文であるかのように文の並びに組みこまれます。
これは、そういった宣言がコンパイル時の効果と実行時の効果の両方を
持っているということです。
</para>
</sect2>
<sect2>
<title>Comments
<index>comment</index> <index>#</index></title>
<para>
(コメント)
</para>
<para>
Text from a <code>&quot;#&quot;</code> character until the end of the line is a comment,
and is ignored.  Exceptions include <code>&quot;#&quot;</code> inside a string or regular
expression.
</para>
<para>
コメントは <code>“#”</code> 文字から、行末まで続き、その部分は無視されます。
例外は、文字列や正規表現の中にある <code>&quot;#&quot;</code> です。
</para>
</sect2>
<sect2>
<title>Simple Statements
<index>statement</index> <index>semicolon</index> <index>expression</index> <index>;</index></title>
<para>
(単純文)
</para>
<para>
The only kind of simple statement is an expression evaluated for its
side effects.  Every simple statement must be terminated with a
semicolon, unless it is the final statement in a block, in which case
the semicolon is optional.  (A semicolon is still encouraged if the
block takes up more than one line, because you may eventually add
another line.)  Note that there are some operators like <code>eval {}</code> and
<code>do {}</code> that look like compound statements, but aren't (they're just
TERMs in an expression), and thus need an explicit termination if used
as the last item in a statement.
</para>
<para>
単純文となる唯一の種類は、その副作用のために評価される式です。
すべての単純文は、それがセミコロンを省略することのできるブロックの
最後にない限りは文を終端するためのセミコロンがなければなりません
(ブロックが二行以上に渡る場合には、セミコロンを付けることをお薦めします。
なぜなら、別の行を追加する可能性があるからです)。
<code>eval {}</code> や <code>do {}</code> のように、一見複合文のようにみえるけれども
そうではない(これらは単なる式における TERM です)ものがあって、
そういったものを文の最後のアイテムとして使った場合には明示的に終端する
必要があるのだということに注意してください。
</para>
</sect2>
<sect2>
<title>Truth and Falsehood
<index>truth</index> <index>falsehood</index> <index>true</index> <index>false</index> <index>!</index> <index>not</index> <index>negation</index> <index>0</index></title>
<para>
(真偽値)
</para>
<para>
The number 0, the strings <code>'0'</code> and <code>''</code>, the empty list <code>()</code>, and
<code>undef</code> are all false in a boolean context. All other values are true.
Negation of a true value by <code>!</code> or <code>not</code> returns a special false value.
When evaluated as a string it is treated as <code>''</code>, but as a number, it
is treated as 0.
</para>
<para>
数値 0, 文字列 <code>'0'</code> と <code>''</code>, 空リスト <code>()</code>, <code>undef</code> は全て真偽値
コンテキストでは偽となります。
その他の全ての値は真です。
真の値を <code>!</code> や <code>not</code> で否定すると、特殊な偽の値を返します。
これを文字列として評価すると <code>''</code> として扱われますが、数値として評価すると
0 として扱われます。
</para>
</sect2>
<sect2>
<title>Statement Modifiers
<index>statement modifier</index> <index>modifier</index> <index>if</index> <index>unless</index> <index>while</index>
<index>until</index> <index>foreach</index> <index>for</index></title>
<para>
(文修飾子)
</para>
<para>
Any simple statement may optionally be followed by a <emphasis>SINGLE</emphasis> modifier,
just before the terminating semicolon (or block ending).  The possible
modifiers are:
</para>
<para>
任意の単純文には、<strong>一つ</strong> の修飾子を終端のセミコロンの直前(もしくは
ブロックの終端の直前)に付けることができます。
使うことのできる修飾子は以下の通りです。
</para>
<verbatim><![CDATA[
if EXPR
unless EXPR
while EXPR
until EXPR
foreach LIST
]]></verbatim>
<para>
The <code>EXPR</code> following the modifier is referred to as the &quot;condition&quot;.
Its truth or falsehood determines how the modifier will behave.
</para>
<para>
修飾子に引き続く <code>EXPR</code> は「条件」として参照されます。
その真偽値が修飾子の振る舞いを決定します。
</para>
<para>
<code>if</code> executes the statement once <emphasis>if</emphasis> and only if the condition is
true.  <code>unless</code> is the opposite, it executes the statement <emphasis>unless</emphasis>
the condition is true (i.e., if the condition is false).
</para>
<para>
<code>if</code> は <emphasis>もし</emphasis> 条件が真の場合にのみ文を実行します。
<code>unless</code> は逆で、条件が真 <emphasis>でない限り</emphasis> (つまり、条件が偽なら) 文を
実行します。
</para>
<verbatim><![CDATA[
print "Basset hounds got long ears" if length $ear >= 10;
go_outside() and play() unless $is_raining;
]]></verbatim>
<para>
The <code>foreach</code> modifier is an iterator: it executes the statement once
for each item in the LIST (with <code>$_</code> aliased to each item in turn).
</para>
<para>
<code>foreach</code> 修飾子は反復子です:
LIST の値それぞれ毎に文を実行します(実行中は <code>$_</code> がそれぞれの値の
エイリアスとなります)。
</para>
<verbatim><![CDATA[
print "Hello $_!\n" foreach qw(world Dolly nurse);
]]></verbatim>
<para>
<code>while</code> repeats the statement <emphasis>while</emphasis> the condition is true.
<code>until</code> does the opposite, it repeats the statement <emphasis>until</emphasis> the
condition is true (or while the condition is false):
</para>
<para>
<code>while</code> は条件が真 <emphasis>の間</emphasis> 文を繰り返します。
<code>until</code> は逆で、条件が真 <emphasis>になるまで</emphasis> (つまり条件が偽の間) 文を
繰り返します:
</para>
<verbatim><![CDATA[
# Both of these count from 0 to 10.
print $i++ while $i <= 10;
print $j++ until $j >  10;
]]></verbatim>
<para>
The <code>while</code> and <code>until</code> modifiers have the usual &quot;<code>while</code> loop&quot;
semantics (conditional evaluated first), except when applied to a
<code>do</code>-BLOCK (or to the deprecated <code>do</code>-SUBROUTINE statement), in
which case the block executes once before the conditional is
evaluated.  This is so that you can write loops like:
</para>
<para>
修飾子 <code>while</code> と <code>until</code> は、一般的な &quot;<code>while</code> loop&quot; の意味を
持っています(条件が最初に評価される)が、<code>do</code>-ブロック(もしくは現在では
使用を推奨されていない <code>do</code>-サブルーチン文)に適用されるときは例外で、
このときは条件が評価されるよりも前に、一度ブロックが実行されます。
このため、次のようなループを記述することができます。
</para>
<verbatim><![CDATA[
do {
	$line = <STDIN>;
	...
} until $line  eq ".\n";
]]></verbatim>
<para>
See <link xref='perlfunc#do'>perlfunc/do</link>.  Note also that the loop control statements described
later will <emphasis>NOT</emphasis> work in this construct, because modifiers don't take
loop labels.  Sorry.  You can always put another block inside of it
(for <code>next</code>) or around it (for <code>last</code>) to do that sort of thing.
For <code>next</code>, just double the braces:
<index>next</index> <index>last</index> <index>redo</index>
</para>
<para>
<link xref='perlfunc#do'>perlfunc/do</link> を参照してください。
後述するループの制御文は、修飾子がループラベルを取らないために
この構造文では <emphasis>動作しない</emphasis> ということにも注意してください。
申し訳ない。
こういった場合に対処するのに別のブロックを内側に入れたり(<code>next</code> の場合)、
別のブロックで囲む(<code>last</code> の場合)という方法が常に使えます。
<code>next</code> では単に中かっこを二重にします:
<index>next</index> <index>last</index> <index>redo</index>
</para>
<verbatim><![CDATA[
do {{
	next if $x == $y;
	# do something here
}} until $x++ > $z;
]]></verbatim>
<para>
For <code>last</code>, you have to be more elaborate:
<index>last</index>
</para>
<para>
<code>last</code> の場合は、もっと念入りにする必要があります:
</para>
<verbatim><![CDATA[
LOOP: { 
	    do {
		last if $x = $y**2;
		# do something here
	    } while $x++ <= $z;
}
]]></verbatim>
<para>
<strong>NOTE:</strong> The behaviour of a <code>my</code> statement modified with a statement
modifier conditional or loop construct (e.g. <code>my $x if ...</code>) is
<strong>undefined</strong>.  The value of the <code>my</code> variable may be <code>undef</code>, any
previously assigned value, or possibly anything else.  Don't rely on
it.  Future versions of perl might do something different from the
version of perl you try it out on.  Here be dragons.
<index>my</index>
</para>
<para>
<strong>注意:</strong> (<code>my $x if ...</code> のような) 条件構造やループ構造で修飾された
<code>my</code> 文の振る舞いは <strong>未定義</strong> です。
<code>my</code> 変数の値は <code>undef</code> かも知れませんし、以前に代入された値かも
知れませんし、その他の如何なる値の可能性もあります。
この値に依存してはいけません。
perl の将来のバージョンでは現在のバージョンとは何か違うかも知れません。
ここには厄介なものがいます。
<index>my</index>
</para>
</sect2>
<sect2>
<title>Compound Statements
<index>statement, compound</index> <index>block</index> <index>bracket, curly</index> <index>curly bracket</index> <index>brace</index>
<index>{</index> <index>}</index> <index>if</index> <index>unless</index> <index>while</index> <index>until</index> <index>foreach</index> <index>for</index> <index>continue</index></title>
<para>
(複合文)
</para>
<para>
In Perl, a sequence of statements that defines a scope is called a block.
Sometimes a block is delimited by the file containing it (in the case
of a required file, or the program as a whole), and sometimes a block
is delimited by the extent of a string (in the case of an eval).
</para>
<para>
Perl では、スコープを定義するような文の並びをブロックと呼びます。
ブロックはそれを含むファイルによって範囲が定められることがあります
(ファイルが require されたときか、プログラム全体としての場合)し、
文字列の展開によって範囲が定められる(eval の場合)こともあります。
</para>
<para>
But generally, a block is delimited by curly brackets, also known as braces.
We will call this syntactic construct a BLOCK.
</para>
<para>
しかし一般的には、ブロックは中かっこによって範囲が定められます。
この構文的な構造をブロックと呼びます。
</para>
<para>
The following compound statements may be used to control flow:
</para>
<para>
以下に挙げる複合文を制御フローとして使うことができます:
</para>
<verbatim><![CDATA[
if (EXPR) BLOCK
if (EXPR) BLOCK else BLOCK
if (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK
LABEL while (EXPR) BLOCK
LABEL while (EXPR) BLOCK continue BLOCK
LABEL until (EXPR) BLOCK
LABEL until (EXPR) BLOCK continue BLOCK
LABEL for (EXPR; EXPR; EXPR) BLOCK
LABEL foreach VAR (LIST) BLOCK
LABEL foreach VAR (LIST) BLOCK continue BLOCK
LABEL BLOCK continue BLOCK
]]></verbatim>
<para>
Note that, unlike C and Pascal, these are defined in terms of BLOCKs,
not statements.  This means that the curly brackets are <emphasis>required</emphasis>--no
dangling statements allowed.  If you want to write conditionals without
curly brackets there are several other ways to do it.  The following
all do the same thing:
</para>
<para>
しかし注意して欲しいのは、C や Pascal とは異なり、ブロックを取るように
定義されていて文を取るではないということです。
つまり、中かっこは <emphasis>必要なもの</emphasis> です -- 曖昧な文が許されません。
中かっこなしの条件文を使いたいのであれば、いくつかのやり方があります。
以下の全ては同じことです:
</para>
<verbatim><![CDATA[
if (!open(FOO)) { die "Can't open $FOO: $!"; }
die "Can't open $FOO: $!" unless open(FOO);
open(FOO) or die "Can't open $FOO: $!";	# FOO or bust!
open(FOO) ? 'hi mom' : die "Can't open $FOO: $!";
			# a bit exotic, that last one
]]></verbatim>
<para>
The <code>if</code> statement is straightforward.  Because BLOCKs are always
bounded by curly brackets, there is never any ambiguity about which
<code>if</code> an <code>else</code> goes with.  If you use <code>unless</code> in place of <code>if</code>,
the sense of the test is reversed.
</para>
<para>
<code>if</code> 文は明解です。
ブロックは常に中かっこで区切られるので、<code>if</code> と <code>else</code> の対応が
曖昧になるようなことは決してありません。
<code>unless</code> を C の <code>if</code> のように使いたいのであれば、検査を反転します。
</para>
<para>
The <code>while</code> statement executes the block as long as the expression is
true (does not evaluate to the null string <code>&quot;&quot;</code> or <code>0</code> or <code>&quot;0&quot;</code>).
The <code>until</code> statement executes the block as long as the expression is
false.
The LABEL is optional, and if present, consists of an identifier followed
by a colon.  The LABEL identifies the loop for the loop control
statements <code>next</code>, <code>last</code>, and <code>redo</code>.
If the LABEL is omitted, the loop control statement
refers to the innermost enclosing loop.  This may include dynamically
looking back your call-stack at run time to find the LABEL.  Such
desperate behavior triggers a warning if you use the <code>use warnings</code>
pragma or the <strong>-w</strong> flag.
</para>
<para>
<code>while</code> 文は、式が真(評価した結果が空文字列 <code>&quot;&quot;</code> でも <code>0</code> でも
<code>&quot;0&quot;</code> でもない)である間、ブロックを実行します。
<code>until</code> 文は、式が偽である間、ブロックを実行します。
LABEL は省略可能ですが、ある場合には、コロンを伴った識別子になります。
LABEL は <code>next</code>、<code>last</code>、<code>redo</code> といったループ制御文のループを規定します。
LABEL が省略された場合、ループ制御文はそれを含むループの中で最も内側の
ループを参照します。
これは、実行時に LABEL を検出するための呼び出しスタックの動的な後戻り検索を
含むことができます。
そのような推奨されない振る舞いは、<code>use warnings</code> プラグマや <strong>-w</strong> フラグを
使った場合には警告を引き起こします。
</para>
<para>
If there is a <code>continue</code> BLOCK, it is always executed just before the
conditional is about to be evaluated again.  Thus it can be used to
increment a loop variable, even when the loop has been continued via
the <code>next</code> statement.
</para>
<para>
<code>continue</code> ブロックが存在する場合、
常に条件が再評価される直前に実行されます。
したがって、このブロックをループ変数のインクリメントのために
使うことができます。
これは、ループが<code>next</code>文を通して継続されるときでも実行されます。
</para>
</sect2>
<sect2>
<title>Loop Control
<index>loop control</index> <index>loop, control</index> <index>next</index> <index>last</index> <index>redo</index> <index>continue</index></title>
<para>
(loop 制御)
</para>
<para>
The <code>next</code> command starts the next iteration of the loop:
</para>
<para>
<code>next</code> コマンドはループの次の繰り返しを開始します:
</para>
<verbatim><![CDATA[
LINE: while (<STDIN>) {
	next LINE if /^#/;	# discard comments
	...
}
]]></verbatim>
<para>
The <code>last</code> command immediately exits the loop in question.  The
<code>continue</code> block, if any, is not executed:
</para>
<para>
<code>last</code> コマンドはループから即座に脱出します。
<code>continue</code> ブロックがあっても、それは実行されません:
</para>
<verbatim><![CDATA[
LINE: while (<STDIN>) {
	last LINE if /^$/;	# exit when done with header
	...
}
]]></verbatim>
<para>
The <code>redo</code> command restarts the loop block without evaluating the
conditional again.  The <code>continue</code> block, if any, is <emphasis>not</emphasis> executed.
This command is normally used by programs that want to lie to themselves
about what was just input.
</para>
<para>
<code>redo</code> コマンドは、条件の再評価をすることなしにループブロックの
再実行を行います。
<code>continue</code> ブロックがあっても、それは <emphasis>実行されません</emphasis>。
このコマンドは通常、プログラムに対する入力に関してプログラム自身を
だましたいといったときに使われます。
</para>
<para>
For example, when processing a file like <filename>/etc/termcap</filename>.
If your input lines might end in backslashes to indicate continuation, you
want to skip ahead and get the next record.
</para>
<para>
たとえば、<filename>/etc/termcap</filename> のようなファイルを処理することを
考えてみましょう。
もし入力された行の行末が継続を示すバックスラッシュであった場合、先へ進んで
次のレコードを取り出したいと思うでしょう。
</para>
<verbatim><![CDATA[
while (<>) {
	chomp;
	if (s/\\$//) {
	    $_ .= <>;
	    redo unless eof();
	}
	# now process $_
}
]]></verbatim>
<para>
which is Perl short-hand for the more explicitly written version:
</para>
<para>
これは Perl の省略記法で、もっとはっきりと書くと以下のようになります:
</para>
<verbatim><![CDATA[
LINE: while (defined($line = <ARGV>)) {
	chomp($line);
	if ($line =~ s/\\$//) {
	    $line .= <ARGV>;
	    redo LINE unless eof(); # not eof(ARGV)!
	}
	# now process $line
}
]]></verbatim>
<para>
Note that if there were a <code>continue</code> block on the above code, it would
get executed only on lines discarded by the regex (since redo skips the
continue block). A continue block is often used to reset line counters
or <code>?pat?</code> one-time matches:
</para>
<para>
上記の例で <code>continue</code> ブロックがあったとしたら、それは
(redo は continue ブロックをスキップするので) 正規表現によって
捨てられた行だけが実行されるということに注意してください。
continue ブロックは行カウンターをリセットするとか、
一度だけマッチする <code>?pat?</code> をリセットするのに使われます。
</para>
<verbatim><![CDATA[
# inspired by :1,$g/fred/s//WILMA/
while (<>) {
	?(fred)?    && s//WILMA $1 WILMA/;
	?(barney)?  && s//BETTY $1 BETTY/;
	?(homer)?   && s//MARGE $1 MARGE/;
} continue {
	print "$ARGV $.: $_";
	close ARGV  if eof();		# reset $.
	reset	    if eof();		# reset ?pat?
}
]]></verbatim>
<para>
If the word <code>while</code> is replaced by the word <code>until</code>, the sense of the
test is reversed, but the conditional is still tested before the first
iteration.
</para>
<para>
<code>while</code> を <code>until</code> で置き換えた場合検査の意味は逆転しますが、
繰り返しが実行されるより前に条件が検査されることは変わりありません。
</para>
<para>
The loop control statements don't work in an <code>if</code> or <code>unless</code>, since
they aren't loops.  You can double the braces to make them such, though.
</para>
<para>
ループ制御文は <code>if</code> や <code>unless</code> 中では動作しません。
なぜならそこはループではないからです。
しかし中かっこを二重にしてこれに対処することはできます。
</para>
<verbatim><![CDATA[
if (/pattern/) {{
	last if /fred/;
	next if /barney/; # same effect as "last", but doesn't document as well
	# do something here
}}
]]></verbatim>
<para>
This is caused by the fact that a block by itself acts as a loop that
executes once, see <link xref='#Basic_BLOCKs_and_Switch_Statem'>&quot;Basic BLOCKs and Switch Statements&quot;</link>.
</para>
<para>
これは、ブロック自身は一度だけ実行されるループとして動作するからです;
<link xref='#Basic_BLOCKs_and_Switch_Statem'>&quot;Basic BLOCKs and Switch Statements&quot;</link> を参照してください。
</para>
<para>
The form <code>while/if BLOCK BLOCK</code>, available in Perl 4, is no longer
available.   Replace any occurrence of <code>if BLOCK</code> by <code>if (do BLOCK)</code>.
</para>
<para>
Perl 4 では使うことのできた <code>while/if BLOCK BLOCK</code> という形式は、
もはや使うことができません。
<code>if BLOCK</code> の部分を <code>if (do BLOCK)</code> で置き換えてください。
</para>
</sect2>
<sect2>
<title>For Loops
<index>for</index> <index>foreach</index></title>
<para>
(for ループ)
</para>
<para>
Perl's C-style <code>for</code> loop works like the corresponding <code>while</code> loop;
that means that this:
</para>
<para>
Perl の C 形式の <code>for</code> ループは、対応する <code>while</code> ループと同様に
動作します。
つまり、以下のものは:
</para>
<verbatim><![CDATA[
for ($i = 1; $i < 10; $i++) {
	...
}
]]></verbatim>
<para>
is the same as this:
</para>
<para>
以下のものと同じです:
</para>
<verbatim><![CDATA[
$i = 1;
while ($i < 10) {
	...
} continue {
	$i++;
}
]]></verbatim>
<para>
There is one minor difference: if variables are declared with <code>my</code>
in the initialization section of the <code>for</code>, the lexical scope of
those variables is exactly the <code>for</code> loop (the body of the loop
and the control sections).
<index>my</index>
</para>
<para>
小さな違いが一つあります: <code>for</code> の初期化部で <code>my</code> を使って変数が
宣言された場合、この変数のレキシカルスコープは <code>for</code> ループ
(ループ本体と制御部) と完全に同じです。
<index>my</index>
</para>
<para>
Besides the normal array index looping, <code>for</code> can lend itself
to many other interesting applications.  Here's one that avoids the
problem you get into if you explicitly test for end-of-file on
an interactive file descriptor causing your program to appear to
hang.
<index>eof</index> <index>end-of-file</index> <index>end of file</index>
</para>
<para>
通常の、配列に対する添え字付けのループのほかにも、<code>for</code> は他の
多くの興味深いアプリケーションのために借用することができます。
以下の例は、対話的なファイル記述子の終端を明示的に検査してしまうと
プログラムをハングアップしたように見えてしまう問題を回避するものです。
<index>eof</index> <index>end-of-file</index> <index>end of file</index>
</para>
<verbatim><![CDATA[
$on_a_tty = -t STDIN && -t STDOUT;
sub prompt { print "yes? " if $on_a_tty }
for ( prompt(); <STDIN>; prompt() ) {
	# do something
}
]]></verbatim>
<para>
Using <code>readline</code> (or the operator form, <code>&lt;EXPR&gt;</code>) as the
conditional of a <code>for</code> loop is shorthand for the following.  This
behaviour is the same as a <code>while</code> loop conditional.
<index>readline</index> <index>&lt;&gt;</index>
</para>
<para>
<code>for</code> ループの条件として <code>readline</code> (または演算子形式の <code>&lt;EXPR&gt;</code>) を
使う場合、以下のように省略形が使えます。
この振る舞いは <code>while</code> ループ条件と同じです。
<index>readline</index> <index>&lt;&gt;</index>
</para>
<verbatim><![CDATA[
for ( prompt(); defined( $_ = <STDIN> ); prompt() ) {
    # do something
}
]]></verbatim>
</sect2>
<sect2>
<title>Foreach Loops
<index>for</index> <index>foreach</index></title>
<para>
(foreach ループ)
</para>
<para>
The <code>foreach</code> loop iterates over a normal list value and sets the
variable VAR to be each element of the list in turn.  If the variable
is preceded with the keyword <code>my</code>, then it is lexically scoped, and
is therefore visible only within the loop.  Otherwise, the variable is
implicitly local to the loop and regains its former value upon exiting
the loop.  If the variable was previously declared with <code>my</code>, it uses
that variable instead of the global one, but it's still localized to
the loop.  This implicit localisation occurs <emphasis>only</emphasis> in a <code>foreach</code>
loop.
<index>my</index> <index>local</index>
</para>
<para>
<code>foreach</code> ループは 通常のリスト値に対しての繰り返しを行い、変数 VAR に
リストの要素を繰り返し一回に一つずつセットします。
変数の前に <code>my</code> というキーワードが置かれていた場合、その変数は
レキシカルスコープを持ち、したがってそれはループの中でのみ可視となります。
このキーワードがなければ、変数はループに対してローカルとなり、ループを
抜けた後で以前の値が再度取得されます。
変数が事前に <code>my</code> を使って宣言されていたならば、グローバルなものの
代わりにその変数を使いますが、それもループにローカルなものとなります。
この暗黙のローカル化は <code>foreach</code> の中で <emphasis>のみ</emphasis> 起きます。
<index>my</index> <index>local</index>
</para>
<para>
The <code>foreach</code> keyword is actually a synonym for the <code>for</code> keyword, so
you can use <code>foreach</code> for readability or <code>for</code> for brevity.  (Or because
the Bourne shell is more familiar to you than <emphasis>csh</emphasis>, so writing <code>for</code>
comes more naturally.)  If VAR is omitted, <code>$_</code> is set to each value.
<index>$_</index>
</para>
<para>
読みやすさのために <code>foreach</code> を、簡潔さのために <code>for</code> を使うことが
できます(あるいは C シェルよりも Bourne シェルに親しんでいるのなら
<code>for</code> の方が自然でしょう)。
VAR が省略された場合には、<code>$_</code> に値が設定されます。
<index>$_</index>
</para>
<para>
If any element of LIST is an lvalue, you can modify it by modifying
VAR inside the loop.  Conversely, if any element of LIST is NOT an
lvalue, any attempt to modify that element will fail.  In other words,
the <code>foreach</code> loop index variable is an implicit alias for each item
in the list that you're looping over.
<index>alias</index>
</para>
<para>
LIST の要素が左辺値であった場合、ループの中で VAR を変更することにより、
対応する値を変更することができます。
逆に、LIST の要素が左辺値でない場合は、この要素を修正しようとしても
失敗します。
言い換えると、<code>foreach</code> ループの帰納変数がループの対象となっている
リスト中の個々のアイテムに対するエイリアスになっているからです。
<index>alias</index>
</para>
<para>
If any part of LIST is an array, <code>foreach</code> will get very confused if
you add or remove elements within the loop body, for example with
<code>splice</code>.   So don't do that.
<index>splice</index>
</para>
<para>
LIST のいずれかの部分が配列であった場合に、たとえば <code>splice</code> を使って
ループの本体でその要素を削除したりあるいは追加したりすると
<code>foreach</code> は非常に混乱してしまいます。
ですからそういうことをしてはいけません。
<index>splice</index>
</para>
<para>
<code>foreach</code> probably won't do what you expect if VAR is a tied or other
special variable.   Don't do that either.
</para>
<para>
VAR が tie されていたりあるいは他の特殊変数であった場合には
<code>foreach</code> はあなたのもくろみどおりには動かないでしょう。
こういうこともしてはいけません。
</para>
<para>
Examples:
</para>
<para>
例:
</para>
<verbatim><![CDATA[
for (@ary) { s/foo/bar/ }
]]></verbatim>
<verbatim><![CDATA[
for my $elem (@elements) {
	$elem *= 2;
}
]]></verbatim>
<verbatim><![CDATA[
for $count (10,9,8,7,6,5,4,3,2,1,'BOOM') {
	print $count, "\n"; sleep(1);
}
]]></verbatim>
<verbatim><![CDATA[
for (1..15) { print "Merry Christmas\n"; }
]]></verbatim>
<verbatim><![CDATA[
foreach $item (split(/:[\\\n:]*/, $ENV{TERMCAP})) {
	print "Item: $item\n";
}
]]></verbatim>
<para>
Here's how a C programmer might code up a particular algorithm in Perl:
</para>
<para>
以下の例は、C プログラマーが Perl でとあるアルゴリズムを記述するときに
使うであろうやり方です:
</para>
<verbatim><![CDATA[
for (my $i = 0; $i < @ary1; $i++) {
	for (my $j = 0; $j < @ary2; $j++) {
	    if ($ary1[$i] > $ary2[$j]) {
		last; # can't go to outer :-(
	    }
	    $ary1[$i] += $ary2[$j];
	}
	# this is where that last takes me
}
]]></verbatim>
<para>
Whereas here's how a Perl programmer more comfortable with the idiom might
do it:
</para>
<para>
それに対して、次の例は Perl プログラマーが同じことをよりゆったりとして
行うやり方です:
</para>
<verbatim><![CDATA[
OUTER: for my $wid (@ary1) {
INNER:   for my $jet (@ary2) {
		next OUTER if $wid > $jet;
		$wid += $jet;
	     }
	  }
]]></verbatim>
<para>
See how much easier this is?  It's cleaner, safer, and faster.  It's
cleaner because it's less noisy.  It's safer because if code gets added
between the inner and outer loops later on, the new code won't be
accidentally executed.  The <code>next</code> explicitly iterates the other loop
rather than merely terminating the inner one.  And it's faster because
Perl executes a <code>foreach</code> statement more rapidly than it would the
equivalent <code>for</code> loop.
</para>
<para>
どのくらいこれが簡単になったように見えますか? これは明確で、安全で、
高速です。
これは余計なものが少ないので明確なのです。
これは後で内側のループと外側のループとの間にコードを付加えた場合でも、
それを間違って実行することがないので安全なのです。
<code>next</code> は内側のループを終了するのではなく、外側のループの繰り返しを
行います。
そしてこれは、Perl は <code>foreach</code> 文をそれと等価な <code>for</code> ループよりも
すばやく実行するので高速なのです。
</para>
</sect2>
<sect2>
<title>Basic BLOCKs and Switch Statements
<index>switch</index> <index>block</index> <index>case</index></title>
<para>
(基本ブロックと switch 文)
</para>
<para>
A BLOCK by itself (labeled or not) is semantically equivalent to a
loop that executes once.  Thus you can use any of the loop control
statements in it to leave or restart the block.  (Note that this is
<emphasis>NOT</emphasis> true in <code>eval{}</code>, <code>sub{}</code>, or contrary to popular belief
<code>do{}</code> blocks, which do <emphasis>NOT</emphasis> count as loops.)  The <code>continue</code>
block is optional.
</para>
<para>
ブロックそれ自身は(ラベルが付いていようがついてなかろうが)一度だけ
実行されるループと、文法的には等価なものです。
このため、ブロックから脱出するためやブロックの再スタートのために
任意のループ制御文を使うことができます(これは <code>eval{}</code>、<code>sub{}</code>、
さらに一般的な認識とは異なり <emphasis>ループではない</emphasis> <code>do{}</code> ブロックに対しては
<emphasis>真ではない</emphasis> ということに注意してください)。
<code>continue</code> ブロックは省略することができます。
</para>
<para>
The BLOCK construct is particularly nice for doing case
structures.
</para>
<para>
ブロック構造は case 構造を行うのに都合が良いです。
</para>
<verbatim><![CDATA[
SWITCH: {
	if (/^abc/) { $abc = 1; last SWITCH; }
	if (/^def/) { $def = 1; last SWITCH; }
	if (/^xyz/) { $xyz = 1; last SWITCH; }
	$nothing = 1;
}
]]></verbatim>
<para>
There is no official <code>switch</code> statement in Perl, because there are
already several ways to write the equivalent.
</para>
<para>
Perl には正式な <code>switch</code> 文というものはありませんが、それは
すでに等価なものを記述する方法が幾つかあるからです。
</para>
<para>
However, starting from Perl 5.8 to get switch and case one can use
the Switch extension and say:
</para>
<para>
しかし、Perl 5.8 から、Switch 拡張を使うことで、switch-case を使えます:
</para>
<verbatim><![CDATA[
use Switch;
]]></verbatim>
<para>
after which one has switch and case.  It is not as fast as it could be
because it's not really part of the language (it's done using source
filters) but it is available, and it's very flexible.
</para>
<para>
とした後で switch と case が使えます。
本当に言語の一部と言うわけではない(これはソースフィルタを使って
実現されています)ので、それほど早くはないですが、ともかくこれは利用可能で、
とても柔軟です。
</para>
<para>
In addition to the above BLOCK construct, you could write
</para>
<para>
先のブロック構造の例に加え、次のように書くこともできます。
</para>
<verbatim><![CDATA[
SWITCH: {
	$abc = 1, last SWITCH  if /^abc/;
	$def = 1, last SWITCH  if /^def/;
	$xyz = 1, last SWITCH  if /^xyz/;
	$nothing = 1;
}
]]></verbatim>
<para>
(That's actually not as strange as it looks once you realize that you can
use loop control &quot;operators&quot; within an expression.  That's just the binary
comma operator in scalar context.  See <link xref='perlop#Comma_Operator'>perlop/&quot;Comma Operator&quot;</link>.)
</para>
<para>
(これは、式の中にあるループを制御できる“演算子”のように見えますが、
実際にはそれほど奇異なものではありません。
単に通常のカンマ演算子です。
<link xref='perlop#Comma_Operator'>perlop/&quot;Comma Operator&quot;</link> を参照してください。)
</para>
<para>
or
</para>
<para>
あるいは:
</para>
<verbatim><![CDATA[
SWITCH: {
	/^abc/ && do { $abc = 1; last SWITCH; };
	/^def/ && do { $def = 1; last SWITCH; };
	/^xyz/ && do { $xyz = 1; last SWITCH; };
	$nothing = 1;
}
]]></verbatim>
<para>
or formatted so it stands out more as a &quot;proper&quot; <code>switch</code> statement:
</para>
<para>
書式付けしてより「適切な」<code>switch</code> 文のようにします:
</para>
<verbatim><![CDATA[
SWITCH: {
	/^abc/ 	    && do {
			    $abc = 1;
			    last SWITCH;
		       };
]]></verbatim>
<verbatim><![CDATA[
/^def/ 	    && do {
		    $def = 1;
		    last SWITCH;
	       };
]]></verbatim>
<verbatim><![CDATA[
/^xyz/ 	    && do {
		    $xyz = 1;
		    last SWITCH;
	        };
$nothing = 1;
    }
]]></verbatim>
<para>
or
</para>
<para>
あるいは:
</para>
<verbatim><![CDATA[
SWITCH: {
	/^abc/ and $abc = 1, last SWITCH;
	/^def/ and $def = 1, last SWITCH;
	/^xyz/ and $xyz = 1, last SWITCH;
	$nothing = 1;
}
]]></verbatim>
<para>
or even, horrors,
</para>
<para>
さらにはこういった書き方もあります。
</para>
<verbatim><![CDATA[
if (/^abc/)
	{ $abc = 1 }
elsif (/^def/)
	{ $def = 1 }
elsif (/^xyz/)
	{ $xyz = 1 }
else
	{ $nothing = 1 }
]]></verbatim>
<para>
A common idiom for a <code>switch</code> statement is to use <code>foreach</code>'s aliasing to make
a temporary assignment to <code>$_</code> for convenient matching:
</para>
<para>
<code>switch</code> 文のための一般的なイディオムとは、マッチングに便利な <code>$_</code> に 
一時的に代入させるために <code>foreach</code> のエイリアス化を使うというものです:
</para>
<verbatim><![CDATA[
SWITCH: for ($where) {
		/In Card Names/     && do { push @flags, '-e'; last; };
		/Anywhere/          && do { push @flags, '-h'; last; };
		/In Rulings/        && do {                    last; };
		die "unknown value for form variable where: `$where'";
	    }
]]></verbatim>
<para>
Another interesting approach to a switch statement is arrange
for a <code>do</code> block to return the proper value:
</para>
<para>
switch 文のためのもう一つの興味深いアプローチは、<code>do</code> ブロックを
適切な値を返すようにアレンジするというものです。
</para>
<verbatim><![CDATA[
$amode = do {
	if     ($flag & O_RDONLY) { "r" }	# XXX: isn't this 0?
	elsif  ($flag & O_WRONLY) { ($flag & O_APPEND) ? "a" : "w" }
	elsif  ($flag & O_RDWR)   {
	    if ($flag & O_CREAT)  { "w+" }
	    else                  { ($flag & O_APPEND) ? "a+" : "r+" }
	}
};
]]></verbatim>
<para>
Or
</para>
<para>
または:
</para>
<verbatim><![CDATA[
print do {
    ($flags & O_WRONLY) ? "write-only"          :
    ($flags & O_RDWR)   ? "read-write"          :
                          "read-only";
};
]]></verbatim>
<para>
Or if you are certain that all the <code>&amp;&amp;</code> clauses are true, you can use
something like this, which &quot;switches&quot; on the value of the
<code>HTTP_USER_AGENT</code> environment variable.
</para>
<para>
またはあなたが全ての <code>&amp;&amp;</code> 節が真であることに確信を持っているのなら、
以下のように <code>HTTP_USER_AGENT</code> 環境変数の値に応じて &quot;switch&quot; できます。
</para>
<verbatim><![CDATA[
#!/usr/bin/perl 
# pick out jargon file page based on browser
$dir = 'http://www.wins.uva.nl/~mes/jargon';
for ($ENV{HTTP_USER_AGENT}) { 
	$page  =    /Mac/            && 'm/Macintrash.html'
		 || /Win(dows )?NT/  && 'e/evilandrude.html'
		 || /Win|MSIE|WebTV/ && 'm/MicroslothWindows.html'
		 || /Linux/          && 'l/Linux.html'
		 || /HP-UX/          && 'h/HP-SUX.html'
		 || /SunOS/          && 's/ScumOS.html'
		 ||                     'a/AppendixB.html';
}
print "Location: $dir/$page\015\012\015\012";
]]></verbatim>
<para>
That kind of switch statement only works when you know the <code>&amp;&amp;</code> clauses
will be true.  If you don't, the previous <code>?:</code> example should be used.
</para>
<para>
こういった switch 文は、<code>&amp;&amp;</code> 節が真になるということを分かっているときにのみ
うまくいきます。
そうでなければ、<code>?:</code> を使いましょう。
</para>
<para>
You might also consider writing a hash of subroutine references
instead of synthesizing a <code>switch</code> statement.
</para>
<para>
あるいは <code>switch</code> 文の代わりにサブルーチンのリファレンスのハッシュを
書くことを考えるかもしれません。
</para>
</sect2>
<sect2>
<title>Goto
<index>goto</index></title>
<para>
(goto 文)
</para>
<para>
Although not for the faint of heart, Perl does support a <code>goto</code>
statement.  There are three forms: <code>goto</code>-LABEL, <code>goto</code>-EXPR, and
<code>goto</code>-&amp;NAME.  A loop's LABEL is not actually a valid target for
a <code>goto</code>; it's just the name of the loop.
</para>
<para>
気弱な人のためでないにも関らず、Perl は <code>goto</code> 文をサポートしています。
<code>goto</code>-LABEL、<code>goto</code>-EXPR、<code>goto</code>-&amp;NAME の三つの形式があります。
ループのラベルは実際には <code>goto</code> の正当なターゲットではなく、
ループの名前にすぎません。
</para>
<para>
The <code>goto</code>-LABEL form finds the statement labeled with LABEL and resumes
execution there.  It may not be used to go into any construct that
requires initialization, such as a subroutine or a <code>foreach</code> loop.  It
also can't be used to go into a construct that is optimized away.  It
can be used to go almost anywhere else within the dynamic scope,
including out of subroutines, but it's usually better to use some other
construct such as <code>last</code> or <code>die</code>.  The author of Perl has never felt the
need to use this form of <code>goto</code> (in Perl, that is--C is another matter).
</para>
<para>
<code>goto</code>-LABEL 形式は LABEL でラベル付けされた文を見つけだし、そこから
実行を再開します。
これはサブルーチンであるとか <code>foreach</code> ループのような
初期化を必要とするような構造へ飛び込むために使うことはできません。
また、最適化されて無くなってしまうような構造へ飛び込むこともできません。
動的スコープの中以外のほとんどの場所へは、サブルーチンの
外も含めて移動することができます。
しかし、通常は <code>last</code> や <code>die</code> のような別のやり方を使ったほうが
良いでしょう。
Perl の作者は、未だかつてこの形式の <code>goto</code> を使うことが
必要だと感じたことはありません(Perl の場合です。C の場合はまた別の話です)。
</para>
<para>
The <code>goto</code>-EXPR form expects a label name, whose scope will be resolved
dynamically.  This allows for computed <code>goto</code>s per FORTRAN, but isn't
necessarily recommended if you're optimizing for maintainability:
</para>
<para>
<code>goto</code>-EXPR 形式は動的に解決されるスコープを持っているラベル名を
期待しています。
これによって FORTRAN の計算型 <code>goto</code> が実現できますが、
これは保守性に重きを置くのであれば使うことは止めた方が良いでしょう。
</para>
<verbatim><![CDATA[
goto(("FOO", "BAR", "GLARCH")[$i]);
]]></verbatim>
<para>
The <code>goto</code>-&amp;NAME form is highly magical, and substitutes a call to the
named subroutine for the currently running subroutine.  This is used by
<code>AUTOLOAD()</code> subroutines that wish to load another subroutine and then
pretend that the other subroutine had been called in the first place
(except that any modifications to <code>@_</code> in the current subroutine are
propagated to the other subroutine.)  After the <code>goto</code>, not even <code>caller()</code>
will be able to tell that this routine was called first.
</para>
<para>
<code>goto</code>-&amp;NAME は高度にマジカルで、名前付きサブルーチンの呼び出しを
カレントで実行されているサブルーチンに置き換えます。
これは別のサブルーチンをロードして、最初の場所で呼び出された
別のサブルーチンを要求することをしようとする
<code>AUTOLOAD()</code> サブルーチンで使われていてます
(カレントのサブルーチンにおける <code>@_</code> に対するもの以外の変更は、
別のサブルーチンへ伝播します)。
<code>goto</code> の後で、<code>caller()</code> でなくてもこのサブルーチンが
最初に呼ばれたのだということを伝えることすらできるでしょう。
</para>
<para>
In almost all cases like this, it's usually a far, far better idea to use the
structured control flow mechanisms of <code>next</code>, <code>last</code>, or <code>redo</code> instead of
resorting to a <code>goto</code>.  For certain applications, the catch and throw pair of
<code>eval{}</code> and die() for exception processing can also be a prudent approach.
</para>
<para>
このようなケースのほとんどすべての場合、<code>goto</code> に頼るのではなくて
<code>next</code>、<code>last</code>、<code>redo</code>といった制御フロー機構を使うことが、
ずっとずっと良いアイデアでしょう。
一部のアプリケーションに対しては、<code>eval{}</code> と die() を
catch と throw のペアとして例外処理を行うための賢明なアプローチとして
使うことができるでしょう。
</para>
</sect2>
<sect2>
<title>PODs: Embedded Documentation
<index>POD</index> <index>documentation</index></title>
<para>
(POD: 組み込みドキュメント)
</para>
<para>
Perl has a mechanism for intermixing documentation with source code.
While it's expecting the beginning of a new statement, if the compiler
encounters a line that begins with an equal sign and a word, like this
</para>
<para>
Perl は、ソースコードとドキュメントとを混ぜ書きするための仕掛けを
持っています。
新しい文の始まりが期待されているときに、コンパイラは
以下の例のような = 記号で始まっている語を見つけると:
</para>
<verbatim><![CDATA[
=head1 Here There Be Pods!
]]></verbatim>
<para>
Then that text and all remaining text up through and including a line
beginning with <code>=cut</code> will be ignored.  The format of the intervening
text is described in <link xref='perlpod'>perlpod</link>.
</para>
<para>
そのテキストと、<code>=cut</code> で始まる行までの内容を無視します。
間に入るテキストの書式は <link xref='perlpod'>perlpod</link> で説明されています。
</para>
<para>
This allows you to intermix your source code
and your documentation text freely, as in
</para>
<para>
これによって、ソースコードとドキュメントとを以下に示す例のように
自由に混ぜることができるようになります。
</para>
<verbatim><![CDATA[
=item snazzle($)
]]></verbatim>
<verbatim><![CDATA[
The snazzle() function will behave in the most spectacular
form that you can possibly imagine, not even excepting
cybernetic pyrotechnics.
]]></verbatim>
<verbatim><![CDATA[
=cut back to the compiler, nuff of this pod stuff!
]]></verbatim>
<verbatim><![CDATA[
sub snazzle($) {
	my $thingie = shift;
	.........
}
]]></verbatim>
<para>
Note that pod translators should look at only paragraphs beginning
with a pod directive (it makes parsing easier), whereas the compiler
actually knows to look for pod escapes even in the middle of a
paragraph.  This means that the following secret stuff will be
ignored by both the compiler and the translators.
</para>
<para>
コンパイラはパラグラフの途中に pod エスケープがあったとしてもそれを
認識できるのに、pod トランスレータは pod 指示子で始まっている
パラグラフのみに注目すべき(これは構文解析を簡単にするためです)で
あるということに注意して下さい。
つまり、以下の例にある &quot;secret stuff&quot; はコンパイラからも、
トランスレータからも無視されるということです。
</para>
<verbatim><![CDATA[
$a=3;
=secret stuff
 warn "Neither POD nor CODE!?"
=cut back
print "got $a\n";
]]></verbatim>
<para>
You probably shouldn't rely upon the <code>warn()</code> being podded out forever.
Not all pod translators are well-behaved in this regard, and perhaps
the compiler will become pickier.
</para>
<para>
この例の <code>warn()</code> のようなものが、将来に渡って無視されるということに
依存すべきではありません。
すべての pod トランスレータがそのように振る舞うわけではありませんし、
コンパイラは将来これを無視しないようになるかもしれません。
</para>
<para>
One may also use pod directives to quickly comment out a section
of code.
</para>
<para>
pod 指示子を、コードの一部を手っ取り早くコメントアウトするために
使うこともできます。
</para>
</sect2>
<sect2>
<title>Plain Old Comments (Not!)
<index>comment</index> <index>line</index> <index>#</index> <index>preprocessor</index> <index>eval</index></title>
<para>
Perl can process line directives, much like the C preprocessor.  Using
this, one can control Perl's idea of filenames and line numbers in
error or warning messages (especially for strings that are processed
with <code>eval()</code>).  The syntax for this mechanism is the same as for most
C preprocessors: it matches the regular expression
</para>
<para>
C のプリプロセッサと同じように、Perl は行指示子を処理できます。
これを使うことによって、エラーメッセージや警告メッセージにある
ファイル名や行番号を制御することができます
(特に、<code>eval()</code> で処理される文字列のために)。
この仕組みの構文は C のプリプロセッサとほとんど同じで、正規表現:
</para>
<verbatim><![CDATA[
# example: '# line 42 "new_filename.plx"'
/^\#   \s*
  line \s+ (\d+)   \s*
  (?:\s("?)([^"]+)\2)? \s*
 $/x
]]></verbatim>
<para>
with <code>$1</code> being the line number for the next line, and <code>$3</code> being
the optional filename (specified with or without quotes).
</para>
<para>
にマッチしたものの <code>$1</code> が次の行の行番号となり、省略することもできる
<code>$3</code> は(クォートありかなしで指定された)ファイル名となります。
</para>
<para>
There is a fairly obvious gotcha included with the line directive:
Debuggers and profilers will only show the last source line to appear
at a particular line number in a given file.  Care should be taken not
to cause line number collisions in code you'd like to debug later.
</para>
<para>
行指示子にはかなり明らかな技があります。
デバッガとプロファイラは、与えられたファイルの特定の行番号に対して現れた
最新のソース行のみを表示します。
あとでデバッグしたいコードでは行番号の衝突が起きないように注意するべきです。
</para>
<para>
Here are some examples that you should be able to type into your command
shell:
</para>
<para>
コマンドシェルでタイプすることのできる例をいくつか挙げます:
</para>
<verbatim><![CDATA[
% perl
# line 200 "bzzzt"
# the `#' on the previous line must be the first char on line
die 'foo';
__END__
foo at bzzzt line 201.
]]></verbatim>
<verbatim><![CDATA[
% perl
# line 200 "bzzzt"
eval qq[\n#line 2001 ""\ndie 'foo']; print $@;
__END__
foo at - line 2001.
]]></verbatim>
<verbatim><![CDATA[
% perl
eval qq[\n#line 200 "foo bar"\ndie 'foo']; print $@;
__END__
foo at foo bar line 200.
]]></verbatim>
<verbatim><![CDATA[
% perl
# line 345 "goop"
eval "\n#line " . __LINE__ . ' "' . __FILE__ ."\"\ndie 'foo'";
print $@;
__END__
foo at goop line 345.
]]></verbatim>
<para>
Created: KIMURA Koichi
Updated: Kentaro Shirakata &lt;argrath@ub32.org&gt;
</para>
</sect2>
</sect1>
</pod>
