ktats****@users*****
ktats****@users*****
2012年 9月 1日 (土) 01:59:01 JST
Index: docs/articles/qntm.org/files/perl/perl.html diff -u docs/articles/qntm.org/files/perl/perl.html:1.5 docs/articles/qntm.org/files/perl/perl.html:1.6 --- docs/articles/qntm.org/files/perl/perl.html:1.5 Fri Aug 31 01:35:20 2012 +++ docs/articles/qntm.org/files/perl/perl.html Sat Sep 1 01:58:59 2012 @@ -1015,7 +1015,9 @@ <h3 class=original>In-place array modification</h3> <h3>ãã®å ´ã§é åãå¤æ´ãã</h3> -<p>We'll use <tt>@stack</tt> to demonstrate these:</p> +<p class="original">We'll use <tt>@stack</tt> to demonstrate these:</p> + +<p><tt>@stack</tt>ã使ã£ã¦ãã¢ãã¾ã:</p> <pre class="perl prettyprint"> my @stack = ("Fred", "Eileen", "Denise", "Charlie"); @@ -1424,8 +1426,8 @@ export PERL5LIB=/foo/bar/baz:$PERL5LIB </pre> -<p>Once the Perl module is created and <tt>perl</tt> knows where to look for it, you can use the <tt>require</tt> built-in function to search for and execute it during a Perl script. For example, calling <tt>require Mathematics::Powers</tt> causes the Perl interpreter to search each directory listed in <tt>PERL5LIB</tt> in turn, looking for a file called <tt>Mathematics/Powers.pm</tt>. After the module has been loaded, the subroutines and variables that were defined there suddenly become available in the main script. Our example script might be called <tt>powers.pl</tt> and read as follows:</p> -<p class=original>Perlã¢ã¸ã¥ã¼ã«ãä½ããã¦ã<tt>perl</tt>ããããã©ãã«ããããç¥ã£ã¦ããã°ãçµè¾¼ã®<tt>require</tt>é¢æ°ã使ã£ã¦æ¢ããPerlã®ã¹ã¯ãªããä¸ã§å®è¡ãããã¨ãã§ãã¾ããä¾ãã°ã<tt>require Mathematics::Powers</tt>ãå¼ã¶ã¨ãPerlã¤ã³ã¿ã¼ããªã¿ã¯<tt>PERL5LIB</tt>ã«ãªã¹ãããã¦ãããã£ã¬ã¯ããªãé çªã«ã<tt>Mathematics/Powers.pm</tt>ã¨ãããã¡ã¤ã«ãæ¢ãã¾ããã¢ã¸ã¥ã¼ã«ããã¼ããããããçªç¶ã«ãµãã«ã¼ãã³ã¨å¤æ°ãã¡ã¤ã³ã¹ã¯ãªããã«å®ç¾©ããã¾ãããã®ä¾ã®ã¹ã¯ãªããã<tt>powers.pl</tt>ã¨å¼ã³ã¾ããããç¶ãã¦èªãã§ããã:</p> +<p class=original>Once the Perl module is created and <tt>perl</tt> knows where to look for it, you can use the <tt>require</tt> built-in function to search for and execute it during a Perl script. For example, calling <tt>require Mathematics::Powers</tt> causes the Perl interpreter to search each directory listed in <tt>PERL5LIB</tt> in turn, looking for a file called <tt>Mathematics/Powers.pm</tt>. After the module has been loaded, the subroutines and variables that were defined there suddenly become available in the main script. Our example script might be called <tt>powers.pl</tt> and read as follows:</p> +<p>Perlã¢ã¸ã¥ã¼ã«ãä½ããã¦ã<tt>perl</tt>ããããã©ãã«ããããç¥ã£ã¦ããã°ãçµè¾¼ã®<tt>require</tt>é¢æ°ã使ã£ã¦æ¢ããPerlã®ã¹ã¯ãªããä¸ã§å®è¡ãããã¨ãã§ãã¾ããä¾ãã°ã<tt>require Mathematics::Powers</tt>ãå¼ã¶ã¨ãPerlã¤ã³ã¿ã¼ããªã¿ã¯<tt>PERL5LIB</tt>ã«ãªã¹ãããã¦ãããã£ã¬ã¯ããªãé çªã«ã<tt>Mathematics/Powers.pm</tt>ã¨ãããã¡ã¤ã«ãæ¢ãã¾ããã¢ã¸ã¥ã¼ã«ããã¼ããããããçªç¶ã«ãµãã«ã¼ãã³ã¨å¤æ°ãã¡ã¤ã³ã¹ã¯ãªããã«å®ç¾©ããã¾ãããã®ä¾ã®ã¹ã¯ãªããã<tt>powers.pl</tt>ã¨å¼ã³ã¾ããããç¶ãã¦èªãã§ããã:</p> <pre class="perl prettyprint"> use strict; @@ -1439,27 +1441,43 @@ <p class=original>Now read this next bit carefully.</p> <p>ã§ã¯ã次ã¯ã¡ãã£ã¨æ³¨æãã¦èªãã§ãã ããã</p> <p class=original>Packages and modules are two completely separate and distinct features of the Perl programming language. The fact that they both use the same double colon delimiter is a monumental red herring. It is possible to switch packages multiple times over the course of a script or module, and it is possible to use the same package declaration in multiple locations in multiple files. Calling <tt>require Foo::Bar</tt> <em>does not</em> look for and load a file with a <tt>package Foo::Bar</tt> declaration somewhere inside it. Calling <tt>require Foo::Bar</tt> <em>does not</em> necessarily load subroutines or package variables in the <tt>Foo::Bar</tt> namespace. Calling <tt>require Foo::Bar</tt> merely loads a file called <tt>Foo/Bar.pm</tt>, which need not have <em>any</em> kind of package declaration inside it at all, and in fact might declare <tt>package Baz::Qux</tt> and other nonsense inside it for all you know.</p> -<p>ããã±ã¼ã¸ã¨ã¢ã¸ã¥ã¼ã«ã®2ã¤ã¯ãããã°ã©ãã³ã°è¨èªPerlã«ããã¦å®å ¨ã«åããã¦ãããåºå¥ãããæ©è½ã§ãããã®2ã¤ãåãããã«ã³ãã³ã®ããªãã¿ã使ã£ã¦ãããã¨ã¯ãè¨å¿µç¢çãªèµ¤ãã·ã³ã§ããããã¯ãã¹ã¯ãªããã¾ãã¯ã¢ã¸ã¥ã¼ã«ã®ã³ã¼ã¹çµç±ã§ããã±ã¼ã¸ãè¤æ°ååãæ¿ãããã¨ãã§ããã¾ããè¤æ°ã®ãã¡ã¤ã«å ã®è¤æ°ã®å ´æã§åä¸ã®ããã±ã¼ã¸å®£è¨ã使ããã¨ãã§ãã¾ãã<tt>require Foo::Bar</tt>ãå¼ã¶ãã¨ã¯<tt>Foo::Bar</tt>åå空éã«ãããµãã«ã¼ãã³ãããã±ã¼ã¸å¤æ°ãå¿ ããããã¼ã<em>ãã¾ãã</em>ã<tt>require Foo::Bar</tt>ãå¼ã¶ãã¨ã¯ãåã«<tt>Foo/Bar.pm</tt>ã¨ãããã¡ã¤ã«ããã¼ãããã ãã§ããããã®ãã¡ã¤ã«ã«ã<em>ã©ã®ãããª</em>種é¡ã®ããã±ã¼ã¸å®£è¨ãå¿ è¦æãã¾ãããå®éã«ã¯ãããªãã®ç¥ã£ã¦ããå ¨ã¦ã®<tt>package Baz ::Qux</tt>ã®ãããªæå³ã®ãªããã®ã宣è¨ã§ãã¾ãã</p> +<p>ããã±ã¼ã¸ã¨ã¢ã¸ã¥ã¼ã«ã®2ã¤ã¯ãããã°ã©ãã³ã°è¨èªPerlã«ããã¦å®å ¨ã«åããã¦ãããåºå¥ãããæ©è½ã§ãããã®2ã¤ãåãããã«ã³ãã³ã®ããªãã¿ã使ã£ã¦ãããã¨ã¯ãéè¦ãªã²ã£ããã§ããããã¯ãã¹ã¯ãªããã¾ãã¯ã¢ã¸ã¥ã¼ã«ã®ã³ã¼ã¹çµç±ã§ããã±ã¼ã¸ãè¤æ°ååãæ¿ãããã¨ãã§ããã¾ããè¤æ°ã®ãã¡ã¤ã«å ã®è¤æ°ã®å ´æã§åä¸ã®ããã±ã¼ã¸å®£è¨ã使ããã¨ãã§ãã¾ãã<tt>require Foo::Bar</tt>ãå¼ã¶ãã¨ã¯<tt>Foo::Bar</tt>åå空éã«ãããµãã«ã¼ãã³ãããã±ã¼ã¸å¤æ°ãå¿ ããããã¼ã<em>ãã¾ãã</em>ã<tt>require Foo::Bar</tt>ãå¼ã¶ãã¨ã¯ãåã«<tt>Foo/Bar.pm</tt>ã¨ãããã¡ã¤ã«ããã¼ãããã ãã§ããããã®ãã¡ã¤ã«ã«ã<em>ã©ã®ãããª</em>種é¡ã®ããã±ã¼ã¸å®£è¨ãå¿ è¦æãã¾ãããå®éã«ã¯ã<tt>package Baz::Qux</tt>ã®ãããªæå³ã®ãªããã ®ã宣è¨ã§ãã¾ãã</p> -<p>Likewise, a subroutine call <tt>Baz::Qux::processThis()</tt> need not necessarily have been declared inside a file named <tt>Baz/Qux.pm</tt>. It could have been declared <em>literally anywhere</em>.</p> -<p>Separating these two concepts is one of the stupidest features of Perl, and treating them as separate concepts invariably results in chaotic, maddening code. Fortunately for us, the majority of Perl programmers obey the following two laws:</p> -<ol> +<p class=original>Likewise, a subroutine call <tt>Baz::Qux::processThis()</tt> need not necessarily have been declared inside a file named <tt>Baz/Qux.pm</tt>. It could have been declared <em>literally anywhere</em>.</p> +<p>ãã®ä¸ã<tt>Baz::Qux::processThis()</tt>ã®ãµãã«ã¼ãã³å¼ã³åºãã¯ã<tt>Baz/Qux.pm</tt>ã¨ããååã®ãã¡ã¤ã«å ã«å®£è¨ããã¦ããå¿ è¦ããããã¾ããã<em>æåéããã©ãã«ã§ã</em>å®ç¾©ãããã¨ãã§ãã¾ãã</p> +<p class=original>Separating these two concepts is one of the stupidest features of Perl, and treating them as separate concepts invariably results in chaotic, maddening code. Fortunately for us, the majority of Perl programmers obey the following two laws:</p> +<p>ãã®2ã¤ã®ã³ã³ã»ãããåãã¦ãã¾ã£ããã¨ã¯ãPerlã®æãæããªç¹å¾´ã®1ã¤ã§ãããããã2ã¤ã®ã³ã³ã»ãããåãã¦åãæ±ã£ã¦ãã¾ãã¨å¸¸ã«ãã«ãªã¹ã§æ°ã®çã£ããããªã³ã¼ãã¨ãªãã¾ãã幸éãªãã¨ã«ãPerlããã°ã©ãã®å¤ãã¯ã次ã®2ã¤ã®è¦åã«å¾ã£ã¦ãã¾ã:</p> +<ol class="original"> <li><strong>A Perl script (<tt>.pl</tt> file) must always contain exactly zero <tt>package</tt> declarations.</strong></li> <li><strong>A Perl module (<tt>.pm</tt> file) must always contain exactly one <tt>package</tt> declaration, corresponding exactly to its name and location.</strong> E.g. module <tt>Mathematics/Powers.pm</tt> must begin with <tt>package Mathematics::Powers</tt>.</li> </ol> -<p>Because of this, in practice you will find that most "packages" and "modules" produced by reliable third parties <em>can</em> be regarded and referred to interchangeably. However, it is important that you do not take this for granted, because one day you <em>will</em> meet code produced by a madman.</p> +<ol> + <li><strong>Perlã¹ã¯ãªãã(<tt>.pl</tt>ãã¡ã¤ã«)ã¯ã1ã¤ã®<tt>package</tt>宣è¨ãå«ã¾ãªãã</strong></li> + <li><strong>Perlã¢ã¸ã¥ã¼ã«(<tt>.pm</tt>ãã¡ã¤ã«)ã¯ã1ã¤ã®<tt>package</tt>宣è¨ãå«ã¿ãååã¨å ´æãä¸è´ãã¦ããã</strong> ä¾: ã¢ã¸ã¥ã¼ã«<tt>Mathematics/Powers.pm</tt>ã¯ã<tt>package Mathematics::Powers</tt>ã§å§ã¾ã£ã¦ããã</li> +</ol> -<h2>Object-oriented Perl</h2> +<p class="original">Because of this, in practice you will find that most "packages" and "modules" produced by reliable third parties <em>can</em> be regarded and referred to interchangeably. However, it is important that you do not take this for granted, because one day you <em>will</em> meet code produced by a madman.</p> +<p>ããã«ãã, ä¿¡é ¼ã§ãããµã¼ããã¼ãã£ã«ä½ããããã»ã¨ãã©ã®"packages"ã¨"modules"ãè¦ã¤ãããã¨ãã§ããç¸äºã«é¢ä¿ãåç §ãã<em>ãã</em>ããããããããå½ããåã¨æããªããã¨ã¯éè¦ã§ãããã¤ãã常è»ãé¸ãã人ãä½ã£ãã³ã¼ãã«åºä¼ã<em>ã§ããã</em>ã</p> -<p>Perl is not a great language for OO programming. Perl's OO capabilities were grafted on after the fact, and this shows.</p> +<h2 class="oritingal">Object-oriented Perl</h2> +<h2>ãªãã¸ã§ã¯ãæåPerl</h2> -<ul> +<p class="orginal">Perl is not a great language for OO programming. Perl's OO capabilities were grafted on after the fact, and this shows.</p> +<p>Perlã¯OOããã°ã©ãã³ã°ã«ããã¾ãé©ããè¨èªã§ã¯ããã¾ããã Perlã®OOè½åã¯å¾ä»ã®ãã®ã§ããããã¯ã</p> + +<ul class="original"> <li><p>An <i>object</i> is simply a reference (i.e. a scalar variable) which happens to know which class its referent belongs to. To tell a reference that its referent belongs to a class, use <tt>bless</tt>. To find out what class a reference's referent belongs to (if any), use <tt>ref</tt>.</p></li> <li><p>A <i>method</i> is simply a subroutine that expects an object (or, in the case of class methods, a package name) as its first argument. Object methods are invoked using <tt>$obj->method()</tt>; class methods are invoked using <tt>Package::Name->method()</tt>.</p></li> <li><p>A <i>class</i> is simply a package that happens to contain methods.</p></li> </ul> +<ul> + <li><p><i>ãªãã¸ã§ã¯ã</i>ã¯åç´ã«ãã©ã®ã¯ã©ã¹ã«å±ãã¦ãããã示ãã¦ãããªãã¡ã¬ã³ã¹ã§ã(i.e. ã¹ã«ã©å¤æ°)ããã®ãªãã¡ã¬ã³ããã¯ã©ã¹ã«å±ãã¦ãããã¨ããªãã¡ã¬ã³ã¹ã«æããããã«ã<tt>bless</tt>ã使ãã¾ããblessã«ãããã©ã®ã¯ã©ã¹ãã©ã®ãªãã¡ã¬ã³ãã«å±ããã®ããåããããã«ãªãã¾ãããªãã¡ã¬ã³ã¹ã®ãªãã¡ã¬ã³ããã©ã®ã¯ã©ã¹ã«å±ãã¦ããããã¿ã¤ããããã«ã¯ã<tt>ref</tt>ã使ãã¾ã</p></li></li> + <li><p><i>ã¡ã½ãã</i>ã¯åç´ã«ç¬¬ä¸å¼æ°ãªãã¸ã§ã¯ã(ã¾ãã¯ãã¯ã©ã¹ã¡ã½ããã§ããã°ãããã±ã¼ã¸å)ã§ãããµãã«ã¼ãã³ã§ãããªãã¸ã§ã¯ãã¡ã½ããã¯ã<tt>$obj->method()</tt>ã使ã£ã¦å¼ã³åºããã¾ã; ã¯ã©ã¹ã¡ã½ããã¯<tt>Package::Name->method()</tt>ã§ãã</p></li> + <li><p><i>ã¯ã©ã¹</i>ã¯åç´ã«ã¡ã½ãããå«ãããã±ã¼ã¸ã§ãã</p></li> +</ul> -<p>A quick example. An example module <tt>Animals/Animals.pm</tt> containing a class <tt>Animals::Animal</tt> reads like this:</p> +<p class="original">A quick example. An example module <tt>Animals/Animals.pm</tt> containing a class <tt>Animals::Animal</tt> reads like this:</p> +<p>以ä¸ãç°¡åãªä¾ã§ãã ä¾ã¨ãã¦ã<tt>Animals::Animal</tt>ã¯ã©ã¹ãå«ãã¢ã¸ã¥ã¼ã«<tt>Animals/Animals.pm</tt>ã¯ã次ã®ããã«ãªãã¾ã:</p> <pre class="perl prettyprint"> use strict; @@ -1487,7 +1505,8 @@ 1; </pre> -<p>And a Perl script making use of this class might read:</p> +<p class="original">And a Perl script making use of this class might read:</p> +<p>ãã®ã¯ã©ã¹ã使ãPerlã¹ã¯ãªããã¯æ¬¡ã®ããã«ãªãã§ããã:</p> <pre class="perl prettyprint"> use strict; @@ -1503,13 +1522,17 @@ $animal->eat("insects", "curry", "salmon"); </pre> -<p>This final call is equivalent to <tt>Animals::Animal::eat($animal, "insects", "curry", "salmon")</tt>.</p> +<p class="original">This final call is equivalent to <tt>Animals::Animal::eat($animal, "insects", "curry", "salmon")</tt>.</p> +<p>æå¾ã®å¼ã³åºãã¯ã<tt>Animals::Animal::eat($animal, "insects", "curry", "salmon")</tt>ã¨åãã§ãã</p> -<p>Note: literally any reference can be blessed into any class. It's up to you to ensure that (1) the referent can actually be used as an instance of this class and (2) that the class in question exists and has been loaded.</p> +<p class="original">Note: literally any reference can be blessed into any class. It's up to you to ensure that (1) the referent can actually be used as an instance of this class and (2) that the class in question exists and has been loaded.</p> +<p>注æ: æåéããã©ã®ãããªãªãã¡ã¬ã³ã¹ããã©ã®ãããªã¯ã©ã¹ã«ã§ãblessãããã¾ãã(1)ãªãã¡ã¬ã³ã¹ãå®éã«ãã®ã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ã¨ãã¦ä½¿ããã¦ãããã¨ã(2)åé¡ã®ã¯ã©ã¹ãåå¨ãããã¼ãããã¦ããããä¿è¨¼ããã®ã¯ããªã次第ã§ãã</p> -<h3>Inheritance</h3> +<h3 class="original">Inheritance</h3> +<h3>ç¶æ¿</h3> -<p>To create a class inheriting from a base class, populate the <tt>@ISA</tt> package variable. Let's suppose we subclassed <tt>Animals::Animal</tt> with <tt>Animals::Bear</tt>, located at <tt>Animals/Bear.pm</tt>:</p> +<p class="original">To create a class inheriting from a base class, populate the <tt>@ISA</tt> package variable. Let's suppose we subclassed <tt>Animals::Animal</tt> with <tt>Animals::Bear</tt>, located at <tt>Animals/Bear.pm</tt>:</p> +<p>ãã¼ã¹ã¯ã©ã¹ããç¶æ¿ãã¦ããã¯ã©ã¹ãä½ãã«ã¯ã<tt>@ISA</tt>ããã±ã¼ã¸å¤æ°ã«ãã¼ã¹ã¯ã©ã¹ãå ¥ãã¾ãããµãã¯ã©ã¹<tt>Animals::Animal</tt>ã<tt>Animals::Bear</tt>ã§ãµãã¯ã©ã¹ãä½ãã¨ãã¾ããå ´æã¯<tt>Animals/Bear.pm</tt>ã«ãªãã¾ã:</p> <pre class="perl prettyprint"> use strict; @@ -1532,7 +1555,8 @@ 1; </pre> -<p>And some sample code:</p> +<p class="original">And some sample code:</p> +<p>ãµã³ãã«ã³ã¼ã:</p> <pre class="perl prettyprint"> use strict; @@ -1545,12 +1569,18 @@ $bear->eat("insects", "curry", "salmon"); # eat only the salmon </pre> -<p>This final method call tries to invoke <tt>Animals::Bear::eat($bear, "insects", "curry", "salmon")</tt>, but a subroutine <tt>eat()</tt> isn't defined in the <tt>Animals::Bear</tt> package. However, because <tt>@ISA</tt> has been populated with a parent package <tt>Animals::Animal</tt>, the Perl interpreter tries calling <tt>Animals::Animal::eat($bear, "insects", "curry", "salmon")</tt> instead, which works. Note how the class <tt>Animals::Animal</tt> had to be loaded explicitly by <tt>Animals::Bear</tt>.</p> -<p>Since <tt>@ISA</tt> is an array, Perl supports multiple inheritance, with all the benefits and horrors this entails.</p> +<p class="original">This final method call tries to invoke <tt>Animals::Bear::eat($bear, "insects", "curry", "salmon")</tt>, but a subroutine <tt>eat()</tt> isn't defined in the <tt>Animals::Bear</tt> package. However, because <tt>@ISA</tt> has been populated with a parent package <tt>Animals::Animal</tt>, the Perl interpreter tries calling <tt>Animals::Animal::eat($bear, "insects", "curry", "salmon")</tt> instead, which works. Note how the class <tt>Animals::Animal</tt> had to be loaded explicitly by <tt>Animals::Bear</tt>.</p> +<p>æå¾ã®ã¡ã½ããå¼ã³åºãã¯ã<tt>Animals::Bear::eat($bear, "insects", "curry", "salmon")</tt>ãå¼ã¼ãã¨ãã¾ããããµãã«ã¼ãã³<tt>eat()</tt>ã¯ã<tt>Animals::Bear</tt> ããã±ã¼ã¸ã«ã¯å®ç¾©ããã¦ãã¾ãããã§ããã<tt>@ISA</tt>ã«ããã±ã¼ã¸<tt>Animals::Animal</tt>ãããã®ã§ãPerlã¤ã³ã¿ããªã¿ã¯ <tt>Animals::Animal::eat($bear, "insects", "curry", "salmon")</tt>ã代ããã«å¼ã¼ãã¨ãã¦ã代ããã«ããã¯åãã¾ãã次ã®ãã¨ã«æ³¨æãã¦ãã ãããã¯ã©ã¹<tt>Animals::Animal</tt>ã¯<tt>Animals::Bear</tt>ã§æ示çã«ãã¼ãããã¦ããªããã°ããã¾ããã</p> +<p class="original">Since <tt>@ISA</tt> is an array, Perl supports multiple inheritance, with all the benefits and horrors this entails.</p> +<p><tt>@ISA</tt> ã¯é åãªã®ã§ãPerl ã¯å¤éç¶æ¿ããµãã¼ããã¾ããå ¨ã¦ã®å©çã¨ææãä¼´ãã¾ããã</p> + -<h2><tt>BEGIN</tt> blocks</h2> +<h2 class="original"><tt>BEGIN</tt> blocks</h2> +<h2><tt>BEGIN</tt>ãããã¯</h2> -<p>A <tt>BEGIN</tt> block is executed as soon as the compiler has finished parsing it, even before the compiler parses the rest of the file. It is ignored at execution time.</p> +<p class="original">A <tt>BEGIN</tt> block is executed as soon as the compiler has finished parsing it, even before the compiler parses the rest of the file. It is ignored at execution time.</p> + +<p><tt>BEGIN</tt>ãããã¯ã¯ã³ã³ãã¤ã©ããã®ãããã¯ããã¼ã¹ãçµããã¨ããã«å®è¡ããã¾ããã³ã³ãã¤ã©ãã¾ã å ¨ã¦ããã¼ã¹ãã¦ããªãã¦ãã§ããBEGINãããã¯ã¯å®è¡æã«ã¯ç¡è¦ããã¾ãã</p> <pre class="perl prettyprint"> use strict; @@ -1565,8 +1595,11 @@ # actual code </pre> -<p>A <tt>BEGIN</tt> block is always executed first. If you create multiple <tt>BEGIN</tt> blocks (don't), they are executed in order from top to bottom as the compiler encounters them. A <tt>BEGIN</tt> block always executes first even if it is placed halfway through a script (don't do this) or even at the end (or this).</p> -<p>Because they are executed at compilation time, a <tt>BEGIN</tt> block placed inside a conditional block will <em>still</em> be executed first, even if the conditional evaluates to false and despite the fact that the conditional <em>has not been evaluated at all yet</em> and in fact <em>may never be evaluated</em>. <strong>Do not put <tt>BEGIN</tt> blocks in conditionals!</strong> If you want to do something conditionally at compile time, you need to put the conditional <em>inside</em> the <tt>BEGIN</tt> block:</p> +<p class="original">A <tt>BEGIN</tt> block is always executed first. If you create multiple <tt>BEGIN</tt> blocks (don't), they are executed in order from top to bottom as the compiler encounters them. A <tt>BEGIN</tt> block always executes first even if it is placed halfway through a script (don't do this) or even at the end (or this).</p> +<p><tt>BEGIN</tt>ãããã¯ã¯å¸¸ã«ããï½é¬¼å®è¡ããã¾ããè¤æ°ã®<tt>BEGIN</tt>ãããã¯ãæ¸ãã¨(don't)ã ä¸ããé çªã«ã³ã³ãã¤ã©ããããã«åºä¼ãé çªã«å®è¡ããã¾ãã<tt>BEGIN</tt>ãããã¯ã¯å¸¸ã«æåã«å®è¡ããã¾ãããããBEGINãããã¯ãã¹ã¯ãªããã®éä¸(ãããªãã¨ã¯ããªãã§ãã ãã)ããæå¾(or this)ã«æ¸ããã¦ããããã¦ãããã</p> +<p class="original">Because they are executed at compilation time, a <tt>BEGIN</tt> block placed inside a conditional block will <em>still</em> be executed first, even if the conditional evaluates to false and despite the fact that the conditional <em>has not been evaluated at all yet</em> and in fact <em>may never be evaluated</em>. <strong>Do not put <tt>BEGIN</tt> blocks in conditionals!</strong> If you want to do something conditionally at compile time, you need to put the conditional <em>inside</em> the <tt>BEGIN</tt> block:</p> +<p>ã³ã³ãã¤ã«æã«å®è¡ãããã®ã§ã<tt>BEGIN</tt>ãããã¯ãæ¡ä»¶ãããã¯ã®ä¸ã«ãã£ã¦ãã<em>ã¾ã </em>æåã«å®è¡ããã¾ãããã¨ããæ¡ä»¶ã®è©ä¾¡ãå½ã§ãããæ¡ä»¶ã<em>ã¾ã ã¾ã£ããè©ä¾¡ããã¦ããªã</em>ã«ãããããããå®éã«ã¯ã<em>è©ä¾¡ããããã¨ããªã</em>ã¨ãã¦ãã§ãã +<strong><tt>BEGIN</tt>ãããã¯ãæ¡ä»¶ã®ä¸ã«ç½®ãã¦ã¯è¡ãã¾ãã!</strong> ã³ã³ãã¤ã«æã«ä½ãããã®æ¡ä»¶ä»ãã®ãã¨ãããããã°ã<tt>BEGIN</tt>ãããã¯ã®<em>ä¸ã«<em>æ¡ä»¶æãç½®ããªããã°ãªãã¾ãã:</p> <pre class="perl prettyprint"> BEGIN { if($condition) { @@ -1576,14 +1609,17 @@ </pre> <h2><tt>use</tt></h2> -<p>Okay. Now that you understand the obtuse behaviour and semantics of packages, modules, class methods and <tt>BEGIN</tt> blocks, I can explain the exceedingly commonly-seen <tt>use</tt> function.</p> -<p>The following three statements:</p> +<p class="original">Okay. Now that you understand the obtuse behaviour and semantics of packages, modules, class methods and <tt>BEGIN</tt> blocks, I can explain the exceedingly commonly-seen <tt>use</tt> function.</p> +<p>ããã§ãããããããããã«ãããããã±ã¼ã¸ãã¢ã¸ã¥ã¼ã«ã¯ã©ã¹ã¡ã½ããã¨<tt>BEGIN</tt>ãããã¯ã®æå³ãç解ããã®ã§ãããè¦ããã<tt>use</tt>é¢æ°ã«ã¤ãã¦èª¬æã§ãã¾ãã</p> +<p class="original">The following three statements:</p> +<p>以ä¸ã®3ã¤ã®ã¹ãã¼ãã¡ã³ãã¯:</p> <pre class="perl prettyprint"> use Bugs::Caterpillar ("crawl", "pupate"); use Bugs::Caterpillar (); use Bugs::Caterpillar; </pre> -<p>are respectively equivalent to:</p> +<p class="original">are respectively equivalent to:</p> +<p>以ä¸ã¨ããããç価ã§ã:</p> <pre class="perl prettyprint"> BEGIN { require Bugs::Caterpillar; @@ -1597,18 +1633,27 @@ Bugs::Caterpillar->import(); } </pre> -<ul> +<ul class="original"> <li>No, the three examples are not in the wrong order. It is just that Perl is dumb.</li> <li>A <tt>use</tt> call is a disguised <tt>BEGIN</tt> block. The same caveats apply. <tt>use</tt> statements must always be placed at the top of the file, and <strong>never inside conditionals</strong>.</li> <li><tt>import()</tt> is not a built-in Perl function. It is a <strong>user-defined class method</strong>. The burden is on the programmer of the <tt>Bugs::Caterpillar</tt> package to define or inherit <tt>import()</tt>, and the method could theoretically accept anything as arguments and do anything with those arguments.</li> <li>Notice how <tt>require Bugs::Caterpillar</tt> loads a <strong>module</strong> named <tt>Bugs/Caterpillar.pm</tt>, whereas <tt>Bugs::Caterpillar->import()</tt> calls the <tt>import()</tt> subroutine that was defined inside the <tt>Bugs::Caterpillar</tt> <strong>package</strong>. Let's hope the module and the package coincide!</li> </ul> +<ul> + <li>ããããï¼ã¤ã®ä¾ã¯ééã£ãé çªã§ã¯ããã¾ãããPerlã馬鹿ãªã ãã§ãã</li> + <li><tt>use</tt>ã®å¼ã³åºãã¯<tt>BEGIN</tt>ãããã¯ã«å¤æããã¾ããåãè¦åãé©ç¨ããã¾ãã<tt>use</tt>ã¹ãã¼ãã¡ã³ãã¯ãã¡ã¤ã«ã®æåã«ç½®ãããªããã°ããã¾ããã<strong>æ¡ä»¶ã®ä¸ã«ç½®ãã¦ã¯ããã¾ãã</strong>ã</li> + <li><tt>import()</tt> ã¯Perlã®çµè¾¼ã®é¢æ°ã§ã¯ããã¾ããã<strong>ã¦ã¼ã¶ã¼å®ç¾©ã®ã¯ã©ã¹ã¡ã½ããã§ã</strong>ã<tt>import()</tt>ãå®ç¾©ããããç¶æ¿ãããã®ç¾©åã¯<tt>Bugs::Caterpillar</tt>ããã±ã¼ã¸ã®ããã°ã©ãã«ãããã¡ã½ããã¯çè«ä¸ãã©ã®ãããªå¼æ°ã§ãåãå ¥ããã©ã®ãããªãã¨ã§ãã§ãã¾ãã</li> + <li><tt>require Bugs::Caterpillar</tt>ã<tt>Bugs/Caterpillar.pm</tt>ã¨ããååã®<strong>module</strong>ããã¼ããããã¨ã«æ³¨æãã¦ãã ããã<tt>Bugs::Caterpillar->import()</tt>ã¯ã<tt>Bugs::Caterpillar</tt> <strong>package</strong>å ã«å®ç¾©ããã<tt>import()</tt>ãµãã«ã¼ãã³ãå¼ã³ã¾ããã¢ã¸ã¥ã¼ã«ã¨ããã±ã¼ã¸ãä¸è´ãã¦ãããã¨ãæå¾ ãã¾ããã!</li> +</ul> + <h2>Exporter</h2> -<p>The most common way to define an <tt>import()</tt> method is to inherit it from Exporter module. Exporter is a <i>de facto</i> core feature of the Perl programming language. In Exporter's implementation of <tt>import()</tt>, the list of arguments that you pass in is interpreted as a list of subroutine names. When a subroutine is <tt>import()</tt>ed, it becomes available in the current namespace as well as in its own original namespace.</p> +<p class="original">The most common way to define an <tt>import()</tt> method is to inherit it from Exporter module. Exporter is a <i>de facto</i> core feature of the Perl programming language. In Exporter's implementation of <tt>import()</tt>, the list of arguments that you pass in is interpreted as a list of subroutine names. When a subroutine is <tt>import()</tt>ed, it becomes available in the current namespace as well as in its own original namespace.</p> -<p>This concept is easiest to grasp using an example. Here's what <tt>Bugs/Caterpillar.pm</tt> looks like:</p> +<p><tt>import()</tt>ã¡ã½ãããå®ç¾©ãã常èçãªæ¹æ³ã¯ Exporter ã¢ã¸ã¥ã¼ã«ããç¶æ¿ãããã¨ã§ããExporter ã¯ããã°ã©ãã³ã°è¨èªPerlã®<i>ããã¡ã¯ã</i>ã®ã³ã¢ã®æ©è½ã§ããExporterã®<tt>import</tt>ã®å®è£ ã§ã渡ããå¼æ°ã®ãªã¹ãã¯ããµãã«ã¼ãã³ã®ååã¨ãã¦è§£éããã¾ãããµãã«ã¼ãã³ã<tt>import()</tt>ãããã¨ãç¾å¨ã®åå空éã§ããã®ãªãªã¸ãã«ã®åå空éã«ãããã®ã¨åãããã«ãå©ç¨å¯è½ã«ãªãã¾ãã</p> +<p class="original">This concept is easiest to grasp using an example. Here's what <tt>Bugs/Caterpillar.pm</tt> looks like:</p> +<p>ãã®ã³ã³ã»ããã¯ä¾ã使ãã¨ææ¡ããããã§ãããã<tt>Bugs/Caterpillar.pm</tt>ã¯æ¬¡ã®ãããªãã®ã§ã:</p> <pre class="perl prettyprint"> use strict; @@ -1630,7 +1675,8 @@ 1; </pre> -<p>And here's a script which makes use of the <tt>Bugs/Caterpillar.pm</tt> module:</p> +<p class="original">And here's a script which makes use of the <tt>Bugs/Caterpillar.pm</tt> module:</p> +<p><tt>Bugs/Caterpillar.pm</tt>ã¢ã¸ã¥ã¼ã«ã使ã£ãã¹ã¯ãªããã§ã:</p> <pre class="perl prettyprint"> use strict;