argra****@users*****
argra****@users*****
2011年 5月 22日 (日) 00:55:03 JST
Index: docs/perl/5.10.1/perlintro.pod diff -u docs/perl/5.10.1/perlintro.pod:1.4 docs/perl/5.10.1/perlintro.pod:1.5 --- docs/perl/5.10.1/perlintro.pod:1.4 Sat May 7 04:15:22 2011 +++ docs/perl/5.10.1/perlintro.pod Sun May 22 00:55:03 2011 @@ -90,13 +90,11 @@ =end original この言語は、綺麗さ (小規模、エレガント、最少) ではなく、 実用性 -使い易さ、効率、完全性) を目指しています。 -Its major -features are that it's easy to use, supports both procedural and -object-oriented (OO) programming, has powerful built-in support for text -processing, and has one of the world's most impressive collections of -third-party modules. -(TBT) +(使い易さ、効率、完全性) を目指しています。 +主な機能は、簡単に使える、手続き的とオブジェクト指向 (OO) の両方で +プログラミングできる、テキスト処理のための強力な組み込み機能がある、 +世界でもっともめざましいサードパーティモジュールのコレクションがある、と +いったことです。 =begin original @@ -107,11 +105,10 @@ =end original -Perl の定義は L<perl> と L<perlfaq1> にあり、きっと他の場所にもあります。 -From this we can determine that Perl is different -things to different people, but that lots of people think it's at least -worth writing about. -(TBT) +Perl の異なった定義は L<perl> と L<perlfaq1> にあり、きっと他の +場所にもあります。 +ここから、Perl は色々な人によって色々な定義ができますが、多くの人々は +少なくとも書く価値があると考えています。 =head2 Running Perl programs @@ -203,13 +200,11 @@ 追加した 2 行は、コードにある様々な一般的な問題を捕らえるように perl に 要求します。 この 2 行は別のことをチェックするので、両方が必要です。 -A -potential problem caught by C<use strict;> will cause your code to stop -immediately when it is encountered, while C<use warnings;> will merely -give a warning (like the command-line switch B<-w>) and let your code run. -To read more about them check their respective manual pages at L<strict> -and L<warnings>. -(TBT) +C<use strict;> によって捕捉される潜在的な問題は、コードに発見されると +直ちに停止する一方、C<use warnings;> は (コマンドラインオプション +B<-w> と同様) 警告を出すだけで、コードは実行されます。 +これらに関するさらなる情報は、L<strict> と L<warnings> にあるそれぞれの +マニュアルページをチェックしてください。 =head2 Basic syntax overview @@ -246,8 +241,14 @@ コメントは # マークで始まり、行末まで続きます: +=begin original + # This is a comment +=end original + + # これはコメント + =begin original Whitespace is irrelevant: @@ -266,12 +267,20 @@ =end original +=begin original + …但しクォートされた文字列の中は例外です: # this would print with a linebreak in the middle print "Hello world"; +=end original + + # これは途中に改行が入る + print "Hello + world"; + =begin original Double quotes or single quotes may be used around literal strings: @@ -293,9 +302,16 @@ しかし、ダブルクォートのみが変数と改行(C<\n>)のような特殊文字を 「展開」します: +=begin original + print "Hello, $name\n"; # works fine print 'Hello, $name\n'; # prints $name\n literally +=end original + + print "Hello, $name\n"; # うまく動作する + print 'Hello, $name\n'; # $name\n という文字をそのまま表示する + =begin original Numbers don't need quotes around them: @@ -367,12 +383,11 @@ =end original -Scalar values can be strings, integers or floating point numbers, and Perl -will automatically convert between them as required. There is no need -to pre-declare your variable types, but you have to declare them using -the C<my> keyword the first time you use them. (This is one of the -requirements of C<use strict;>.) -(TBT) +スカラ値には文字列、整数、浮動小数点数があり、Perl は必要なときに自動的に +これらの値を相互に変換します。 +変数型を事前に宣言する必要はありませんが、変数を使うときには C<my> +キーワードを使って宣言する必要があります。 +(これは C<use strict;> が要求することの一つです。) =begin original @@ -397,17 +412,23 @@ =end original -There are a number of "magic" scalars with names that look like -punctuation or line noise. These special variables are used for all -kinds of purposes, and are documented in L<perlvar>. +句読点や回線ノイズのように見える名前を持った、多くの「マジック」変数が +あります。 +これらの特殊変数はあらゆる目的のために利用され、L<perlvar> に +文書化されています。 今のところ知っておくべきただ一つのことは、「デフォルト変数」である C<$_> です。 これは Perl の多くの関数でデフォルト引数として使われ、ある種のループ構造で 暗黙に設定されます。 -(TBT) + +=begin original print; # prints contents of $_ by default +=end original + + print; # デフォルトで $_ の内容を表示する + =item Arrays (配列) @@ -433,9 +454,16 @@ 配列は添え字 0 から始まります。 以下は配列の要素を取得する方法です: +=begin original + print $animals[0]; # prints "camel" print $animals[1]; # prints "llama" +=end original + + print $animals[0]; # "camel" を表示する + print $animals[1]; # "llama" を表示する + =begin original The special variable C<$#array> tells you the index of the last element @@ -445,8 +473,14 @@ 特殊変数 C<$#array> は、配列の最後の要素の添え字を返します: +=begin original + print $mixed[$#mixed]; # last element, prints 1.23 +=end original + + print $mixed[$#mixed]; # 最後の要素である 1.23 を表示する + =begin original You might be tempted to use C<$#array + 1> to tell you how many items there @@ -588,7 +622,7 @@ =end original -ハッシュには特に決まった順序はありませんが、キーをソートして +ハッシュには特に決まった順序はありませんが、キーをソートして、 それを使ってループできます。 =begin original @@ -704,11 +738,11 @@ しかし、上述の使用法はプログラム全体でグローバルな変数を作ることになり、 これは悪いプログラミング手法です。 -C<my> creates lexically -scoped variables instead. The variables are scoped to the block -(i.e. a bunch of statements surrounded by curly-braces) in which they -are defined. -(TBT) +代わりに、C<my> はレキシカルなスコープを持った変数を作ります。 +変数は、宣言されたブロック (中かっこで囲まれた文のかたまり) 内の +スコープを持ちます。 + +=begin original my $x = "foo"; my $some_condition = 1; @@ -720,6 +754,18 @@ print $x; # prints "foo" print $y; # prints nothing; $y has fallen out of scope +=end original + + my $x = "foo"; + my $some_condition = 1; + if ($some_condition) { + my $y = "bar"; + print $x; # "foo" を表示する + print $y; # "bar" を表示する + } + print $x; # "foo" を表示する + print $y; # 何も表示しない; $y はスコープ外 + =begin original Using C<my> in combination with a C<use strict;> at the top of @@ -730,12 +776,12 @@ =end original -Using C<my> in combination with a C<use strict;> at the top of -your Perl scripts means that the interpreter will pick up certain common -programming errors. For instance, in the example above, the final -C<print $y> would cause a compile-time error and prevent you from -running the program. Using C<strict> is highly recommended. -(TBT) +Perl スクリプトの先頭に C<use strict;> を書くことと合わせて C<my> を +使うことによって、インタプリタがある種のよくあるプログラミングミスを +検出できます。 +例えば、上述の例で、最後の C<print $y> はコンパイルエラーとなり、 +プログラムの実行を妨げます。 +C<strict> を使うことを強く勧めます。 =head2 Conditional and looping constructs @@ -750,11 +796,10 @@ =end original -Perl has most of the usual conditional and looping constructs except for -case/switch (but if you really want it, there is a Switch module in Perl -5.8 and newer, and on CPAN. See the section on modules, below, for more -information about modules and CPAN). -(TBT) +Perl には、case/switch を除く、一般的な条件構文とループ構文のほとんど +全てがあります (しかし、もし本当に必要なら、 Perl 5.8 以降と CPAN に +Switch モジュールがあります。モジュールと CPAN に関するさらなる +情報については後述するモジュールの説を参照してください)。 =begin original @@ -765,10 +810,8 @@ =end original 任意の Perl 式は条件となります。 -See the list of operators in -the next section for information on comparison and boolean logic operators, -which are commonly used in conditional statements. -(TBT) +条件文でよく使われる、比較演算子と真偽値論理演算子に関する情報については、 +次の説にある演算子の一覧を参照してください。 =over 4 @@ -812,17 +855,34 @@ たとえブロック中に 1 行しかなくても、Perl では中かっこが必要であることに 注意してください。 -しかし、1 行の条件ブロックをより英語風にする賢明な方法があります: +しかし、1 行の条件ブロックをより英語風にする気の利いた方法があります: + +=begin original # the traditional way if ($zippy) { print "Yow!"; } +=end original + + # 伝統的な方法 + if ($zippy) { + print "Yow!"; + } + +=begin original + # the Perlish post-condition way print "Yow!" if $zippy; print "We have no bananas" unless $bananas; +=end original + + # Perl っぽい後置条件 + print "Yow!" if $zippy; + print "We have no bananas" unless $bananas; + =item while while ( condition ) { @@ -898,7 +958,7 @@ =end original - # デフォルトの $_ を使う必要はありません… + # デフォルトの $_ を使う必要はない… foreach my $key (keys %hash) { print "The value of $key is $hash{$key}\n"; } @@ -941,7 +1001,7 @@ =end original Perl の演算子は L<perlop> に完全に文書化されていますが、もっとも -一般的な者をいくつか以下に示します: +一般的なものをいくつか以下に示します: =over 4 @@ -1023,6 +1083,8 @@ =item Boolean logic +(真偽値論理) + && and || or ! not @@ -1037,12 +1099,11 @@ =end original -(C<and>, C<or> and C<not> aren't just in the above table as descriptions -of the operators -- they're also supported as operators in their own -right. They're more readable than the C-style operators, but have -different precedence to C<&&> and friends. Check L<perlop> for more -detail.) -(TBT) +(C<and>, C<or>, C<not> は演算子の記述としては上述の表にはありません -- +これらも本来の演算子として対応しています。 +これらは C 形式の演算子より読みやすいですが、C<&&> およびその仲間とは +異なった優先順位を持ちます。 +さらなる詳細については L<perlop> を参照してください。) =item Miscellaneous @@ -1142,10 +1203,18 @@ C<< <> >> 演算子は、C<while> ループの中でもっともよく見られます: +=begin original + while (<$in>) { # assigns each line in turn to $_ print "Just read in this line: $_"; } +=end original + + while (<$in>) { # それぞれの行を $_ に代入する + print "Just read in this line: $_"; + } + =begin original We've already seen how to print to standard output using C<print()>. @@ -1214,13 +1283,15 @@ =end original -The C<//> matching operator is documented in L<perlop>. It operates on -C<$_> by default, or can be bound to another variable using the C<=~> -binding operator (also documented in L<perlop>). -(TBT) +C<//> マッチング演算子は L<perlop> に文書化されています。 +これはデフォルトでは C<$_> を操作しますし、C<=~> 結合演算子 +(これも L<perlop> で文書化されています) を使って他の変数に対して +適用することもできます。 =item Simple substitution +(単純な置換) + =begin original s/foo/bar/; # replaces foo with bar in $_ @@ -1254,11 +1325,11 @@ =end original -You don't just have to match on fixed strings. In fact, you can match -on just about anything you could dream of by using more complex regular -expressions. These are documented at great length in L<perlre>, but for -the meantime, here's a quick cheat sheet: -(TBT) +単に固定した文字列にマッチングさせる必要はありません。 +実際の所、より複雑な正規表現を使うことによって、思い付く限りほとんど +全てのものとマッチングさせることが出来ます。 +これらは L<perlre> に長文で文書化されていますが、今のところは、以下に +カンニングペーパーを示します: =begin original @@ -1305,11 +1376,9 @@ =end original -Quantifiers can be used to specify how many of the previous thing you -want to match on, where "thing" means either a literal character, one -of the metacharacters listed above, or a group of characters or -metacharacters in parentheses. -(TBT) +量指定子は直前に指定したマッチングさせたいものを何回マッチングさせたいかを +指定するために使います; ここで「もの」というのはリテラルな 1 文字、 +上述したメタ文字の一つ、かっこに囲まれた文字やメタ文字のグループです。 =begin original @@ -1387,8 +1456,14 @@ 正規表現マッチングの一部の結果を後で使うために捕捉するために使えます。 結果は C<$1>, C<$2> などに入ります。 +=begin original + # a cheap and nasty way to break an email address up into parts +=end original + + # e メールアドレスをパーツに分解するちゃちで扱いにくい方法 + if ($email =~ /([^@]+)@(.+)/) { print "Username is $1\n"; print "Hostname is $2\n"; @@ -1450,12 +1525,12 @@ =end original -What's that C<shift>? Well, the arguments to a subroutine are available -to us as a special array called C<@_> (see L<perlvar> for more on that). -The default argument to the C<shift> function just happens to be C<@_>. -So C<my $logmessage = shift;> shifts the first item off the list of -arguments and assigns it to C<$logmessage>. -(TBT) +C<shift> とはなんでしょう? +ええっと、サブルーチンの引数は C<@_> と呼ばれる特殊配列として利用可能に +なります (これに関する詳細は L<perlvar> を参照してください)。 +C<shift> 関数のデフォルト引数は たまたま C<@_> です。 +それで、C<my $logmessage = shift;> は引数のリストの最初のものを取り出して、 +C<$logmessage> に代入します。 =begin original @@ -1465,9 +1540,16 @@ その他の方法でも C<@_> を操作できます: +=begin original + my ($logmessage, $priority) = @_; # common my $logmessage = $_[0]; # uncommon, and ugly +=end original + + my ($logmessage, $priority) = @_; # 一般的 + my $logmessage = $_[0]; # 一般的でもなく、美しくもない + =begin original Subroutines can also return values: