ktats****@users*****
ktats****@users*****
2012年 9月 2日 (日) 01:35:42 JST
Index: docs/articles/qntm.org/files/perl/perl.html diff -u docs/articles/qntm.org/files/perl/perl.html:1.8 docs/articles/qntm.org/files/perl/perl.html:1.9 --- docs/articles/qntm.org/files/perl/perl.html:1.8 Sat Sep 1 17:40:49 2012 +++ docs/articles/qntm.org/files/perl/perl.html Sun Sep 2 01:35:41 2012 @@ -55,13 +55,10 @@ <h2 class=original>Preliminary notes</h2> <h2>åæ¸ã</h2> <ul class=original> -<li><p>The following can be said of almost every declarative statement in this document: "that's not, strictly speaking, true; the situation is actually a lot more complicated". If you see a serious lie, point it out, but I reserve the right to preserve certain critical lies-to-children.</p></li> -<li><p>Throughout this document I'm using example <code>print</code> statements to output data but not explicitly appending line breaks. This is done to prevent me from going crazy and to give greater attention to the actual string being printed in each case, which is invariably more important. In many examples, this results in alotofwordsallsmusheduptogetherononeline if the code is run in reality. Try to ignore this. </li> -</ul> - -<ul> -<li><p>以ä¸ã®ãã¨ãããã®ããã¥ã¡ã³ãå ã®ã»ã¨ãã©ãã¹ã¦ã®å®£è¨æã§è¨ããã¨ãåºæ¥ã¾ã: "ããã¯ãå³æ ¼ãªæå³ã§ã®ãçå®ã§ã¯ããã¾ãã; ç¶æ³ã¯å®éã«ã¯ãã£ã¨è¤éã§ã"ãç§ã¯æ æã«åé¡ã®"å®å ¨ãªçå®"ããããã調ã¹ããã¨ããçç¥ããããç¡è¦ãã¦ãã¾ãããããã¯ãã¢ã¤ã³ã·ã¥ã¿ã¤ã³ã®å ´ã®æ¹ç¨å¼ã§7å¹´çã®ç©çå¦ã®å¦çãå§ããã®ã«æå³ããªãã®ã¨åãçç±ã§ããããæ·±å»ãªåãè¦ã¤ããããææãã¦ãã ãããã§ãããç§ã«ã¯åã©ãã«ã¤ãéè¦ãªåããã®ã¾ã¾ã«ãã¦ãã権å©ãããã¾ãã</p></li> -<li><p>ãã®ããã¥ã¡ã³ããéãã¦ãä¾ã«ããã¦<code>print</code>ã¹ãã¼ãã¡ã³ãããã¼ã¿ãåºåããããã«ä½¿ã£ã¦ãã¾ãããæ示çã«æ¹è¡ã追å ãã¦ãã¾ãããããã¯ãç§ãæ°éãã«ãããªãããã§ãããããããã®ã±ã¼ã¹ã§åºåããããããéè¦ãªå®éã®æååã«å¤ãã®æ³¨æãåããããããã§ããå¤ãã®ä¾ã§ãå®éã«ã³ã¼ããåããã¨ãalotofwordsallsmusheduptogetherononelineã®ãããªçµæã«ãªãã¾ããç¡è¦ãã¦ãã ããã</p></li> +<li><p class="original">The following can be said of almost every declarative statement in this document: "that's not, strictly speaking, true; the situation is actually a lot more complicated". If you see a serious lie, point it out, but I reserve the right to preserve certain critical lies-to-children.</p> +<p>以ä¸ã®ãã¨ãããã®ããã¥ã¡ã³ãå ã®ã»ã¨ãã©ãã¹ã¦ã®å®£è¨æã§è¨ããã¨ãåºæ¥ã¾ã: "ããã¯ãå³æ ¼ãªæå³ã§ã®ãçå®ã§ã¯ããã¾ãã; ç¶æ³ã¯å®éã«ã¯ãã£ã¨è¤éã§ã"ãç§ã¯æ æã«åé¡ã®"å®å ¨ãªçå®"ããããã調ã¹ããã¨ããçç¥ããããç¡è¦ãã¦ãã¾ãããããã¯ãã¢ã¤ã³ã·ã¥ã¿ã¤ã³ã®å ´ã®æ¹ç¨å¼ã§7å¹´çã®ç©çå¦ã®å¦çãå§ããã®ã«æå³ããªãã®ã¨åãçç±ã§ããããæ·±å»ãªåãè¦ã¤ããããææãã¦ãã ãããã§ãããç§ã«ã¯åã©ãã«ã¤ãéè¦ãªåããã®ã¾ã¾ã«ãã¦ãã権å©ãããã¾ãã</p></li> +<li><p class="original">Throughout this document I'm using example <code>print</code> statements to output data but not explicitly appending line breaks. This is done to prevent me from going crazy and to give greater attention to the actual string being printed in each case, which is invariably more important. In many examples, this results in alotofwordsallsmusheduptogetherononeline if the code is run in reality. Try to ignore this. </p> +<p>ãã®ããã¥ã¡ã³ããéãã¦ãä¾ã«ããã¦<code>print</code>ã¹ãã¼ãã¡ã³ãããã¼ã¿ãåºåããããã«ä½¿ã£ã¦ãã¾ãããæ示çã«æ¹è¡ã追å ãã¦ãã¾ãããããã¯ãç§ãæ°éãã«ãããªãããã§ãããããããã®ã±ã¼ã¹ã§åºåããããããéè¦ãªå®éã®æååã«å¤ãã®æ³¨æãåããããããã§ããå¤ãã®ä¾ã§ãå®éã«ã³ã¼ããåããã¨ãalotofwordsallsmusheduptogetherononelineã®ãããªçµæã«ãªãã¾ããç¡è¦ãã¦ãã ããã</p></li> </ul> <h2 class="original">Hello world</h2> @@ -117,7 +114,7 @@ <li>ä»ã®å¤æ°ã¸ã®ãªãã¡ã¬ã³ã¹</li> </ul> -<pre class="perl original"> +<pre class="perl prettyprint"> my $undef = undef; print $undef; # raises a warning; prints the empty string "" @@ -126,16 +123,6 @@ print $undef2; # exactly the same warning; prints "" </pre> -<pre class="perl original"> -my $num = 4040.5; -print $num; # "4040.5" -</pre> - -<pre class="perl original"> -my $string = "world"; -print $string; # "world" -</pre> - <pre class="perl prettyprint"> my $num = 4040.5; print $num; # "4040.5" @@ -177,7 +164,7 @@ <p class=original><strong>It is impossible to determine whether a scalar contains a "number" or a "string".</strong> More precisely, it should never be necessary to do this. Whether a scalar behaves like a number or a string depends on the operator with which it is used. When used as a string, a scalar will behave like a string. When used as a number, a scalar will behave like a number (raising a warning if this isn't possible):</p> <p><strong>ã¹ã«ã©ã¼ã«"æ°å"ã"å¤æ°"ã®ãããããå ¥ã£ã¦ããã®ããå¤æãããã¨ã¯ã§ãã¾ããã</strong> ããæ£ç¢ºã«ã¯ããããªãã¨ã¯è¦å½éãã§ããPerlã¯ãã®ç¹ã§å¼±ãåä»ãã§ãã ã¹ã«ã©ãæ°åãæåã®ã©ã¡ããã®ããã«æ¯èããã¯ã使ãããæ¼ç®åã«ããã¾ããæååã¨ãã¦ä½¿ãã°ãã¹ã«ã©ã¯æååã®ããã«ãµãã¾ãã¾ããæ°åã¨ãã¦ä½¿ãã°ãã¹ã«ã©ã¯æ°åã®ããã«ãµãã¾ãã¾ã(ã¾ãããããããã¨ãåºæ¥ãªããã°ãè¦åãçºãã¾ã):</p> -<pre class="perl"> +<pre class="perl prettyprint"> my $str1 = "4G"; my $str2 = "4H"; @@ -269,13 +256,6 @@ print "The last populated index is ".$#array; # "The last populated index is 5" </pre> -<p class=original>String concatenation using the <code>.</code> operator:</p> -<p>æååã®çµåã«ã¯<code>.</code>æ¼ç®åã使ãã¾ã:</p> - -<pre class="perl prettyprint"> -print $array[0].$array[1].$array[2]; # "printthesestrings" -</pre> - <p class=original>The arguments with which the original Perl script was invoked are stored in the <a href="http://perldoc.perl.org/perlvar.html">built-in array variable</a> <code>@ARGV</code>.</p> <p>ãªãªã¸ãã«ã®Perlã¹ã¯ãªããã®å®è¡æã®å¼æ°ã¯ã<a href="http://perldoc.perl.org/perlvar.html">çµè¾¼ã®é åå¤æ°</a><code>@ARGV</code>ã«å ¥ãã¾ãã</p> @@ -514,7 +494,7 @@ <p class=original>A scalar expression evaluated in list context turns into a single-element list:</p> <p>ã¹ã«ã©ã®å¼ã¯ãªã¹ãã³ã³ããã¹ãã§è©ä¾¡ãããã¨ãã²ã¨ã¤ã®å¤ã®ãªã¹ãã¨ãªãã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> my @array = "Mendeleev"; # same as 'my @array = ("Mendeleev");' </pre> @@ -530,9 +510,9 @@ my @array = ("Alpha", "Beta", "Gamma", "Pie"); my $scalar = @array; # Value of $scalar is now 4 </pre> -<p class=original>The <code><a href="http://perldoc.perl.org/functions/print.html">print</a></code> built-in function evaluates all of its arguments in list context. In fact, <code>print</code> accepts an unlimited list of arguments and prints each one after the other, which means it can be used to print arrays directly:</p> +<p class="original">The <code><a href="http://perldoc.perl.org/functions/print.html">print</a></code> built-in function evaluates all of its arguments in list context. In fact, <code>print</code> accepts an unlimited list of arguments and prints each one after the other, which means it can be used to print arrays directly:</p> <p><code><a href="http://perldoc.perl.org/functions/print.html">print</a></code> çµè¾¼é¢æ°ã¯å ¨ã¦ã®å¼æ°ããªã¹ãã³ã³ããã¹ãã§è©ä¾¡ãã¾ãã<code>print</code>ã¯ç¡å¶éã®ãªã¹ãã®å¼æ°ãååããä¸ã¤ä¸ã¤åºåãã¾ããã¤ã¾ããé åãç´æ¥ä¸ãããã¨ãåºæ¥ã¾ãã</p> -<pre class="perl"> +<pre class="perl prettyprint"> my @array = ("Alpha", "Beta", "Goo"); my $scalar = "-X-"; print @array; # "AlphaBetaGoo"; @@ -550,7 +530,7 @@ <p class=original>In the same way that lists cannot contain lists as elements, <strong>arrays and hashes cannot contain other arrays and hashes as elements.</strong> They can only contain scalars. Watch what happens when we try:</p> <p>ãªã¹ããè¦ç´ ã¨ãã¦ãªã¹ããå«ããªãã®ã¨åæ§ã<strong>é åã¨ããã·ã¥ã¯ä»ã®é åãããã·ã¥ãè¦ç´ ã¨ãã¦æã¦ã¾ãã</strong>ã 両æ¹ã¨ãã¹ã«ã©ããæã¦ã¾ããã ä»ãã試ããã¨ãããè¦ã¦ãã ãã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> my @outer = ("Sun", "Mercury", "Venus", undef, "Mars"); my @inner = ("Earth", "Moon"); @@ -855,8 +835,7 @@ </pre> <p class=original>Aside: singulars and plurals are best spelled out in full in both cases. Don't do something clever like the following, because anybody searching the codebase to replace the words "tooth" or "teeth" will never find this line:</p> -<p>ä½è«: 両æ¹ã®ã±ã¼ã¹ã®åæ°ã¨è¤æ°ãå®å ¨ã«æ¸ãåºããã¦ãã¾ãã決ãã¦ä»¥ä¸ã®ãããªå·§å¦ãªãã¨ãããªãã§ãã ããã -ã³ã¼ããæ¤ç´¢ãã¦ã"tooth"ã"teeth"ã®åèªãç½®ãæãããã¨ãã¦ãããã®è¡ããè¦ã¤ãããã¨ãã§ãã¾ããã</p> +<p>ä½è«: 両æ¹ã®ã±ã¼ã¹ã®åæ°ã¨è¤æ°ãå®å ¨ã«æ¸ãåºããã¦ãã¾ãã決ãã¦ä»¥ä¸ã®ãããªå·§å¦ãªãã¨ãããªãã§ãã ãããã³ã¼ããæ¤ç´¢ãã¦ã"tooth"ã"teeth"ã®åèªãç½®ãæãããã¨ãã¦ãããã®è¡ããè¦ã¤ãããã¨ãã§ãã¾ããã</p> <pre class="perl prettyprint"> my $lost = 1; @@ -876,8 +855,8 @@ <p class=original><code>if</code> statements evaluate their conditions in scalar context. For example, <code>if(@array)</code> returns true if and only if <code>@array</code> has 1 or more elements. It doesn't matter what those elements are - they may contain <code>undef</code> or other false values for all we care.</p> <p><code>if/code>æã§ã¯ããããã®æ¡ä»¶ãã¯ã¹ã«ã©ã³ã³ããã¹ãã§ä¾¡ããã¾ãã<code>if(@array)</code>ã¯ã<code>@array</code>ã«ã²ã¨ã¤ä»¥ä¸ã®ã¨ã¬ã¡ã³ããããå ´åã®ã¿ãçã¨ãªãã¾ããé åã®ä¸ã®å¤ãä½ãã¯åé¡ã«ãã¾ãã - å ¨ã¦ã<code>undef</code>ãä»ã®å½ã®å¤ã§ã</p> -<h3 class=original>Array iteration</h3> -<h3>é åã®ã¤ãã¬ã¼ã·ã§ã³</h3> +<h2 class=original>Loops</h2> +<h2>ã«ã¼ã</h2> <p class=original>There's More Than One Way To Do It.</p> <p>ããããã¯ã²ã¨ã¤ã§ã¯ããã¾ããã</p> @@ -914,7 +893,8 @@ } while ($i < scalar @array); </pre> -<p>and</p> +<p class="original">and</p> +<p>ããã¦</p> <pre class="perl prettyprint"> my $i = 0; @@ -950,22 +930,6 @@ } </pre> -<p class=original>If you don't provide an explicit iterator, Perl uses a default iterator, <code>$_</code>. <code>$_</code> is the first and friendliest of the built-in variables:</p> -<p>æ示çãªã¤ãã¬ã¼ã¿ã使ããªããã°ãPerlã¯ããã©ã«ãã®ã¤ãã¬ã¼ã¿ã¨ãã¦<code>$_</code>ã使ãã¾ãã<code>$_</code>㯠-If you don't provide an explicit iterator, Perl uses a default iterator, . <code>$_</code>ã¯æåã®ãã¬ã³ããªã¼ãªçµè¾¼ã®å¤æ°ã§ã:</p> - -<pre class="perl prettyprint"> -foreach ( @array ) { - print $_; -} -</pre> - -<p class=original>If using the default iterator, and you only wish to put a single statement inside your loop, you can use the super-short loop syntax:</p> -<p>ããã©ã«ãã®ã¤ãã¬ã¼ã¿ã使ããã¾ããã«ã¼ãã«åä¸ã®ã¹ãã¼ãã¡ã³ãããç½®ããªããªããã¨ã¦ãçãã«ã¼ãã®ã·ã³ã¿ãã¯ã¹ã使ãã¾ã:</p> -<pre class="perl prettyprint"> -print $_ foreach @array; -</pre> - <p class=original>You can't iterate over a hash. However, you can iterate over its keys. Use the <code>keys</code> built-in function to retrieve an array containing all the keys of a hash. Then use the <code>foreach</code> approach that we used for arrays:</p> <p>ããã·ã¥ã¯ã¤ãã¬ã¼ãã§ãã¾ããããã®ãã¼ãã¤ãã¬ã¼ãã§ãã¾ããçµè¾¼é¢æ°ã®<code>keys</code>ã使ã£ã¦ãããã·ã¥ã®å ¨ã¦ã®ãã¼ãå«ãé åãåãåºãã¦ãã ããããããããé åã§ä½¿ã£ã<code>foreach</code>ã®ã¢ããã¼ãã使ãã¾ã:</p> @@ -983,6 +947,15 @@ } </pre> +<p class="original">If you don't provide an explicit iterator, Perl uses a default iterator, <code>$_</code>. <code>$_</code> is the first and friendliest of <a href="http://perldoc.perl.org/perlvar.html">built-in variables</a>:</p> +<p>æ示çãªã¤ãã¬ã¼ã¿ã使ããªããã°ãPerlã¯ããã©ã«ãã®ã¤ãã¬ã¼ã¿ã¨ãã¦<code>$_</code>ã使ãã¾ãã<code>$_</code>ã¯æåã®æããã¬ã³ããªã¼ãª<a href="http://perldoc.perl.org/perlvar.html">çµè¾¼ã®å¤æ°</a>ã§ã:</p> + +<pre class="perl prettyprint"> +foreach ( @array ) { + print $_; +} +</pre> + <p class="original">If using the default iterator, and you only wish to put a single statement inside your loop, you can use the super-short loop syntax:</p> <p>ããã©ã«ãã®ã¤ãã¬ã¼ã¿ã使ããªããã«ã¼ãã«ä¸ã¤ã®ã¹ãã¼ãã¡ã³ãããç½®ããªãã®ãªããã¨ã¦ãçãã«ã¼ãã®ã·ã³ã¿ãã¯ã¹ã使ãã¾ã:</p> <pre class="perl prettyprint"> @@ -996,7 +969,7 @@ <p><code>next</code> 㨠<code>last</code>ã¯ã«ã¼ãé²ã¿ãå¶å¾¡ããã®ã«ä½¿ããã¾ããå¤ãã®ããã°ã©ãã³ã°è¨èªã§ã¯ãããããã<code>continue</code> 㨠<code>break</code>ã¨ãªã£ã¦ãã¾ãããªãã·ã§ã³ã§ãã©ã®ã«ã¼ãã«ãã©ãã«ãã¤ãããã¨ãã§ãã¾ããæ £ä¾ã«ãããã©ãã«ã¯ <code>å ¨ã¦å¤§æåã§</code>æ¸ããã¨ã«ãªã£ã¦ãã¾ããã«ã¼ãã«ã©ãã«ãã¤ãããã¨ã§ã<code>next</code> 㨠<code>last</code> ã«ã©ãã«ã対象ã«ã§ãã¾ãã100æªæºã®ç´ æ°ãè¦ã¤ããä¾ã§ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> CANDIDATE: for my $candidate ( 3 .. 100 ) { for my $divisor ( 2 .. <a href="http://perldoc.perl.org/functions/sqrt.html">sqrt</a> $candidate ) { next CANDIDATE if $candidate % $divisor == 0; @@ -1198,7 +1171,7 @@ <p class=original>Although the brackets are optional, subroutines should always be invoked using brackets, even when called with no arguments. This makes it clear that a subroutine call is happening.</p> <p>æ¬å¼§ã¯ãªãã·ã§ã³ã§ããããµãã«ã¼ãã³ã¯ãå¼æ°ãã²ã¨ã¤ãç¡ãã¦ãã常ã«æ¬å¼§ä»ãã§ä½¿ãã¹ãã§ãããããã¦ããã°ããµãã«ã¼ãã³ãå¼ã°ãããã¨ããæ確ã«ãªãã¾ãã</p> -<p class=original>Once you're inside a subroutine, the arguments are available using the <a href="http://perldoc.perl.org/perlvar.html">built-in array variable</a> <code>@_</code>>. Examples:</p> +<p class=original>Once you're inside a subroutine, the arguments are available using the <a href="http://perldoc.perl.org/perlvar.html">built-in array variable</a> <code>@_</code>. Example:</p> <p>ãµãã«ã¼ãã³ã®ä¸ã«å ¥ã£ã¦ãã¾ãã¨ã<a href="http://perldoc.perl.org/perlvar.html">çµè¾¼ã®é åå¤æ°</a><code>@_</code>ã使ãã¾ããä¾:</p> <pre class="perl prettyprint"> @@ -1266,7 +1239,7 @@ </pre> <p class=original>If no array is provided to the <code>shift</code> function, then it operates on <code>@_</code> implicitly. This approach is seen very commonly:</p> <p><code>shift</code>ã«é åã渡ããªããã°ãæé»ã«ã<code>@_</code>ã«å¯¾ãã¦æä½ãã¾ãããã®ã¢ããã¼ãã¯ã¨ã¦ãããè¦ããã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> sub left_pad { my $oldString = shift; my $width = shift; @@ -1334,10 +1307,7 @@ <p class="original">You can exit from a Perl script with the return code of your choice (from 0 to 255) using <code>exit</code>.</p> <p>Perlããçµäºããéã«ã<code>exit</code>ã使ã£ã¦(0ãã255ã¾ã§ã®)好ããªãªã¿ã¼ã³ã³ã¼ããè¿ãã¾ãã</p> <p class="original">Perl provides More Than One Way To - in a single call - spawn a child process, pause the current script until the child process has finished, and then resume interpretation of the current script. Whichever method is used, you will find that immediately afterwards, the <a href="http://perldoc.perl.org/perlvar.html">built-in scalar variable</a> <code>$?</code> has been populated with the status word that was returned from that child process's termination. You can get the return code by taking just the highest 8 of those 16 bits: <code>$? >> 8</code>.</p> -<p>Perlã«ã¯ä¸ã¤ä»¥ä¸ã® - åããã»ã¹ãç£ã - æ¹æ³ãããã¾ããç¾å¨ã®ã¹ã¯ãªãããæ¢ããåããã»ã¹ãçµãã£ãããç¾å¨ã®ã¹ã¯ãªããã®è§£éãç¶ãã¾ããã©ã®æ¹æ³ã使ã£ã¦ãããã®ç´å¾ã§ãåããã»ã¹ã®çµäºæã«è¿ãããç¶æ ã¯ã¼ããå ¥ã£ã¦ããã<a href="http://perldoc.perl.org/perlvar.html">çµè¾¼ã®ã¹ã«ã©å¤æ°</a>ã®<code>$?</code>ã«å ¥ãã¾ãã - - -ãè¦ã¤ãããã¾ãã16ãããã®ä¸ä½8ãåããã¨ã§ããªã¿ã¼ã³ã³ã¼ããå¾ããã¨ãã§ãã¾ã: <code>$? >> 8</code>ã</p> +<p>Perlã«ã¯ä¸ã¤ä»¥ä¸ã® - åããã»ã¹ãç£ã - æ¹æ³ãããã¾ããç¾å¨ã®ã¹ã¯ãªãããæ¢ããåããã»ã¹ãçµãã£ãããç¾å¨ã®ã¹ã¯ãªããã®è§£éãç¶ãã¾ããã©ã®æ¹æ³ã使ã£ã¦ãããã®ç´å¾ã§ãåããã»ã¹ã®çµäºæã«è¿ãããç¶æ ã¯ã¼ããå ¥ã£ã¦ããã<a href="http://perldoc.perl.org/perlvar.html">çµè¾¼ã®ã¹ã«ã©å¤æ°</a>ã®<code>$?</code>ã«å ¥ãã¾ããè¿ãããå¤ã®16ãããã®ä¸ä½8ãåããã¨ã§ããªã¿ã¼ã³ã³ã¼ããå¾ããã¨ãã§ãã¾ã: <code>$? >> 8</code>ã</p> <p class="original">The <code>system</code> function can be used to invoke another program with the arguments listed. The value returned by <code>system</code> is the same value with which <code>$?</code> is populated:</p> <p><code>system</code>é¢æ°ã¯ä»ã®ããã°ã©ã ãå¼æ°ã®ãªã¹ãã¨ä¸ç·ã«å¼ã³åºãã¾ãã<code>system</code>ã«ãã£ã¦è¿ãããå¤ã¯ã<code>$?</code>ã«å ¥ãã®ã¨åãå¤ã§ã:</p> <pre class="perl prettyprint"> @@ -1358,7 +1328,7 @@ use warnings; print @ARGV; -exit(37); +exit 37; </pre> <h2 class="original">Files and file handles</h2> @@ -1398,9 +1368,9 @@ # process the line... } </pre> -<p>To truncate that possible trailing line break, use <code><a href="http://perldoc.perl.org/functions/chomp.html">chomp</a></code>:</p> +<p class="original">To truncate that possible trailing line break, use <code><a href="http://perldoc.perl.org/functions/chomp.html">chomp</a></code>:</p> <p><code><a href="http://perldoc.perl.org/functions/chomp.html">chomp</a></code>ã使ãã¨æ¹è¡ãåãé¤ãã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> chomp $line; </pre> <p class="original">Note that <code>chomp</code> acts on <code>$line</code> in place. <code>$line = chomp $line</code> is probably not what you want.</p> @@ -1422,6 +1392,7 @@ # process $line... } </pre> +<p class="original">And even:</p> <p>次ã®ããã«ãã:</p> <pre class="perl prettyprint"> while(<$fh>) { @@ -1440,24 +1411,24 @@ <p class=original>File handles are actually closed automatically when they drop out of scope, but otherwise:</p> <p>ãã¡ã¤ã«ãã³ãã«ã¯ã¹ã³ã¼ããæããã¨èªåçã«éãããã¾ãããããã¯:</p> -<pre class="perl"> +<pre class="perl prettyprint"> <a href="http://perldoc.perl.org/functions/close.html">close</a> $fh2; close $fh; </pre> <p class="original">Three filehandles exist as global constants: <code>STDIN</code>, <code>STDOUT</code> and <code>STDERR</code>. These are open automatically when the script starts. To read a single line of user input:</p> <p>3ã¤ã®ãã¡ã¤ã«ãã³ãã«ãã°ãã¼ãã«ãªå®æ°ã¨ãã¦ããã¾ã: <code>STDIN</code>ã¨<code>STDOUT</code>ã¨<code>STDERR</code>ãããã¾ãããããã¯ã¹ã¯ãªãããéå§ãããã¨ãã«èªåçã«éããã¾ããã¦ã¼ã¶ã¼ã®å ¥åãä¸è¡èªãã«ã¯:</p> -<pre class="perl"> +<pre class="perl prettyprint"> my $line = <STDIN>; </pre> <p class="original">To just wait for the user to hit Enter:</p> <p>ã¦ã¼ã¶ã¼ãã¨ã³ã¿ã¼ãæ¼ãã¾ã§å¾ ã¤ã ãã§ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> <STDIN>; </pre> <p class="original">Calling <code><></code> with no filehandle reads data from standard input, or from any files named in arguments when the Perl script was called.</p> <p><code><></code>ããã¡ã¤ã«ãã³ãã«ãªãã§å¼ã¶ã¨ãæ¨æºå ¥åãããã¼ã¿ãèªãããPerlã¹ã¯ãªãããå¼ã°ããæã®å¼æ°ã«æ¸¡ãããååã®ãã¡ã¤ã«ããèªã¿ã¾ãã</p> -<p>As you may have gathered, <code>print</code> prints to <code>STDOUT</code> by default if no filehandle is named.</p> +<p class="original">As you may have gathered, <code>print</code> prints to <code>STDOUT</code> by default if no filehandle is named.</p> <p>åãããã¨æãã¾ããã<code>print</code>ã¯ããã¡ã¤ã«ãã³ãã«ãçç¥ãããå ´åã¯ãããã©ã«ãã§<code>STDOUT</code>ã«åºåãã¾ããã</p> <h3 class=original>File tests</h3> @@ -1475,23 +1446,29 @@ <p class=original>These are just three of <a href="http://perldoc.perl.org/functions/-X.html">a large class of functions</a> of the form <code>-X</code> where <code>X</code> is some lower- or upper-case letter. These functions are called <i>file tests</i>. Note the leading minus sign. In a Google query, the minus sign indicates to exclude results containing this search term. This makes file tests hard to Google for! Just search for "perl file test" instead.</p> <p><code>-X</code>å½¢å¼ã®<a href="http://perldoc.perl.org/functions/-X.html">大ããªã¯ã©ã¹ã®é¢æ°</a>ã®3ã¤ã®é¢æ°ã§ãã<code>X</code>ã¯å°æåã大æåã§ãããããã®é¢æ°ã¯<i>ãã¡ã¤ã«ãã¹ã</i>ã¨å¼ã°ãã¾ãããã¤ãã¹è¨å·ãå ã«æ¥ããã¨ã«æ°ãã¤ãã¦ãã ãããGoogleã¯ã¨ãªã§ã¯ããã¤ãã¹è¨å·ã¯ããã®è¨èãçµæã«å«ããªããã¨ãæ示ãã¾ãããã®ããããã¡ã¤ã«ãã¹ããGoogleã§æ¤ç´¢ãã¥ãããã¦ãã¾ã! 代ããã«ã"perl file test"ã§æ¤ç´¢ãã¦ä¸ããã</p> -<h2>Regular expressions</h2> -<p>Regular expressions appear in many languages and tools other than Perl. Perl's core regular expression syntax is basically the same as everywhere else. (Perl's <em>full</em> regular expression capabilities are terrifyingly complex and difficult to understand; the best advice I can give you is to avoid this complexity wherever possible.)</p> -<p>Match operations are performed using <code>=~ m//</code>. In scalar context, <code>=~ m//</code> returns true on success, false on failure.</p> +<h2 class="original">Regular expressions</h2> +<h2>æ£è¦è¡¨ç¾</h2> +<p class="original">Regular expressions appear in many languages and tools other than Perl. Perl's core regular expression syntax is basically the same as everywhere else. (Perl's <em>full</em> regular expression capabilities are terrifyingly complex and difficult to understand; the best advice I can give you is to avoid this complexity wherever possible.)</p> +<p>æ£è¦è¡¨ç¾ã¯Perl以å¤ã®å¤ãã®è¨èªããã¼ã«ã«ã¿ããã¾ããPerlã®ã³ã¢ã®æ£è¦è¡¨ç¾ã·ã³ã¿ãã¯ã¹ã¯ä»ã®ãã®ã¨åºæ¬çã«åãã§ãã(Perlã®<em>å®å ¨ãª</em>æ£è¦è¡¨ç¾ã®æ§è½ã¯æãããè¤éã§ç解ããã®ãé£ããã§ã; ä¸çªè¯ãã¢ããã¤ã¹ãããã¨ããããå¯è½ãªéãããããè¤éãªãã®ãé¿ããã¨ãããã¨ã§ã)ã</p> +<p class="original">Match operations are performed using <code>=~ m//</code>. In scalar context, <code>=~ m//</code> returns true on success, false on failure.</p> +<p>ãããã¯ããããæ¼ç®å<code>=~ m//</code>ã使ã£ã¦ããã¾ããã¹ã«ã©ã³ã³ããã¹ãã§ã¯ã<code>=~ m//</code>ã¯ãæåãªãçãè¿ãã失æãªãå½ãè¿ãã¾ãã</p> -<pre class="perl"> +<pre class="perl prettyprint"> my $string = "Hello world"; if($string =~ m/(\w+)\s+(\w+)/) { print "success"; } </pre> -<p>Parentheses perform sub-matches. After a successful match operation is performed, the sub-matches get stuffed into the built-in variables <code>$1</code>, <code>$2</code>, <code>$3</code>, ...:</p> -<pre class="perl"> +<p class="original">Parentheses perform sub-matches. After a successful match operation is performed, the sub-matches get stuffed into the built-in variables <code>$1</code>, <code>$2</code>, <code>$3</code>, ...:</p> +<p>æ¬å¼§ã¯ãµããããã«ãªãã¾ããããããæåãããããµããããã¯çµè¾¼å¤æ°ã®<code>$1</code>, <code>$2</code>, <code>$3</code>, ...ã«ããããããã®ãå ¥ãã¾ã:</p> +<pre class="perl prettyprint"> print $1; # "Hello" print $2; # "world" </pre> -<p>In list context, <code>=~ m//</code> returns <code>$1</code>, <code>$2</code>, ... as a list.</p> -<pre class="perl"> +<p class="original">In list context, <code>=~ m//</code> returns <code>$1</code>, <code>$2</code>, ... as a list.</p> +<p>ãªã¹ãã³ã³ããã¹ãã§ã¯ã<code>=~ m//</code> ã¯<code>$1</code>, <code>$2</code>, ... ããªã¹ãã¨ãã¦è¿ãã¾ã:</p> + +<pre class="perl prettyprint"> my $string = "colourless green ideas sleep furiously"; my @matches = $string =~ m/(\w+)\s+((\w+)\s+(\w+))\s+(\w+)\s+(\w+)/; @@ -1499,33 +1476,40 @@ # prints "'colourless', 'green ideas', 'green', 'ideas', 'sleep', 'furiously'" </pre> -<p>Substitution operations are performed using <code>=~ s///</code>.</p> -<pre class="perl"> +<p class="original">Substitution operations are performed using <code>=~ s///</code>.</p> +<p>ç½®ææä½ã¯<code>=~ s///</code>ã§è¡ãã¾ãã</p> +<pre class="perl prettyprint"> my $string = "Good morning world"; $string =~ s/world/Vietnam/; print $string; # "Good morning Vietnam" </pre> -<p>Notice how the contents of <code>$string</code> have changed. You have to pass a scalar variable on the left-hand side of an <code>=~ s///</code> operation. If you pass a literal string, you'll get an error.</p> +<p class="original">Notice how the contents of <code>$string</code> have changed. You have to pass a scalar variable on the left-hand side of an <code>=~ s///</code> operation. If you pass a literal string, you'll get an error.</p> +<p><code>$string</code>ã®ä¸èº«ãå¤æ´ããæ¹æ³ã«æ³¨æãã¦ãã ããã<code>=~ s///</code> æä½ã®å·¦å´ã«ã¹ã«ã©å¤æ°ã渡ããªããã°ãªãã¾ããããªãã©ã«ã®æååã渡ããå ´åãã¨ã©ã¼ã«ãªãã¾ãã</p> + +<p class="original">The <code>/g</code> flag indicates "group match".</p> +<p><code>/g</code>ãã©ã°ã¯"ã°ã«ã¼ãããã"ãæ示ãã¾ãã</p> -<p>The <code>/g</code> flag indicates "group match".</p> +<p class="original">In scalar context, each <code>=~ m//g</code> call finds another match after the previous one, returning true on success, false on failure. You can access <code>$1</code> and so on afterwards in the usual way. For example:</p> +<p>ã¹ã«ã©ã³ã³ããã¹ãã§ã¯ãããããã®<code>=~ m//g</code> å¼ã³åºãã¯åã®ãã®ã®å¾ã®ä»ã®ããããæ¢ããæåããã¨çãè¿ãã失æããã¨å¤ãè¿ãã¾ããããããæ¹æ³ã§<code>$1</code>ãªã©ã«ã¢ã¯ã»ã¹åºæ¥ã¾ãã ä¾:</p> -<p>In scalar context, each <code>=~ m//g</code> call finds another match after the previous one, returning true on success, false on failure. You can access <code>$1</code> and so on afterwards in the usual way. For example:</p> -<pre class="perl"> +<pre class="perl prettyprint"> my $string = "a tonne of feathers or a tonne of bricks"; while($string =~ m/(\w+)/g) { print "'".$1."'\n"; } </pre> -<p>In list context, an <code>=~ m//g</code> call returns all of the matches at once.</p> -<pre class="perl"> +<p class="original">In list context, an <code>=~ m//g</code> call returns all of the matches at once.</p> +<p>ãªã¹ãã³ã³ããã¹ãã§ã¯ã<code>=~ m//g</code>å¼ã³åºãã¯ãããããããã®ãä¸åº¦ã«å ¨é¨è¿ãã¾ãã</p> +<pre class="perl prettyprint"> my @matches = $string =~ m/(\w+)/g; print join ", ", map { "'".$_."'" } @matches; </pre> -<p>An <code>=~ s///g</code> call performs a global search/replace and returns the number of matches. Here, we replace all vowels with the letter "r".</p> +<p class="original">An <code>=~ s///g</code> call performs a global search/replace and returns the number of matches. Here, we replace all vowels with the letter "r".</p> +<p><code>=~ s///g</code>å¼ã³åºãã¯ã°ãã¼ãã«ãªæ¤ç´¢/ç½®æã§ãããããæ°ãè¿ãã¾ããããã§ã¯ããã¹ã¦ã®æ¯é³ãæå"r"ã«ç½®æãã¦ãã¾ãã</p> -<pre class="perl"> +<pre class="perl prettyprint"> # Try once without /g. $string =~ s/[aeiou]/r/; print $string; # "r tonne of feathers or a tonne of bricks" @@ -1539,9 +1523,11 @@ print $string, "\n"; # "r trnnr rf frrthrrs rr r trnnr rf brrcks" </pre> -<p>The <code>/i</code> flag makes matches and substitutions case-insensitive.</p> -<p>The <code>/x</code> flag allows your regular expression to contain whitespace (e.g., line breaks) and comments.</p> -<pre class="perl"> +<p class="original">The <code>/i</code> flag makes matches and substitutions case-insensitive.</p> +<p><code>/i</code> ãã©ã°ã¯ãããã¨ç½®æãã±ã¼ã¹ã¤ã³ã»ã³ã·ãã£ãã«ãã¾ãã</p> +<p class="original">The <code>/x</code> flag allows your regular expression to contain whitespace (e.g., line breaks) and comments.</p> +<p class="original"><code>/x</code>ãã©ã°ã¯æ£è¦è¡¨ç¾ã®ä¸ã«ç©ºç½(e.g. æ¹è¡)ãã³ã¡ã³ããå«ãããã¨ãã§ããããã«ãã¾ãã</p> +<pre class="perl prettyprint"> "Hello world" =~ m/ (\w+) # one or more word characters [ ] # single literal space, stored inside a character class @@ -1551,98 +1537,132 @@ # returns true </pre> -<h2 class=original>Packages and modules</h2> -<h2>ããã±ã¼ã¸ã¨ã¢ã¸ã¥ã¼ã«</h2> +<h2 class=original>Modules and packages</h2> +<h2>ã¢ã¸ã¥ã¼ã«ã¨ããã±ã¼ã¸</h2> + +<p class="original">In Perl, modules and packages are different things.</p> +<p>Perlã«ããã¦ãã¢ã¸ã¥ã¼ã«ã¨ããã±ã¼ã¸ã¯å¥ç©ã§ãã</p> + + +<h3 class=original>Modules</h3> +<h3>ã¢ã¸ã¥ã¼ã«</h3> + +<p class=original>A <i>module</i> is a <code>.pm</code> file that you can include in another Perl file (script or module). A module is a text file with exactly the same syntax as a <code>.pl</code> Perl script. An example module might be located at <code>C:\foo\bar\baz\Demo\StringUtils.pm</code> or <code>/foo/bar/baz/Demo/StringUtils.pm</code>, and read as follows:</p> +<p>A <i>ã¢ã¸ã¥ã¼ã«</i>ã¯ãä»ã®Perlãã¡ã¤ã«(ã¹ã¯ãªãããã¢ã¸ã¥ã¼ã«)ã«å«ãããã¨ãåºæ¥ã<code>.pm</code>ãã¡ã¤ã«ã§ããã¢ã¸ã¥ã¼ã«ã¯ <code>.pl</code> Perlã¹ã¯ãªããã¨ã¾ã£ããåãã·ã³ã¿ãã¯ã®ã¹ããã¹ããã¡ã¤ã«ã§ããä¾ã®ã¢ã¸ã¥ã¼ã«ã¯ã<code>C:\foo\bar\baz\Demo\StringUtils.pm</code> ã <code>/foo/bar/baz/Demo/StringUtils.pm</code>ã«ããã¾ããç¶ããèªãã§ãã ãã:</p> +<pre class="perl prettyprint"> +use strict; +use warnings; + +sub zombify { + my $word = shift @_; + $word =~ s/[aeiou]/r/g; + return $word; +} + +return 1; +</pre> + +<p class=original>Because a module is executed from top to bottom when it is loaded, you need to return a true value at the end to show that it was loaded successfully.</p> +<p>ã¢ã¸ã¥ã¼ã«ã¯ãã¼ããããã¨ãæåããæå¾ã¾ã§å®è¡ãããã®ã§ããã¼ããæåãããã¨ã示ãããã«ãæå¾ã«çã®å¤ãè¿ãå¿ è¦ãããã¾ãã</p> + +<p class=original>So that the Perl interpreter can find them, directories containing Perl modules should be listed in your environment variable <code>PERL5LIB</code> beforehand. List the root directory containing the modules, don't list the module directories or the modules themselves:</p> +<p>Perlã¤ã³ã¿ã¼ããªã¿ã¯ããããè¦ã¤ãããã¨ãã§ããããã«ã¯ããã®åã«ãPerlã¢ã¸ã¥ã¼ã«ãå ¥ã£ã¦ãããã£ã¬ã¯ããªããç°å¢å¤æ°<code>PERL5LIB</code>ã«ãªã¹ãããã¦ããã¹ãã§ããã¢ã¸ã¥ã¼ã«ãå ¥ã£ã¦ããã«ã¼ããã£ã¬ã¯ããªããªã¹ããã¦ãã ãããã¢ã¸ã¥ã¼ã«ã®ãã£ã¬ã¯ããªãã¢ã¸ã¥ã¼ã«èªèº«ããªã¹ããã¦ã¯ããã¾ããã</p> + +<pre class="bash"> +set PERL5LIB=C:\foo\bar\baz;%PERL5LIB% +</pre> +<p class=original>or</p> +<p>ã¾ãã¯</p> +<pre class="bash"> +export PERL5LIB=/foo/bar/baz:$PERL5LIB +</pre> + +<p class=original>Once the Perl module is created and <code>perl</code> knows where to look for it, you can use the <code>require</code> built-in function to search for and execute it during a Perl script. For example, calling <code>require Demo::StringUtils</code> causes the Perl interpreter to search each directory listed in <code>PERL5LIB</code> in turn, looking for a file called <code>Demo/StringUtils.pm</code>. 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 <code>main.pl</code> and read as follows:</p> +<p>Perlã¢ã¸ã¥ã¼ã«ãä½ããã¦ã<code>perl</code>ããããã©ãã«ããããç¥ã£ã¦ããã°ãçµè¾¼ã®<code>require</code>é¢æ°ã使ã£ã¦æ¢ããPerlã®ã¹ã¯ãªããä¸ã§å®è¡ãããã¨ãã§ãã¾ããä¾ãã°ã<code>require Demo::StringUtils</code>ãå¼ã¶ã¨ãPerlã¤ã³ã¿ã¼ããªã¿ã¯<code>PERL5LIB</code>ã«ãªã¹ãããã¦ãããã£ã¬ã¯ããªãé çªã«ã<code>Demo/StringUtils.pm</code>ã¨ãããã¡ã¤ã«ãæ¢ãã¾ããã¢ã¸ã¥ã¼ã«ããã¼ããããããçªç¶ã«ãµãã«ã¼ãã³ã¨å¤æ°ãã¡ã¤ã³ã¹ã¯ãªããã«å®ç¾©ããã¾ãããã®ä¾ã®ã¹ã¯ãªããã<code>main.pl</code>ã¨å¼ã³ã¾ããããç¶ãã¦èªãã§ããã:</p> + +<pre class="perl prettyprint"> +use strict; +use warnings; + +require Demo::StringUtils; + +print zombify("i want brains"); # "r wrnt brrrns" +</pre> + +<p class="original"><i>Note the use of the double colon <code>::</code> as a directory separator.</i></p> +<p><i>åå空éã®åºåãã«<code>::</code>ã使ã£ã¦ããã®ã«æ³¨æãã¦ãã ãã</i></p> -<p class=original>In Perl, packages and modules are different things.</p> -<p>Perlã§ã¯ãããã±ã¼ã¸ã¨ã¢ã¸ã¥ã¼ã«ã¯éããã®ã§ãã</p> +<p class="original">Now a problem surfaces: if <code>main.pl</code> contains many <code>require</code> calls, and each of the modules so loaded contains more <code>require</code> calls, then it can become difficult to track down the original declaration of the <code>zombify</code> subroutine. The solution to this problem is to use packages.</p> +<p>ããã§åé¡ã表é¢åãã¾ã: <code>main.pl</code>ããå¤ãã®<code>require</code> ãå¼ãã§ãã¦ãããããã®ã¢ã¸ã¥ã¼ã«ãããã«<code>require</code>ãå¼ãã§ããå ´åã<code>zombify</code>ãµãã«ã¼ãã³ã®å ã®å®£è¨ãã©ãã«ããã®ãã追ãã«ãããªãã¾ãããã®è§£æ±ºçã¨ãã¦ã¯ãããã±ã¼ã¸ã使ããã¨ã§ãã</p> <h3 class=original>Packages</h3> <h3>ããã±ã¼ã¸</h3> -<p class=original>A <i>ããã±ã¼ã¸</i> is a namespace in which subroutines and package variables can be declared. Any subroutine or package variable you declare is implicitly declared within the current package. At the beginning of execution, you are in the <code>main</code> package, but you can switch package using the <code>package</code> built-in function:</p> -<p><i>package</i>ã¯åå空éã§ããã®ä¸ã§ã宣è¨ãããµãã«ã¼ãã³ãããã±ã¼ã¸å¤æ°ã¯ãæé»çã«ãç¾å¨ã®ããã±ã¼ã¸å ã§å®£è¨ããã¾ããå®è¡ã®æåã¯ã<code>main</code>ããã±ã¼ã¸ã«ãªãã¾ãããçµè¾¼é¢æ°ã®<code>package</code>ã使ã£ã¦ãããã±ã¼ã¸ãåãæ¿ãããã¾ã:</p> +<p class=original>A <i>package</i> is a namespace in which subroutines can be declared. Any subroutine you declare is implicitly declared within the current package. At the beginning of execution, you are in the <code>main</code> package, but you can switch package using the <code>package</code> built-in function:</p> +<p><i>package</i>ã¯åå空éã§ããã®ä¸ã§ããµãã«ã¼ãã³ã宣è¨ã§ãã¾ãã宣è¨ãããµãã«ã¼ãã³ã¯ãæé»çã«ãç¾å¨ã®ããã±ã¼ã¸å ã«å®£è¨ããã¾ããå®è¡ã®æåã¯ã<code>main</code>ããã±ã¼ã¸ã«ãªãã¾ãããçµè¾¼é¢æ°ã®<code>package</code>ã使ã£ã¦ãããã±ã¼ã¸ãåãæ¿ãããã¾ã:</p> <pre class="perl prettyprint"> +use strict; +use warnings; + sub subroutine { print "universe"; } -our $variable = "empty"; - package Food::Potatoes; # no collision: sub subroutine { print "kingedward"; } - -our $variable = "mashed"; </pre> -<p class=original>Any time you call a subroutine, you implicitly call a subroutine which is inside the current package. The same is true of package variables. Alternatively, you can explicitly provide a package. See what happens if we continue the above script:</p> -<p>ãµãã«ã¼ãã³ãå¼ãã ã¨ãã¯ãã¤ã§ããæé»ã«ç¾å¨ã®ããã±ã¼ã¸å ã®ãµãã«ã¼ãã³ãå¼ãã§ãã¾ããåããã¨ã¯ãããã±ã¼ã¸å¤æ°ã«ãè¨ãã¾ãããã®ä»£ããã«ãããã±ã¼ã¸ãæ示çã«æ¸ããã¨ãã§ãã¾ããä¸ã®ã¹ã¯ãªãããå®è¡ããããä½ãèµ·ããã§ãããã:</p> +<p class="original"><i>Note the use of the double colon <code>::</code> as a namespace separator.</i></p> +<p><i>åå空éã®åºåãã«<code>::</code>ã使ã£ã¦ããã®ã«æ³¨æãã¦ãã ãã</i></p> -<pre class="perl prettyprint"> -subroutine(); # "kingedward" -print $variable; # "mashed" +<p class="original">Any time you call a subroutine, you implicitly call a subroutine which is inside the current package. Alternatively, you can explicitly provide a package. See what happens if we continue the above script:</p> +<p>ãµãã«ã¼ãã³ãå¼ãã ã¨ãã¯ãã¤ã§ããæé»ã«ç¾å¨ã®ããã±ã¼ã¸å ã®ãµãã«ã¼ãã³ãå¼ãã§ãã¾ãã代ããã«ãããã±ã¼ã¸ãæ示çã«æ¸ããã¨ãã§ãã¾ããä¸ã®ã¹ã¯ãªãããå®è¡ããããä½ãèµ·ããã§ãããã:</p> -main::subroutine(); # "universe" -print $main::variable; # "empty" -Food::Potatoes::subroutine(); # "kingedward" -print $Food::Potatoes::variable; # "mashed" +<pre class="perl prettyprint"> +subroutine(); # "kingedward" +main::subroutine(); # "universe" +Food::Potatoes::subroutine(); # "kingedward" </pre> -<h3 class=original>Modules</h3> -<h3>ã¢ã¸ã¥ã¼ã«</h3> +<p class="original">So the logical solution to the problem described above is to modify <code>C:\foo\bar\baz\Demo\StringUtils.pm</code> or <code>/foo/bar/baz/Demo/StringUtils.pm</code> to read:</p> +<p>ã§ãã®ã§ãä¸ã§è¿°ã¹ãåé¡ã®è«ççãªè§£æ±ºçã¯<code>C:\foo\bar\baz\Demo\StringUtils.pm</code>ã<code>/foo/bar/baz/Demo/StringUtils.pm</code>ãå¤æ´ãããã¨ã§ã:</p> -<p class=original>A <i>module</i> is a <code>.pm</code> file that you can include in another Perl file (script or module). A module is a text file with exactly the same syntax as a <code>.pl</code> Perl script. An example module might be located at <code>C:\foo\bar\baz\Mathematics\Powers.pm</code> or <code>/foo/bar/baz/Mathematics/Powers.pm</code>, and read as follows:</p> -<p>A <i>ã¢ã¸ã¥ã¼ã«</i>ã¯ãä»ã®Perlãã¡ã¤ã«(ã¹ã¯ãªãããã¢ã¸ã¥ã¼ã«)ã«å«ãããã¨ãåºæ¥ã<code>.pm</code>ãã¡ã¤ã«ã§ããã¢ã¸ã¥ã¼ã«ã¯ <code>.pl</code> Perlã¹ã¯ãªããã¨ã¾ã£ããåãã·ã³ã¿ãã¯ã®ã¹ããã¹ããã¡ã¤ã«ã§ããä¾ã®ã¢ã¸ã¥ã¼ã«ã¯ã<code>C:\foo\bar\baz\Mathematics\Powers.pm</code> ã <code>/foo/bar/baz/Mathematics/Powers.pm</code>ã«ããã¾ããç¶ããèªãã§ãã ãã:</p> <pre class="perl prettyprint"> use strict; use warnings; -package Mathematics::Powers; - -our $e = 2.71828; +<ins>package Demo::StringUtils;</ins> -sub exp { - return $e ** shift; +sub zombify { + my $word = shift @_; + $word =~ s/[aeiou]/r/g; + return $word; } -1; -</pre> - -<p class=original>Because a module is executed from top to bottom when it is loaded, you need to return a true value at the end to show that it was loaded successfully. <code>return 1</code> would suffice. If you don't use <code>return</code>, the value returned is <em>the value returned when the most recent statement was evaluated</em>. So, you will often see <code>1</code> at the bottom of a Perl module, as shown above.</p> -<p>ã¢ã¸ã¥ã¼ã«ã¯ãã¼ããããã¨ãæåããæå¾ã¾ã§å®è¡ãããã®ã§ããã¼ããæåãããã¨ã示ãããã«ãæå¾ã«çã®å¤ãè¿ãå¿ è¦ãããã¾ãã<code>return 1</code>ã§ååã§ãã<code>return</code>ã使ããªããã°ãè¿ãããå¤ã¯ã<em>ç´è¿ã®ã¹ãã¼ãã¡ã³ããè©ä¾¡ãããã¨ãã®æ»ãå¤</em>ã«ãªãã¾ãããã®ããã«ãä¸ã§è¦ãããã«ãPerlã¢ã¸ã¥ã¼ã«ã®æå¾ã«<code>1</code>ãè¯ãè¦ããããã¨ã«ãªãã¾ãã</p> - -<p class=original>So that the Perl interpreter can find them, directories containing Perl modules should be listed in your environment variable <code>PERL5LIB</code> beforehand. List the root directory containing the modules, don't list the module directories or the modules themselves:</p> -<p>Perlã¤ã³ã¿ã¼ããªã¿ã¯ããããè¦ã¤ãããã¨ãã§ããããã«ã¯ããã®åã«ãPerlã¢ã¸ã¥ã¼ã«ãå ¥ã£ã¦ãããã£ã¬ã¯ããªããç°å¢å¤æ°<code>PERL5LIB</code>ã«ãªã¹ãããã¦ããã¹ãã§ããã¢ã¸ã¥ã¼ã«ãå ¥ã£ã¦ããã«ã¼ããã£ã¬ã¯ããªããªã¹ããã¦ãã ãããã¢ã¸ã¥ã¼ã«ã®ãã£ã¬ã¯ããªãã¢ã¸ã¥ã¼ã«èªèº«ããªã¹ããã¦ã¯ããã¾ããã</p> - -<pre class="bash"> -set PERL5LIB=C:\foo\bar\baz;%PERL5LIB% -</pre> -<p>or</p> -<pre class="bash"> -export PERL5LIB=/foo/bar/baz:$PERL5LIB +return 1; </pre> -<p class=original>Once the Perl module is created and <code>perl</code> knows where to look for it, you can use the <code>require</code> built-in function to search for and execute it during a Perl script. For example, calling <code>require Mathematics::Powers</code> causes the Perl interpreter to search each directory listed in <code>PERL5LIB</code> in turn, looking for a file called <code>Mathematics/Powers.pm</code>. 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 <code>powers.pl</code> and read as follows:</p> -<p>Perlã¢ã¸ã¥ã¼ã«ãä½ããã¦ã<code>perl</code>ããããã©ãã«ããããç¥ã£ã¦ããã°ãçµè¾¼ã®<code>require</code>é¢æ°ã使ã£ã¦æ¢ããPerlã®ã¹ã¯ãªããä¸ã§å®è¡ãããã¨ãã§ãã¾ããä¾ãã°ã<code>require Mathematics::Powers</code>ãå¼ã¶ã¨ãPerlã¤ã³ã¿ã¼ããªã¿ã¯<code>PERL5LIB</code>ã«ãªã¹ãããã¦ãããã£ã¬ã¯ããªãé çªã«ã<code>Mathematics/Powers.pm</code>ã¨ãããã¡ã¤ã«ãæ¢ãã¾ããã¢ã¸ã¥ã¼ã«ããã¼ããããããçªç¶ã«ãµãã«ã¼ãã³ã¨å¤æ°ãã¡ã¤ã³ã¹ã¯ãªããã«å®ç¾©ããã¾ãããã®ä¾ã®ã¹ã¯ãªããã<code>powers.pl</code>ã¨å¼ã³ã¾ããããç¶ãã¦èªãã§ããã:</p> +<p class="original">And modify <code>main.pl</code> to read:</p> +<p>ããã¦ã<code>main.pl</code>ãå¤æ´ãã¾ã</p> <pre class="perl prettyprint"> use strict; use warnings; -require Mathematics::Powers; +require Demo::StringUtils; -print Mathematics::Powers::exp(2); # "7.3890461584" +print <ins>Demo::StringUtils::</ins>zombify("i want brains"); # "r wrnt brrrns" </pre> <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 <code>require Foo::Bar</code> <em>does not</em> look for and load a file with a <code>package Foo::Bar</code> declaration somewhere inside it. Calling <code>require Foo::Bar</code> <em>does not</em> necessarily load subroutines or package variables in the <code>Foo::Bar</code> namespace. Calling <code>require Foo::Bar</code> merely loads a file called <code>Foo/Bar.pm</code>, which need not have <em>any</em> kind of package declaration inside it at all, and in fact might declare <code>package Baz::Qux</code> and other nonsense inside it for all you know.</p> -<p>ããã±ã¼ã¸ã¨ã¢ã¸ã¥ã¼ã«ã®2ã¤ã¯ãããã°ã©ãã³ã°è¨èªPerlã«ããã¦å®å ¨ã«åããã¦ãããåºå¥ãããæ©è½ã§ãããã®2ã¤ãåãããã«ã³ãã³ã®ããªãã¿ã使ã£ã¦ãããã¨ã¯ãéè¦ãªã²ã£ããã§ããããã¯ãã¹ã¯ãªããã¾ãã¯ã¢ã¸ã¥ã¼ã«ã®ã³ã¼ã¹çµç±ã§ããã±ã¼ã¸ãè¤æ°ååãæ¿ãããã¨ãã§ããã¾ããè¤æ°ã®ãã¡ã¤ã«å ã®è¤æ°ã®å ´æã§åä¸ã®ããã±ã¼ã¸å®£è¨ã使ããã¨ãã§ãã¾ãã<code>require Foo::Bar</code>ãå¼ã¶ãã¨ã¯<code>Foo::Bar</code>åå空éã«ãããµãã«ã¼ãã³ãããã±ã¼ã¸å¤æ°ãå¿ ããããã¼ã<em>ãã¾ãã</em>ã<code>require Foo::Bar</code>ãå¼ã¶ãã¨ã¯ãåã«<code>Foo/Bar.pm</code>ã¨ãããã¡ã¤ã«ããã¼ãããã ãã§ããããã®ãã¡ã¤ã«ã«ã<em>ã©ã®ãããª</em>種é¡ã®ããã±ã¼ã¸å®£è¨ãå¿ è¦æãã¾ãããå®éã«ã¯ã<code>package Baz::Qux</code>ã®ãã㪠æå³ã®ãªããã®ã宣è¨ã§ãã¾ãã</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 <code>require Foo::Bar</code> <em>does not</em> look for and load a file with a <code>package Foo::Bar</code> declaration somewhere inside it, nor does it necessarily load subroutines in the <code>Foo::Bar</code> namespace. Calling <code>require Foo::Bar</code> merely loads a file called <code>Foo/Bar.pm</code>, which need not have <em>any</em> kind of package declaration inside it at all, and in fact might declare <code>package Baz::Qux</code> and other nonsense inside it for all you know.</p> +<p>ããã±ã¼ã¸ã¨ã¢ã¸ã¥ã¼ã«ã®2ã¤ã¯ãããã°ã©ãã³ã°è¨èªPerlã«ããã¦å®å ¨ã«åããã¦ãããåºå¥ãããæ©è½ã§ãããã®2ã¤ãåãããã«ã³ãã³ã®ããªãã¿ã使ã£ã¦ãããã¨ã¯ãéè¦ãªã²ã£ããã§ããããã¯ãã¹ã¯ãªããã¾ãã¯ã¢ã¸ã¥ã¼ã«ã®ã³ã¼ã¹çµç±ã§ããã±ã¼ã¸ãè¤æ°ååãæ¿ãããã¨ãã§ããã¾ããè¤æ°ã®ãã¡ã¤ã«å ã®è¤æ°ã®å ´æã§åä¸ã®ããã±ã¼ã¸å®£è¨ã使ããã¨ãã§ãã¾ãã<code>require Foo::Bar</code>ãå¼ã¶ãã¨ã¯<code>Foo::Bar</code>åå空éã«ãããµãã«ã¼ãã³ãããã±ã¼ã¸å¤æ°ãå¿ ããããã¼ã<em>ãã¾ãã</em>ãã<code>Foo::Bar</code>åå空éã®ãµãã«ã¼ãã³ãå¿ ããããã¼ãããããã§ã¯ããã¾ããã<code>require Foo::Bar</code>ãå¼ã¶ãã¨ã¯ãåã«<code>Foo/Bar.pm</code>ã¨ãããã¡ã¤ã«ããã¼ãããã ãã§ããããã®ãã¡ã¤ã«ã«ã<em>ã©ã®ãããª< /em>種é¡ã®ããã±ã¼ã¸å®£è¨ãå¿ è¦æãã¾ãããå®éã«ã¯ã<code>package Baz::Qux</code>ã®ãããªãã³ã»ã³ã¹ãªå®£è¨ãã§ãã¾ãã</p> <p class=original>Likewise, a subroutine call <code>Baz::Qux::processThis()</code> need not necessarily have been declared inside a file named <code>Baz/Qux.pm</code>. It could have been declared <em>literally anywhere</em>.</p> <p>ãã®ä¸ã<code>Baz::Qux::processThis()</code>ã®ãµãã«ã¼ãã³å¼ã³åºãã¯ã<code>Baz/Qux.pm</code>ã¨ããååã®ãã¡ã¤ã«å ã«å®£è¨ããã¦ããå¿ è¦ããããã¾ããã<em>æåéããã©ãã«ã§ã</em>å®ç¾©ãããã¨ãã§ãã¾ãã</p> @@ -1650,35 +1670,39 @@ <p>ãã®2ã¤ã®ã³ã³ã»ãããåãã¦ãã¾ã£ããã¨ã¯ãPerlã®æãæããªç¹å¾´ã®1ã¤ã§ãããããã2ã¤ã®ã³ã³ã»ãããåãã¦åãæ±ã£ã¦ãã¾ãã¨å¸¸ã«ãã«ãªã¹ã§æ°ã®çã£ããããªã³ã¼ãã¨ãªãã¾ãã幸éãªãã¨ã«ãPerlããã°ã©ãã®å¤ãã¯ã次ã®2ã¤ã®è¦åã«å¾ã£ã¦ãã¾ã:</p> <ol class="original"> <li><strong>A Perl script (<code>.pl</code> file) must always contain exactly zero <code>package</code> declarations.</strong></li> - <li><strong>A Perl module (<code>.pm</code> file) must always contain exactly one <code>package</code> declaration, corresponding exactly to its name and location.</strong> E.g. module <code>Mathematics/Powers.pm</code> must begin with <code>package Mathematics::Powers</code>.</li> + <li><strong>A Perl module (<code>.pm</code> file) must always contain exactly one <code>package</code> declaration, corresponding exactly to its name and location.</strong> E.g. module <code>Demo/StringUtils.pm</code> must begin with <code>package Demo::StringUtils</code>.</li> </ol> <ol> <li><strong>Perlã¹ã¯ãªãã(<code>.pl</code>ãã¡ã¤ã«)ã¯ã1ã¤ã®<code>package</code>宣è¨ãå«ã¾ãªãã</strong></li> - <li><strong>Perlã¢ã¸ã¥ã¼ã«(<code>.pm</code>ãã¡ã¤ã«)ã¯ã1ã¤ã®<code>package</code>宣è¨ãå«ã¿ãååã¨å ´æãä¸è´ãã¦ããã</strong> ä¾: ã¢ã¸ã¥ã¼ã«<code>Mathematics/Powers.pm</code>ã¯ã<code>package Mathematics::Powers</code>ã§å§ã¾ã£ã¦ããã</li> + <li><strong>Perlã¢ã¸ã¥ã¼ã«(<code>.pm</code>ãã¡ã¤ã«)ã¯ã1ã¤ã®<code>package</code>宣è¨ãå«ã¿ãååã¨å ´æãä¸è´ãã¦ããã</strong> ä¾: ã¢ã¸ã¥ã¼ã«<code>Demo/StringUtils.pm</code>ã¯ã<code>package Demo::StringUtils</code>ã§å§ã¾ã£ã¦ããã</li> </ol> <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> -<h2 class="oritingal">Object-oriented Perl</h2> +<h2 class="original">Object-oriented Perl</h2> <h2>ãªãã¸ã§ã¯ãæåPerl</h2> <p class="orignal">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 <code>bless</code>. To find out what class a reference's referent belongs to (if any), use <code>ref</code>.</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 <code>$obj->method()</code>; class methods are invoked using <code>Package::Name->method()</code>.</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. ã¹ã«ã©å¤æ°)ããã®ãªãã¡ã¬ã³ããã¯ã©ã¹ã«å±ãã¦ãããã¨ããªãã¡ã¬ã³ã¹ã«æããããã«ã<code>bless</code>ã使ãã¾ããblessã«ãããã©ã®ã¯ã©ã¹ãã©ã®ãªãã¡ã¬ã³ãã«å±ããã®ããåããããã«ãªãã¾ãããªãã¡ã¬ã³ã¹ã®ãªãã¡ã¬ã³ããã©ã®ã¯ã©ã¹ã«å±ãã¦ããããã¿ã¤ããããã«ã¯ã<code>ref</code>ã使ãã¾ã</p></li></li> - <li><p><i>ã¡ã½ãã</i>ã¯åç´ã«ç¬¬ä¸å¼æ°ãªãã¸ã§ã¯ã(ã¾ãã¯ãã¯ã©ã¹ã¡ã½ããã§ããã°ãããã±ã¼ã¸å)ã§ãããµãã«ã¼ãã³ã§ãããªãã¸ã§ã¯ãã¡ã½ããã¯ã<code>$obj->method()</code>ã使ã£ã¦å¼ã³åºããã¾ã; ã¯ã©ã¹ã¡ã½ããã¯<code>Package::Name->method()</code>ã§ãã</p></li> - <li><p><i>ã¯ã©ã¹</i>ã¯åç´ã«ã¡ã½ãããå«ãããã±ã¼ã¸ã§ãã</p></li> + <li> + <p class="original">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 <code>bless</code>. To find out what class a reference's referent belongs to (if any), use <code>ref</code>.</p> + <p><i>ãªãã¸ã§ã¯ã</i>ã¯åç´ã«ãã©ã®ã¯ã©ã¹ã«å±ãã¦ãããã示ãã¦ãããªãã¡ã¬ã³ã¹ã§ã(i.e. ã¹ã«ã©å¤æ°)ããã®ãªãã¡ã¬ã³ããã¯ã©ã¹ã«å±ãã¦ãããã¨ããªãã¡ã¬ã³ã¹ã«æããããã«ã<code>bless</code>ã使ãã¾ããblessã«ãããã©ã®ã¯ã©ã¹ãã©ã®ãªãã¡ã¬ã³ãã«å±ããã®ããåããããã«ãªãã¾ãããªãã¡ã¬ã³ã¹ã®ãªãã¡ã¬ã³ããã©ã®ã¯ã©ã¹ã«å±ãã¦ããããã¿ã¤ããããã«ã¯ã<code>ref</code>ã使ãã¾ã</p> + </li> + <li> + <p class="original">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 <code>$obj->method()</code>; class methods are invoked using <code>Package::Name->method()</code>.</p> + <p><i>ã¡ã½ãã</i>ã¯åç´ã«ç¬¬ä¸å¼æ°ãªãã¸ã§ã¯ã(ã¾ãã¯ãã¯ã©ã¹ã¡ã½ããã§ããã°ãããã±ã¼ã¸å)ã§ãããµãã«ã¼ãã³ã§ãããªãã¸ã§ã¯ãã¡ã½ããã¯ã<code>$obj->method()</code>ã使ã£ã¦å¼ã³åºããã¾ã; ã¯ã©ã¹ã¡ã½ããã¯<code>Package::Name->method()</code>ã§ãã</p> + </li> + <li> + <p class="original">A <i>class</i> is simply a package that happens to contain methods.</p> + <p><i>ã¯ã©ã¹</i>ã¯åç´ã«ã¡ã½ãããå«ãããã±ã¼ã¸ã§ãã</p> + </li> </ul> -<p class="original">A quick example. An example module <code>Animals/Animals.pm</code> containing a class <code>Animals::Animal</code> reads like this:</p> -<p>以ä¸ãç°¡åãªä¾ã§ãã ä¾ã¨ãã¦ã<code>Animal</code>ã¯ã©ã¹ãå«ãã¢ã¸ã¥ã¼ã«<code>Animal.pm</code>ã¯ã次ã®ããã«ãªãã¾ã:</p> +<p class="original">A quick example makes this clearer. An example module <code>Animal.pm</code> containing a class <code>Animal</code> reads like this:</p> +<p>以ä¸ãç°¡åãªä¾ã§ãããã¯ã£ããããã¾ãã ä¾ã®ã¢ã¸ã¥ã¼ã«ã¨ãã¦<code>Animal.pm</code>ã®ã¯ã©ã¹<code>Animal</code>ã¯ã次ã®ããã«ãªãã¾ã:</p> <pre class="perl prettyprint"> use strict; @@ -1688,10 +1712,10 @@ sub eat { # First argument is always the object to act upon. - my $self = shift; - + my $self = shift @_; + foreach my $food ( @_ ) { - if($self->canEat($food)) { + if($self->can_eat($food)) { print "Eating ", $food; } else { print "Can't eat ", $food; @@ -1700,52 +1724,51 @@ } # For the sake of argument, assume an Animal can eat anything. -sub canEat { +sub can_eat { return 1; } -1; +return 1; </pre> -<p class="original">And a Perl script making use of this class might read:</p> -<p>ãã®ã¯ã©ã¹ã使ãPerlã¹ã¯ãªããã¯æ¬¡ã®ããã«ãªãã§ããã:</p> +<p class="original">And we might make use of this class like so:</p> +<p>ãã®ã¯ã©ã¹ã使ã次ã®ããã«ä½¿ãã§ããã:</p> <pre class="perl prettyprint"> -use strict; -use warnings; - require Animal; -my $animal = {}; # $animal is an ordinary hash reference -print ref $animal; # "HASH" -bless $animal, "Animals::Animal"; # now it is an object of class "Animal" -print ref $animal; # "Animal" - -$animal->eat("insects", "curry", "salmon"); +my $animal = { + "legs" => 4, + "colour" => "brown", +}; # $animal is an ordinary hash reference +print ref $animal; # "HASH" +bless $animal, "Animal"; # now it is an object of class "Animal" +print ref $animal; # "Animal" </pre> -<p class="original">This final call is equivalent to <code>Animals::Animal::eat($animal, "insects", "curry", "salmon")</code>.</p> -<p>æå¾ã®å¼ã³åºãã¯ã<code>Animals::Animal::eat($animal, "insects", "curry", "salmon")</code>ã¨åãã§ãã</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> <p class="original">You can still work with the original hash in the usual way:</p> +<p>ã¾ã ãé常ã®ããæ¹ã§ãªãªã¸ãã«ã®ããã·ã¥ãæä½ã§ãã¾ã</p> <pre class="perl prettyprint"> print "Animal has ", $animal->{"legs"}, " leg(s)"; </pre> -<p class="original">You can also call methods on the object using the same -> operator, like so:</p> + +<p class="original">You can also call methods on the object using the same <code>-></code> operator, like so:</p> +<p>åã<code>-></code>ãªãã¬ã¼ã¿ã§ãªãã¸ã§ã¯ãããã¡ã½ãããå¼ã¶ãã¨ãã§ãã¾ã:</p> <pre class="perl prettyprint"> -$animal->eat("insects", "curry", "eucalyptus"); +$animal->eat("insects", "curry", "eucalyptus"); </pre> -<p class="original">This final call is equivalent to Animal::eat($animal, "insects", "curry", "eucalyptus").</p> +<p class="original">This final call is equivalent to <code>Animal::eat($animal, "insects", "curry", "eucalyptus")</code>.</p> +<p>æå¾ã®å¼ã³åºãã¯ã<code>Animal::eat($animal, "insects", "curry", "eucalyptus")</code>ã¨åãã§ãã</p> <h3 class="original">Constructors</h3> <h3>ã³ã³ã¹ãã©ã¯ã¿</h3> <p class=original>A constructor is a class method which returns a new object. If you want one, just declare one. You can use any name you like. For class methods, the first argument passed is not an object but a class name. In this case, <code>"Animal"</code>:</p> <p>ã³ã³ã¹ãã©ã¯ã¿ã¯ã¯ã©ã¹ã¡ã½ããã§ãæ°ãããªãã¸ã§ã¯ããè¿ãã¾ããã³ã³ã¹ãã©ã¯ã¿ã欲ãããã°ãããã宣è¨ããã ãã§ãã好ããªååã使ãã¾ããã¯ã©ã¹ã¡ã½ããã«ã¯ãæåã®å¼æ°ã¨ãã¦ããªãã¸ã§ã¯ãã§ã¯ãªãã¯ã©ã¹åã渡ãã¾ãããã®ã±ã¼ã¹ã§ã¯ã<code>"Animal"</code>ã§ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> use strict; use warnings; @@ -1761,7 +1784,7 @@ <p class=original>And then use it like so:</p> <p>次ã®ããã«ä½¿ãã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> my $animal = Animal->new(); </pre> @@ -1792,19 +1815,6 @@ return 1; </pre> -<p>And some sample code:</p> - -<pre class="perl"> -use strict; -use warnings; - -require Koala; - -my $koala = Koala->new(); - -$koala->eat("insects", "curry", "eucalyptus"); # eat only the eucalyptus -</pre> - <p class="original">And some sample code:</p> <p>ãµã³ãã«ã³ã¼ã:</p> @@ -1819,8 +1829,8 @@ $koala->eat("insects", "curry", "eucalyptus"); # eat only the eucalyptus </pre> -<p class="original">This final method call tries to invoke <code>Koala::eat($bear, "insects", "curry", "eucalyptus")</code>, but a subroutine <code>eat()</code> isn't defined in the <code>Koala</code> package. However, because <code>@ISA</code> has been populated with a parent package <code>Animal</code>, the Perl interpreter tries calling <code>Animal::eat($bear, "insects", "curry", "eucalyptus")</code> instead, which works. Note how the class <code>Animal</code> had to be loaded explicitly by <code>Koala</code>.</p> -<p>æå¾ã®ã¡ã½ããå¼ã³åºãã¯ã<code>Koala::eat($bear, "insects", "curry", "eucalyptus")</code>ãå¼ã¼ãã¨ãã¾ããããµãã«ã¼ãã³<code>eat()</code>ã¯ã<code>Koala</code> ããã±ã¼ã¸ã«ã¯å®ç¾©ããã¦ãã¾ãããã§ããã<code>@Koala::ISA</code>ã«ããã±ã¼ã¸<code>Animal</code>ãããã®ã§ãPerlã¤ã³ã¿ããªã¿ã¯ <code>Animal::eat($bear, "insects", "curry", "eucalyptus")</code>ã代ããã«å¼ã¼ãã¨ãã¦ã代ããã«ããã¯åãã¾ãã次ã®ãã¨ã«æ³¨æãã¦ãã ãããã¯ã©ã¹<code>Animal</code>ã¯<code>Koala</code>ã§æ示çã«ãã¼ãããã¦ããªããã°ããã¾ããã</p> +<p class="original">This final method call tries to invoke <code>Koala::eat($koala, "insects", "curry", "eucalyptus")</code>, but a subroutine <code>eat()</code> isn't defined in the <code>Koala</code> package. However, because <code>@Koala::ISA</code> has been populated with a parent class <code>Animal</code>, the Perl interpreter tries calling <code>Animal::eat($koala, "insects", "curry", "eucalyptus")</code> instead, which works. Note how the class <code>Animal</code> had to be loaded explicitly by <code>Koala.pm</code>.</p> +<p>æå¾ã®ã¡ã½ããå¼ã³åºãã¯ã<code>Koala::eat($koala, "insects", "curry", "eucalyptus")</code>ãå¼ã¼ãã¨ãã¾ããããµãã«ã¼ãã³<code>eat()</code>ã¯ã<code>Koala</code> ããã±ã¼ã¸ã«ã¯å®ç¾©ããã¦ãã¾ãããã§ããã<code>@Koala::ISA</code>ã«è¦ªã¯ã©ã¹<code>Animal</code>ãããã®ã§ãPerlã¤ã³ã¿ããªã¿ã¯ <code>Animal::eat($koala, "insects", "curry", "eucalyptus")</code>ã代ããã«å¼ã¼ãã¨ãã¦ã代ããã«ããã¯åãã¾ãã次ã®ãã¨ã«æ³¨æãã¦ãã ãããã¯ã©ã¹<code>Animal</code>ã¯<code>Koala.pm</code>ã§æ示çã«ãã¼ãããã¦ããªããã°ããã¾ããã</p> <p class="original">Since <code>@ISA</code> is an array, Perl supports multiple inheritance, with all the benefits and horrors this entails.</p> <p><code>@ISA</code> ã¯é åãªã®ã§ãPerl ã¯å¤éç¶æ¿ããµãã¼ããã¾ããå ¨ã¦ã®å©çã¨ææãä¼´ãã¾ããã</p> @@ -1848,8 +1858,7 @@ <p class="original">A <code>BEGIN</code> block is always executed first. If you create multiple <code>BEGIN</code> blocks (don't), they are executed in order from top to bottom as the compiler encounters them. A <code>BEGIN</code> 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><code>BEGIN</code>ãããã¯ã¯å¸¸ã«ããï½é¬¼å®è¡ããã¾ããè¤æ°ã®<code>BEGIN</code>ãããã¯ãæ¸ãã¨(don't)ã ä¸ããé çªã«ã³ã³ãã¤ã©ããããã«åºä¼ãé çªã«å®è¡ããã¾ãã<code>BEGIN</code>ãããã¯ã¯å¸¸ã«æåã«å®è¡ããã¾ãããããBEGINãããã¯ãã¹ã¯ãªããã®éä¸(ãããªãã¨ã¯ããªãã§ãã ãã)ããæå¾(or this)ã«æ¸ããã¦ããããã¦ãããã</p> <p class="original">Because they are executed at compilation time, a <code>BEGIN</code> 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 <code>BEGIN</code> blocks in conditionals!</strong> If you want to do something conditionally at compile time, you need to put the conditional <em>inside</em> the <code>BEGIN</code> block:</p> -<p>ã³ã³ãã¤ã«æã«å®è¡ãããã®ã§ã<code>BEGIN</code>ãããã¯ãæ¡ä»¶ãããã¯ã®ä¸ã«ãã£ã¦ãã<em>ã¾ã </em>æåã«å®è¡ããã¾ãããã¨ããæ¡ä»¶ã®è©ä¾¡ãå½ã§ãããæ¡ä»¶ã<em>ã¾ã ã¾ã£ããè©ä¾¡ããã¦ããªã</em>ã«ãããããããå®éã«ã¯ã<em>è©ä¾¡ããããã¨ããªã</em>ã¨ãã¦ãã§ãã -<strong><code>BEGIN</code>ãããã¯ãæ¡ä»¶ã®ä¸ã«ç½®ãã¦ã¯è¡ãã¾ãã!</strong> ã³ã³ãã¤ã«æã«ä½ãããã®æ¡ä»¶ä»ãã®ãã¨ãããããã°ã<code>BEGIN</code>ãããã¯ã®<em>ä¸ã«</em>æ¡ä»¶æãç½®ããªããã°ãªãã¾ãã:</p> +<p>ã³ã³ãã¤ã«æã«å®è¡ãããã®ã§ã<code>BEGIN</code>ãããã¯ãæ¡ä»¶ãããã¯ã®ä¸ã«ãã£ã¦ãã<em>ã¾ã </em>æåã«å®è¡ããã¾ãããã¨ããæ¡ä»¶ã®è©ä¾¡ãå½ã§ãããæ¡ä»¶ã<em>ã¾ã ã¾ã£ããè©ä¾¡ããã¦ããªã</em>ã«ãããããããå®éã«ã¯ã<em>è©ä¾¡ããããã¨ããªã</em>ã¨ãã¦ãã§ãã<strong><code>BEGIN</code>ãããã¯ãæ¡ä»¶ã®ä¸ã«ç½®ãã¦ã¯è¡ãã¾ãã!</strong> ã³ã³ãã¤ã«æã«ä½ãããã®æ¡ä»¶ä»ãã®ãã¨ãããããã°ã<code>BEGIN</code>ãããã¯ã®<em>ä¸ã«</em>æ¡ä»¶æãç½®ããªããã°ãªãã¾ãã:</p> <pre class="perl prettyprint"> BEGIN { if($condition) { @@ -1864,37 +1873,37 @@ <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; +use Caterpillar ("crawl", "pupate"); +use Caterpillar (); +use Caterpillar; </pre> <p class="original">are respectively equivalent to:</p> <p>以ä¸ã¨ããããç価ã§ã:</p> <pre class="perl prettyprint"> BEGIN { - require Bugs::Caterpillar; - Bugs::Caterpillar->import("crawl", "pupate"); + require Caterpillar; + Caterpillar->import("crawl", "pupate"); } BEGIN { - require Bugs::Caterpillar; + require Caterpillar; } BEGIN { - require Bugs::Caterpillar; - Bugs::Caterpillar->import(); + require Caterpillar; + Caterpillar->import(); } </pre> <ul class="original"> <li>No, the three examples are not in the wrong order. It is just that Perl is dumb.</li> <li>A <code>use</code> call is a disguised <code>BEGIN</code> block. The same caveats apply. <code>use</code> statements must always be placed at the top of the file, and <strong>never inside conditionals</strong>.</li> - <li><code>import()</code> is not a built-in Perl function. It is a <strong>user-defined class method</strong>. The burden is on the programmer of the <code>Bugs::Caterpillar</code> package to define or inherit <code>import()</code>, and the method could theoretically accept anything as arguments and do anything with those arguments.</li> - <li>Notice how <code>require Bugs::Caterpillar</code> loads a <strong>module</strong> named <code>Bugs/Caterpillar.pm</code>, whereas <code>Bugs::Caterpillar->import()</code> calls the <code>import()</code> subroutine that was defined inside the <code>Bugs::Caterpillar</code> <strong>package</strong>. Let's hope the module and the package coincide!</li> + <li><code>import()</code> is not a built-in Perl function. It is a <strong>user-defined class method</strong>. The burden is on the programmer of the <code>Caterpillar</code> package to define or inherit <code>import()</code>, and the method could theoretically accept anything as arguments and do anything with those arguments. Consult the documentation of <code>Caterpillar.pm</code> to find out exactly what will happen.</li> + <li>Notice how <code>require Caterpillar</code> loads a <strong>module</strong> named <code>Caterpillar.pm</code>, whereas <code>Caterpillar->import()</code> calls the <code>import()</code> subroutine that was defined inside the <code>Caterpillar</code> <strong>package</strong>. Let's hope the module and the package coincide!</li> </ul> <ul> <li>ããããï¼ã¤ã®ä¾ã¯ééã£ãé çªã§ã¯ããã¾ãããPerlã馬鹿ãªã ãã§ãã</li> <li><code>use</code>ã®å¼ã³åºãã¯<code>BEGIN</code>ãããã¯ã«å¤æããã¾ããåãè¦åãé©ç¨ããã¾ãã<code>use</code>ã¹ãã¼ãã¡ã³ãã¯ãã¡ã¤ã«ã®æåã«ç½®ãããªããã°ããã¾ããã<strong>æ¡ä»¶ã®ä¸ã«ç½®ãã¦ã¯ããã¾ãã</strong>ã</li> - <li><code>import()</code> ã¯Perlã®çµè¾¼ã®é¢æ°ã§ã¯ããã¾ããã<strong>ã¦ã¼ã¶ã¼å®ç¾©ã®ã¯ã©ã¹ã¡ã½ããã§ã</strong>ã<code>import()</code>ãå®ç¾©ããããç¶æ¿ãããã®ç¾©åã¯<code>Bugs::Caterpillar</code>ããã±ã¼ã¸ã®ããã°ã©ãã«ãããã¡ã½ããã¯çè«ä¸ãã©ã®ãããªå¼æ°ã§ãåãå ¥ããã©ã®ãããªãã¨ã§ãã§ãã¾ãã</li> - <li><code>require Bugs::Caterpillar</code>ã<code>Bugs/Caterpillar.pm</code>ã¨ããååã®<strong>module</strong>ããã¼ããããã¨ã«æ³¨æãã¦ãã ããã<code>Bugs::Caterpillar->import()</code>ã¯ã<code>Bugs::Caterpillar</code> <strong>package</strong>å ã«å®ç¾©ããã<code>import()</code>ãµãã«ã¼ãã³ãå¼ã³ã¾ããã¢ã¸ã¥ã¼ã«ã¨ããã±ã¼ã¸ãä¸è´ãã¦ãããã¨ãæå¾ ãã¾ããã!</li> + <li><code>import()</code> ã¯Perlã®çµè¾¼ã®é¢æ°ã§ã¯ããã¾ããã<strong>ã¦ã¼ã¶ã¼å®ç¾©ã®ã¯ã©ã¹ã¡ã½ããã§ã</strong>ã<code>import()</code>ãå®ç¾©ããããç¶æ¿ãããã®ç¾©åã¯<code>Caterpillar</code>ããã±ã¼ã¸ã®ããã°ã©ãã«ãããã¡ã½ããã¯çè«ä¸ãã©ã®ãããªå¼æ°ã§ãåãå ¥ããã©ã®ãããªãã¨ã§ãã§ãã¾ãã<code>Captepillar.pm</code>ã®ããã¥ã¡ã³ããèªãã§ãä½ãèµ·ãããæ£ç¢ºã«èª¿æ»ãã¦ãã ããã</li> + <li><code>require Caterpillar</code>ã<code>Caterpillar.pm</code>ã¨ããååã®<strong>module</strong>ããã¼ããããã¨ã«æ³¨æãã¦ãã ããã<code>Caterpillar->import()</code>ã¯ã<code>Caterpillar</code> <strong>package</strong>å ã«å®ç¾©ããã<code>import()</code>ãµãã«ã¼ãã³ãå¼ã³ã¾ããã¢ã¸ã¥ã¼ã«ã¨ããã±ã¼ã¸ãä¸è´ãã¦ãããã¨ãæå¾ ãã¾ããã!</li> </ul> <h2>Exporter</h2> @@ -1902,7 +1911,7 @@ <p class="original">The most common way to define an <code>import()</code> method is to inherit it from the <a href="http://perldoc.perl.org/Exporter.html">Exporter</a> module. Exporter is a core module, and a <i>de facto</i> core feature of the Perl programming language. In Exporter's implementation of <code>import()</code>, the list of arguments that you pass in is interpreted as a list of subroutine names. When a subroutine is <code>import()</code>ed, it becomes available in the current package as well as in its own original package.</p></p> <p><code>import()</code>ã¡ã½ãããå®ç¾©ãã常èçãªæ¹æ³ã¯ Exporter ã¢ã¸ã¥ã¼ã«ããç¶æ¿ãããã¨ã§ããExporter ã¯ã³ã¢ã¢ã¸ã¥ã¼ã«ã§ãããã°ã©ãã³ã°è¨èªPerlã®<i>ããã¡ã¯ã</i>ã®ã³ã¢ã®æ©è½ã§ããExporterã®<code>import</code>ã®å®è£ ã§ã渡ããå¼æ°ã®ãªã¹ãã¯ããµãã«ã¼ãã³ã®ååã¨ãã¦è§£éããã¾ãããµãã«ã¼ãã³ã<code>import()</code>ãããã¨ãç¾å¨ã®åå空éã§ããã®ãªãªã¸ãã«ã®åå空éã«ãããã®ã¨åãããã«ãå©ç¨å¯è½ã«ãªãã¾ãã</p> -<p class="original">This concept is easiest to grasp using an example. Here's what <code>Bugs/Caterpillar.pm</code> looks like:</p> +<p class="original">This concept is easiest to grasp using an example. Here's what <code>Caterpillar.pm</code> looks like:</p> <p>ãã®ã³ã³ã»ããã¯ä¾ã使ãã¨ææ¡ããããã§ãããã<code>Caterpillar.pm</code>ã¯æ¬¡ã®ãããªãã®ã§ã:</p> <pre class="perl prettyprint"> @@ -1925,7 +1934,7 @@ </pre> <p class="original">And here's a script which makes use of the <code>Caterpillar.pm</code> module:</p> -<p><code>Bugs/Caterpillar.pm</code>ã¢ã¸ã¥ã¼ã«ã使ã£ãã¹ã¯ãªããã§ã:</p> +<p><code>Caterpillar.pm</code>ã¢ã¸ã¥ã¼ã«ã使ã£ãã¹ã¯ãªããã§ã:</p> <pre class="perl prettyprint"> use strict; @@ -1942,107 +1951,77 @@ <p class="original">The package variable <code>@EXPORT_OK</code> can be populated with a list of all subroutines which the user can import explicitly by passing subroutine names to <code>import()</code>. If <code>import()</code> is called with the name of a subroutine not in this list, a runtime error will occur. For example, try <code>use Caterpillar ("pupate")</code>.</p> -<p>ããã±ã¼ã¸å¤æ°<code>@EXPORT</code>ã¯ããã©ã«ãã§ã¨ã¯ã¹ãã¼ãããããµãã«ã¼ãã³ã®ãªã¹ããå ¥ãã¾ãã<code>import()</code> ãå¼æ°ãªãã§å¼ã°ããå ´åã«ããã®ä¾ã§èµ·ãããã¨ãèµ·ãã¾ãã<code>import()</code>ããªã¹ãã«åå¨ããªãååã¨ä¸ç·ã«ãã°ããããå®è¡æã¨ã©ã¼ãèµ·ãã¾ãã試ãã«<code>use Caterpillar ("pupate")</code>ã試ãã¦ã¿ã¦ãã ããã</p> +<p>ããã±ã¼ã¸å¤æ°<code>@EXPORT_OK</code>ã¯ã¦ã¼ã¶ã¼ãã<code>import()</code>ã«æ¸¡ãããã¤ã³ãã¼ãåºæ¥ãå ¨ã¦ã®ãµãã«ã¼ãã³ã®ãªã¹ããå ¥ãããã¾ããããã<code>import()</code>ããªã¹ãã«åå¨ããªãååã¨ä¸ç·ã«ãã°ããããå®è¡æã¨ã©ã¼ãèµ·ãã¾ãã試ãã«<code>use Caterpillar ("pupate")</code>ã試ãã¦ã¿ã¦ãã ããã</p> + +<p class="original">The package variable <code>@EXPORT</code> can be populated with a list of subroutines to be exported by default. These are exported if <code>import()</code> is called with no arguments at all, which is what happens in this example.</p> +<p>ããã±ã¼ã¸å¤æ°<code>@EXPORT</code>ã¯ããã©ã«ãã§ã¨ã¯ã¹ãã¼ãããããµãã«ã¼ãã³ã®ãªã¹ããå ¥ãã¾ãã<code>import()</code> ãå¼æ°ãªãã§å¼ã°ããå ´åã«ããã®ä¾ã§èµ·ãããã¨ãèµ·ãã¾ãã</p> <p class=original>>As a result of being <code>import()</code>ed, a method such as <code>Caterpillar::crawl()</code> become available without qualification as <code>crawl()</code>. This saves typing. (Note: regardless of the content of <code>@EXPORT_OK</code>, every method can always be called "longhand", as shown above. There are no private methods in Perl. Customarily, a method intended for private use is named with a leading underscore or two.)</p> <p><code>import()</code>ãããçµæã¨ãã¦ã<code>Caterpillar::crawl()</code>ã®ãããªã¡ã½ããã<code>crawl()</code>ã®ããã«ã修飾ãªãã«å©ç¨å¯è½ã«ãªãã¾ããããã¯ãã¿ã¤ãæ°ã®ç¯ç´ã«ãªãã¾ãã(注æ: <code>@EXPORT_OK</code>ã®ã³ã³ã»ããã«ãããããã å ¨ã¦ã®ã¡ã½ããã¯ãä¸ã§è¦ãããã«ã"longhand"ã§å¼ã¶ãã¨ãåºæ¥ã¾ãã Perlã«ã¯ãã©ã¤ãã¼ãã¡ã½ããã¯ããã¾ãããéä¾ã1ã¤ã2ã¤ã®ã¢ã³ãã¼ã¹ã³ã¢ããå§ã¾ãååãä»ããããã¡ã½ããã¯ãã©ã¤ãã¼ããæå³ãã¾ãã</p> -<p class="original"><strong>A caution.</strong> Notice how <code>crawl()</code> was neither defined in the script, nor explicitly <code>import()</code>ed from another file with e.g. <code>use Caterpillar ("crawl")</code>. Suppose the middle three lines weren't there to provide clues, and suppose there were a dozen <code>use</code> calls alongside <code>use Caterpillar</code>. And remember that any module is free to have more <code>use</code> calls of its own. In such a situation, it is extremely difficult to locate the place where <code>crawl()</code> was originally defined. The moral of this story is twofold:</p> +<p class="original">Note how, again, we are back in a situation where without other clues it might not be easy to tell where <code>crawl()</code> was originally defined. The moral of this story is twofold:</p> +<p>å度注æãã¦ããã¨ãä»ã®ãã®ãªãã·ãã¥ã¨ã¼ã·ã§ã³ã«æ»ãã¨ã<code>crawl()</code>ãå ã ã©ãã§å®ç¾©ããã¦ããã®ããæããã®ã¯ç°¡åã§ã¯ããã¾ããããã®è©±ã®æè¨ã¯2ã¤ããã¾ã:</p> -<p><strong>注æã</strong><code>crawl()</code>ã¯ã¹ã¯ãªããã§å®ç¾©ããã¦ããããæ示çã«ä»ã®ãã¡ã¤ã«ãã<code>import()</code>ããã¾ãããe.g. <code>use Caterpillar ("crawl")</code>ãçãä¸ã®ï¼è¡ã®ããã«ãã³ããæä¾ããã<code>use Caterpillar</code>ã¨ä¸¦è¡ãã¦ã12ã®<code>use</code>å¼ã³åºãããã£ãã¨ããã¨ã ã¾ããã©ã®ã¢ã¸ã¥ã¼ã«ãèªç±ã«è¤æ°ã®<code>use</code>å¼ã³åºããããèªèº«ã§ã§ãããã¨ãæãèµ·ããã¨ããã®ãããªç¶æ³ã§ã¯ã -<code>crawl()</code>ãå ã å®ç¾©ããã¦ããå ´æãè¦ã¤ããã®ã¯å®¹æã§ã¯ããã¾ããããã®è©±ã®æè¨ã¯2ã¤ããã¾ã:</p> - -<ol class="original"> -<li><p>When creating a module which makes use of Exporter, never use <code>@EXPORT</code> to export subroutines by default. Always make the user call subroutines "longhand" or <code>import()</code> them explicitly (using e.g. <code>use Caterpillar ("crawl")</code>, which is a strong clue to look in <code>Bugs/Caterpillar.pm</code> for the definition of <code>crawl()</code>).</p></li> -<li><p>When <code>use</code>ing a module, always explicitly name the subroutines you want to <code>import()</code>. If you don't want to <code>import()</code> any subroutines and wish to refer to them longhand, you must supply an explicit empty list: <code>use Caterpillar ()</code>.</p></li> -</ol> - -<ol class="original"> -<li><p>When creating a module which makes use of Exporter, never use <code>@EXPORT</code> to export subroutines by default. Always make the user call subroutines "longhand" or <code>import()</code> them explicitly (using e.g. <code>use Caterpillar ("crawl")</code>, which is a strong clue to look in <code>Bugs/Caterpillar.pm</code> for the definition of <code>crawl()</code>).</p></li> -<li><p>When <code>use</code>ing a module, always explicitly name the subroutines you want to <code>import()</code>. If you don't want to <code>import()</code> any subroutines and wish to refer to them longhand, you must supply an explicit empty list: <code>use Caterpillar ()</code>.</p></li> -</ol> <ol> -<li><p>Exporterã使ãã¢ã¸ã¥ã¼ã«ãä½ãéã«ã¯ãããã©ã«ãã§ãµãã«ã¼ãã³ãã¨ã¯ã¹ãã¼ããã<code>@EXPORT</code>ã使ããªããã¨ã常ã«ãã¦ã¼ã¶ã¼ã«ã"longhand"ã§ãµãã«ã¼ãã³ãå¼ã°ããããæ示çã«<code>import()</code>ããããã¨(e.g. <code>use Caterpillar ("crawl")</code>ã使ã£ã¦ã<code>Bugs/Caterpillar.pm</code>ã«<code>crawl()</code>)ã®å®ç¾©ãããã¨ããå¼·ããã³ãã«ãªãã¾ãã</p></li> -<li><p>ã¢ã¸ã¥ã¼ã«ã<code>use</code>ããã¨ã常ã«æ示çã«<code>import()</code>ããããµãã«ã¼ãã³ã®ååæ¸ãã¾ããä½ã®ãµãã«ã¼ãã³ã<code>import()</code>ããããªããlonghandã§åç §ãããã®ãªããããã®ãªã¹ããæä¾ããªããã°ããã¾ããã: <code>use Caterpillar ()</code>.</p></li> +<li> + <p class="original">When creating a module which makes use of Exporter, never use <code>@EXPORT</code> to export subroutines by default. Always make the user call subroutines "longhand" or <code>import()</code> them explicitly (using e.g. <code>use Caterpillar ("crawl")</code>, which is a strong clue to look in <code>Caterpillar.pm</code> for the definition of <code>crawl()</code>).</p> + <p>Exporterã使ãã¢ã¸ã¥ã¼ã«ãä½ãéã«ã¯ãããã©ã«ãã§ãµãã«ã¼ãã³ãã¨ã¯ã¹ãã¼ããã<code>@EXPORT</code>ã使ããªããã¨ã常ã«ãã¦ã¼ã¶ã¼ã«ã"longhand"ã§ãµãã«ã¼ãã³ãå¼ã°ããããæ示çã«<code>import()</code>ããããã¨(e.g. <code>use Caterpillar ("crawl")</code>ã使ã£ã¦ã<code>Caterpillar.pm</code>ã«<code>crawl()</code>)ã®å®ç¾©ãããã¨ããå¼·ããã³ãã«ãªãã¾ãã</p> +</li> +<li> + <p class="original">When <code>use</code>ing a module which makes use of Exporter, always explicitly name the subroutines you want to <code>import()</code>. If you don't want to <code>import()</code> any subroutines and wish to refer to them longhand, you must supply an explicit empty list: <code>use Caterpillar ()</code>.</p> + <p>ã¢ã¸ã¥ã¼ã«ã<code>use</code>ããã¨ã常ã«æ示çã«<code>import()</code>ããããµãã«ã¼ãã³ã®ååæ¸ãã¾ããä½ã®ãµãã«ã¼ãã³ã<code>import()</code>ããããªããlonghandã§åç §ãããã®ãªããããã®ãªã¹ããæä¾ããªããã°ããã¾ããã: <code>use Caterpillar ()</code>.</p></li> </ol> - <h2 class="original">Miscellaneous notes</h2> <h2>ãã®ä»ã®æ³¨æ</h2> -<p class="original">Perl provides a wide selection of quote-like operators in addition to what you've seen in these documents:</p> -<p>Perlã¯ã¯ã©ã¼ãã®ä»£ãããããæ¼ç®åã®ããã§ã«ãã®ããã¥ã¡ã³ãå ã§è¦ã¦ãããã®ã«å ãã¦ãå¤ãã®é¸æè¢ãæä¾ãã¦ãã¾ã:</p> -<ul class="original"> +<ul> <li> - <p>There's an alternate syntax, <code>qw{ }</code>, for declaring arrays. This often seen in <code>use</code> statements:</p> -<pre class="perl prettyprint"> -use Account qw{create open close suspend delete}; -</pre> + <p class="original">The core module <a href="http://perldoc.perl.org/Data/Dumper.html">Data::Dumper</a> can be used to output an arbitrary scalar variable to the screen. This is an essential debug tool.</p> + <p>ã³ã¢ã¢ã¸ã¥ã¼ã«<a href="http://perldoc.perl.org/Data/Dumper.html">Data::Dumper</a>ã¯ä»»æã®ã¹ã«ã©ã®å¤ãã¹ã¯ãªã¼ã³ã«åºåããã®ã«ä½¿ãã¾ããããã¯åºæ¬çãªãããã°ãã¼ã«ã§ãã</p> </li> <li> - <p><code>qr//</code> can be used to put a regex into a scalar variable. This is especially useful because recompiling a regular expression multiple times actually takes substantial time:</p> + <p class="original">There's an alternate syntax, <code>qw{ }</code>, for declaring arrays. This is often seen in <code>use</code> statements:</p> + <p>é åã宣è¨ããããã®ä»£ããã®ã·ã³ã¿ãã¯ã¹ã<code>qw{ }</code>ãããã¾ãã<code>use</code>ã¹ãã¼ãã¡ã³ãã§ããè¦ããã¾ã:</p> <pre class="perl prettyprint"> -my @capitals = ("Baton Rouge", "Indianapolis", "Columbus", "Montgomery", "Helena", "Denver", "Boise"); -my $regex = qr/^[B-H]/; -print join ", ", grep /$regex/, @capitals; +use Account qw{create open close suspend delete}; </pre> + <p class="original">There are <a href="http://perldoc.perl.org/perlop.html#Quote-and-Quote-like-Operators">many other quote-like operators</a>.</p> + <p><a href="http://perldoc.perl.org/perlop.html#Quote-and-Quote-like-Operators">å¤ãã®ä»ã®ã¯ã©ã¼ãã©ã¤ã¯ãªãªãã¬ã¼ã¿ãããã¾ã</a>ã</p> </li> <li> - <p><code>qx{ }</code> can be used instead of `backticks` to invoke a program and capture its output:</p> -<pre class="perl prettyprint"> -my $text = qx{perl anotherscript.pl foo bar baz}; -</pre> + <p class="original">In <code>=~ m//</code> and <code>=~ s///</code> operations, you can use braces instead of slashes as the regex delimiters. This is quite useful if your regex contains a lot of slashes, which would otherwise need escaping with backslashes. For example, <code>=~ m{///}</code> matches three literal forward slashes, and <code>=~ s{^https?://}{}</code> removes the protocol part of a URL.</p> + <p><code>=~ m//</code> 㨠<code>=~ s///</code> æ¼ç®åã§ãæ£è¦è¡¨ç¾ã®åºåãæåã«ã¹ã©ãã·ã¥ã®ä»£ããã«ãã¬ã¼ã¹ã使ãã¾ããããã¯ãæ£è¦è¡¨ç¾ãå¤ãã®ã¹ã©ãã·ã¥ãããã¯ã¹ã©ãã·ã¥ãå«ãå ´åã«ãé常ã«æç¨ã§ããä¾ãã°ã<code>m{///}</code>ã¯ãåæ¹ã®3æåã®ã¹ã©ãã·ã¥ã«ããããã¾ããã¾ãã<code>=~ s{^https?://}{}</code> ã¯ãURLãããããã³ã«é¨åãåé¤ãã¾ãã</p> </li> <li> - <p>And many more!</p> + <p class="original">Perl does have <code>CONSTANTS</code>. These are discouraged now, but weren't always. Constants are actually just subroutine calls with omitted brackets.</p> + <p>Perlã«ã¯<code>å®æ°</code>ãããã¾ãã æ°è½ã¡ãããããªãã®ã§ããã常ã«ã§ã¯ããã¾ãããå®æ°ã¯å®éã«ã¯ããã©ã±ãããçç¥ãããã ã®ãµãã«ã¼ãã³å¼ã³åºãã§ãã</p> </li> -</ul> -<ul> <li> - <p>代ããã®ã·ã³ã¿ãã¯ã¹ã¨ãã¦ã<code>qw{ }</code>ããé åã®å®£è¨ã®ããã«ããã¾ãã<code>use</code>ã¹ãã¼ãã¡ã³ãã§ããè¦ããã¾ã:</p> -<pre class="perl prettyprint"> -use Account qw{create open close suspend delete}; -</pre> + <p class="original">Sometimes people omit quotes around hash keys, writing <code>$hash{key}</code> instead of <code>$hash{"key"}</code>. They can get away with it because in this situation the bareword <code>key</code> occurs as the string <code>"key"</code>, as opposed to a subroutine call <code>key()</code>.</p> + <p>æã ãããã·ã¥ã®ãã¼ã§ã¯ã©ã¼ããçç¥ãã¦ã<code>$hash{key}</code>ã<code>$hash{"key"}</code>ã®ä»£ããã«ä½¿ãã¾ãããã®ç¶æ³ã§ã¯ã裸ã®<code>key</code>ã¯ã<code>"key"</code>ã«ãªãããã§ãã<code>key()</code>ã®ããã«ããã¨ããµãã«ã¼ãã³å¼ã³åºãã«ãªãã¾ãã</p> </li> <li> - <p><code>qr//</code>ã¯æ£è¦è¡¨ç¾ãã¹ã«ã©å¤æ°ã«ç½®ãã®ã«ä½¿ããã¾ããããã¯é常ã«ä¾¿å©ã§ãããªããªããæ£è¦è¡¨ç¾ã®åã³ã³ãã¤ã«ã«ã¯æéããããããã§ã:</p> -<pre class="perl prettyprint"> -my @capitals = ("Baton Rouge", "Indianapolis", "Columbus", "Montgomery", "Helena", "Denver", "Boise"); -my $regex = qr/^[B-H]/; -print join ", ", grep /$regex/, @capitals; -</pre> + <p class="original">If you see a block of unformatted code wrapped in a delimiter with double chevrons, like <code><a href="http://perldoc.perl.org/perlop.html#Quote-and-Quote-like-Operators"><<EOF</a></code>, the magic word to Google for is "here-doc".</p> + <p>like <code><a href="http://perldoc.perl.org/perlop.html#Quote-and-Quote-like-Operators"><<EOF</a></code>ã®ããã«ã2ã¤ã®å±±æ¬å¼§ã§åºåããã¦ã©ãããããæ´çããã¦ããªãã³ã¼ãã®ãããã¯ãè¦ãããGoogleæ¤ç´¢ã®ããã®ãã¸ãã¯ã¯ã¼ãã¯"ãã¢ããã¥ã¡ã³ã(here-doc)"ã§ãã</p> </li> <li> - <p><code>qx{ }</code>㯠`ããã¯ã¯ã©ã¼ã`ã®ä»£ããã«ä½¿ããã¾ãã ããã°ã©ã ãå®è¡ãããã®åºåããã£ããã£ãã¾ã:</p> + <p class="original">Warning! Many built-in functions can be called with no arguments, <strong>causing them to operate on <code>$_</code> instead</strong>. Hopefully this will help you understand formations like:</p> + <p>注æ! å¤ãã®çµè¾¼é¢æ°ã§ã¯ãå¼æ°ãªãã§å¼ã¶ã¨<strong><code>$_</code>ã代ããã«æ¸¡ããã¾ã</strong>ãé¡ããã°ã次ã®ãããªå½¢ãç解ããå©ãã«ãªãã°è¯ãã®ã§ãã:</p> <pre class="perl prettyprint"> -my $text = qx{perl anotherscript.pl foo bar baz}; +print foreach @array; </pre> - </li> - <li> - <p>ããã«ãã£ã¨!</p> - </li> -</ul> -<p class="original">Instead of braces, you can use any character you like as the delimiter in these alternate quote-like operators, as well as in <code>m//</code> regex matches and <code>s///</code> regex replacements. This is actually quite useful if your regex contains a lot of slashes or backslashes. For example, <code>m!///!</code> matches three literal forward slashes.</p> -<p>ãã¬ã¼ã¹ã®ä»£ããã«ãã¯ã©ã¼ãã®ä»£ããã®æ¼ç®åã¯ã©ã®ãããªæåã§ãåºåãã¨ãã¦ä½¿ãã¾ããã¡ããã©<code>m//</code>æ£è¦è¡¨ç¾ã®ãããã<code>s///</code>æ£è¦è¡¨ç¾ã®ç½®æã¨åæ§ã§ããããã¯ãæ£è¦è¡¨ç¾ãå¤ãã®ã¹ã©ãã·ã¥ãããã¯ã¹ã©ãã·ã¥ãå«ãå ´åã«ãé常ã«æç¨ã§ããä¾ãã°ã<code>m!///!</code>ã¯ãå ¨è²ã®3æåã®ã¹ã©ãã·ã¥ã«ããããã¾ãã</p> -<p class="original">Perl does have <code>CONSTANTS</code>. These are discouraged now, but weren't always. Constants are actually just subroutine calls with omitted brackets.</p> -<p>Perlã«ã¯<code>å®æ°</code>ãããã¾ãã æ°è½ã¡ãããããªãã®ã§ããã常ã«ã§ã¯ããã¾ãããå®æ°ã¯å®éã«ã¯ããã©ã±ãããçç¥ãããã ã®ãµãã«ã¼ãã³å¼ã³åºãã§ãã</p> -<p class="original">Sometimes people omit quotes around hash keys. They can get away with it because in this situation a bareword (a string with no sigil) occurs as a string, as opposed to a subroutine call or a filehandle or a package name.</p> -<p>æã ãããã·ã¥ã®ãã¼ã§ã¯ã©ã¼ããçç¥ããã¦ãã¾ããããã®ç¶æ³ã§ã¯ã裸ã®ã¯ã¼ã(ã·ã¸ã«ã®ãªãæåå)ã¯ããµãã«ã¼ãã³å¼ã³åºãããã¡ã¤ã«ãã³ãã«ãããã±ã¼ã¸åã§ã¯ãªããæååã«ãªãããã§ãã</p> -<p class="original">If you see a block of unformatted code wrapped in a delimiter with double chevrons, like <code><<EOF</code>, the magic word to Google for is "heredoc".</p> -<p><code><<EOF</code>ã®ããã«ã2ã¤ã®å±±æ¬å¼§ã§åºåããã¦ã©ãããããæ´çããã¦ããªãã³ã¼ãã®ãããã¯ãè¦ãããGoogleæ¤ç´¢ã®ããã®ãã¸ãã¯ã¯ã¼ãã¯"ãã¢ããã¥ã¡ã³ã"ã§ãã</p> -<p class="original">The Data::Dumper module can be used to output an arbitrary scalar variable to the screen. This is an essential debug tool.</p> -<p>Data::Dumperã¢ã¸ã¥ã¼ã«ã¯ä»»æã®ã¹ã«ã©ã®å¤ãã¹ã¯ãªã¼ã³ã«åºåããã®ã«ä½¿ãã¾ããããã¯åºæ¬çãªãããã°ãã¼ã«ã§ãã</p> -<p class="original">Warning! Many built-in functions can be called with no arguments, <strong>causing them to operate on <code>$_</code> instead</strong>. Hopefully this will help you understand formations like:</p> -<p>注æ! å¤ãã®çµè¾¼é¢æ°ã§ã¯ãå¼æ°ãªãã§å¼ã¶ã¨<strong><code>$_</code>ã代ããã«æ¸¡ããã¾ã</strong>ãé¡ããã°ã次ã®ãããªå½¢ãç解ããå©ãã«ãªãã°è¯ãã®ã§ãã:</p> +<p class="original">and</p> +<p>ããã¦</p> <pre class="perl prettyprint"> -print foreach @array; - foreach ( @array ) { next unless defined; } </pre> -<p class="original">I dislike this formation because it can lead to problems when refactoring.</p> -<p>ç§ã¯ããã®å½¢ã¯å«ãã§ãããªãã¡ã¯ã¿ãªã³ã°ã®éã«ãåé¡ãèµ·ãããã¨ãããããã§ãã</p> + <p class="original">I dislike this formation because it can lead to problems when refactoring.</p> + <p>ç§ã¯ããã®å½¢ã¯å«ãã§ãããªãã¡ã¯ã¿ãªã³ã°ã®éã«ãåé¡ãèµ·ãããã¨ãããããã§ãã</p> + </li> +</ul> <p class="original">The end.</p> <p>ããã¾ãã</p>