ktats****@users*****
ktats****@users*****
2012年 9月 1日 (土) 04:42:07 JST
Index: docs/articles/qntm.org/files/perl/perl.html diff -u docs/articles/qntm.org/files/perl/perl.html:1.6 docs/articles/qntm.org/files/perl/perl.html:1.7 --- docs/articles/qntm.org/files/perl/perl.html:1.6 Sat Sep 1 01:58:59 2012 +++ docs/articles/qntm.org/files/perl/perl.html Sat Sep 1 04:42:07 2012 @@ -1599,7 +1599,7 @@ <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> +<strong><tt>BEGIN</tt>ãããã¯ãæ¡ä»¶ã®ä¸ã«ç½®ãã¦ã¯è¡ãã¾ãã!</strong> ã³ã³ãã¤ã«æã«ä½ãããã®æ¡ä»¶ä»ãã®ãã¨ãããããã°ã<tt>BEGIN</tt>ãããã¯ã®<em>ä¸ã«</em>æ¡ä»¶æãç½®ããªããã°ãªãã¾ãã:</p> <pre class="perl prettyprint"> BEGIN { if($condition) { @@ -1691,23 +1691,41 @@ crawl(); # "inch inch" </pre> -<p>The package variable <tt>@EXPORT_OK</tt> can be populated with a list of all subroutines which the user can import explicitly by passing subroutine names to <tt>import()</tt>. If <tt>import()</tt> is called with the name of a subroutine not in this list, a runtime error will occur. For example, try <tt>use Bugs::Caterpillar ("pupate")</tt>.</p> +<p class="original">The package variable <tt>@EXPORT_OK</tt> can be populated with a list of all subroutines which the user can import explicitly by passing subroutine names to <tt>import()</tt>. If <tt>import()</tt> is called with the name of a subroutine not in this list, a runtime error will occur. For example, try <tt>use Bugs::Caterpillar ("pupate")</tt>.</p> -<p>The package variable <tt>@EXPORT</tt> can be populated with a list of subroutines to be exported by default. These are exported if <tt>import()</tt> is called with no arguments at all, which is what happens in this example.</p> +<p>ããã±ã¼ã¸å¤æ°<tt>@EXPORT</tt>ã¯ããã©ã«ãã§ã¨ã¯ã¹ãã¼ãããããµãã«ã¼ãã³ã®ãªã¹ããå ¥ãã¾ãã<tt>import()</tt> ãå¼æ°ãªãã§å¼ã°ããå ´åã«ããã®ä¾ã§èµ·ãããã¨ãèµ·ãã¾ãã</p> -<p>As a result of being <tt>import()</tt>ed, a method such as <tt>Bugs::Caterpillar::crawl()</tt> become available without qualification as <tt>crawl()</tt>. This saves typing. (Note: regardless of the content of <tt>@EXPORT_OK</tt>, 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 class=original>>As a result of being <tt>import()</tt>ed, a method such as <tt>Bugs::Caterpillar::crawl()</tt> become available without qualification as <tt>crawl()</tt>. This saves typing. (Note: regardless of the content of <tt>@EXPORT_OK</tt>, 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><strong>A caution.</strong> Notice how <tt>crawl()</tt> was neither defined in the script, nor explicitly <tt>import()</tt>ed from another file with e.g. <tt>use Bugs::Caterpillar ("crawl")</tt>. Suppose the middle three lines weren't there to provide clues, and suppose there were a dozen <tt>use</tt> calls alongside <tt>use Bugs::Caterpillar</tt>. And remember that any module is free to have more <tt>use</tt> calls of its own. In such a situation, it is extremely difficult to locate the place where <tt>crawl()</tt> was originally defined. The moral of this story is twofold:</p> -<ol> +<p><tt>import()</tt>ãããçµæã¨ãã¦ã<tt>Bugs::Caterpillar::crawl()</tt>ã®ãããªã¡ã½ããã<tt>crawl()</tt>ã®ããã«ã修飾ãªãã«å©ç¨å¯è½ã«ãªãã¾ããããã¯ãã¿ã¤ãæ°ã®ç¯ç´ã«ãªãã¾ãã(注æ: <tt>@EXPORT_OK</tt>ã®ã³ã³ã»ããã«ãããããã å ¨ã¦ã®ã¡ã½ããã¯ãä¸ã§è¦ãããã«ã"longhand"ã§å¼ã¶ãã¨ãåºæ¥ã¾ãã Perlã«ã¯ãã©ã¤ãã¼ãã¡ã½ããã¯ããã¾ãããéä¾ã1ã¤ã2ã¤ã®ã¢ã³ãã¼ã¹ã³ã¢ããå§ã¾ãååãä»ããããã¡ã½ããã¯ãã©ã¤ãã¼ããæå³ãã¾ãã</p> + +<p class="original"><strong>A caution.</strong> Notice how <tt>crawl()</tt> was neither defined in the script, nor explicitly <tt>import()</tt>ed from another file with e.g. <tt>use Bugs::Caterpillar ("crawl")</tt>. Suppose the middle three lines weren't there to provide clues, and suppose there were a dozen <tt>use</tt> calls alongside <tt>use Bugs::Caterpillar</tt>. And remember that any module is free to have more <tt>use</tt> calls of its own. In such a situation, it is extremely difficult to locate the place where <tt>crawl()</tt> was originally defined. The moral of this story is twofold:</p> + +<p><strong>注æã</strong><tt>crawl()</tt>ã¯ã¹ã¯ãªããã§å®ç¾©ããã¦ããããæ示çã«ä»ã®ãã¡ã¤ã«ãã<tt>import()</tt>ããã¾ãããe.g. <tt>use Bugs::Caterpillar ("crawl")</tt>ãçãä¸ã®ï¼è¡ã®ããã«ãã³ããæä¾ããã<tt>use Bugs::Caterpillar</tt>ã¨ä¸¦è¡ãã¦ã12ã®<tt>use</tt>å¼ã³åºãããã£ãã¨ããã¨ã ã¾ããã©ã®ã¢ã¸ã¥ã¼ã«ãèªç±ã«è¤æ°ã®<tt>use</tt>å¼ã³åºããããèªèº«ã§ã§ãããã¨ãæãèµ·ããã¨ããã®ãããªç¶æ³ã§ã¯ã +<tt>crawl()</tt>ãå ã å®ç¾©ããã¦ããå ´æãè¦ã¤ããã®ã¯å®¹æã§ã¯ããã¾ããããã®è©±ã®æè¨ã¯2ã¤ããã¾ã:</p> + +<ol class="original"> <li><p>When creating a module which makes use of Exporter, never use <tt>@EXPORT</tt> to export subroutines by default. Always make the user call subroutines "longhand" or <tt>import()</tt> them explicitly (using e.g. <tt>use Bugs::Caterpillar ("crawl")</tt>, which is a strong clue to look in <tt>Bugs/Caterpillar.pm</tt> for the definition of <tt>crawl()</tt>).</p></li> <li><p>When <tt>use</tt>ing a module, always explicitly name the subroutines you want to <tt>import()</tt>. If you don't want to <tt>import()</tt> any subroutines and wish to refer to them longhand, you must supply an explicit empty list: <tt>use Bugs::Caterpillar ()</tt>.</p></li> </ol> -<h2>Files</h2> +<ol class="original"> +<li><p>When creating a module which makes use of Exporter, never use <tt>@EXPORT</tt> to export subroutines by default. Always make the user call subroutines "longhand" or <tt>import()</tt> them explicitly (using e.g. <tt>use Bugs::Caterpillar ("crawl")</tt>, which is a strong clue to look in <tt>Bugs/Caterpillar.pm</tt> for the definition of <tt>crawl()</tt>).</p></li> +<li><p>When <tt>use</tt>ing a module, always explicitly name the subroutines you want to <tt>import()</tt>. If you don't want to <tt>import()</tt> any subroutines and wish to refer to them longhand, you must supply an explicit empty list: <tt>use Bugs::Caterpillar ()</tt>.</p></li> +</ol> +<ol> +<li><p>Exporterã使ãã¢ã¸ã¥ã¼ã«ãä½ãéã«ã¯ãããã©ã«ãã§ãµãã«ã¼ãã³ãã¨ã¯ã¹ãã¼ããã<tt>@EXPORT</tt>ã使ããªããã¨ã常ã«ãã¦ã¼ã¶ã¼ã«ã"longhand"ã§ãµãã«ã¼ãã³ãå¼ã°ããããæ示çã«<tt>import()</tt>ããããã¨(e.g. <tt>use Bugs::Caterpillar ("crawl")</tt>ã使ã£ã¦ã<tt>Bugs/Caterpillar.pm</tt>ã«<tt>crawl()</tt>)ã®å®ç¾©ãããã¨ããå¼·ããã³ãã«ãªãã¾ãã</p></li> +<li><p>ã¢ã¸ã¥ã¼ã«ã<tt>use</tt>ããã¨ã常ã«æ示çã«<tt>import()</tt>ããããµãã«ã¼ãã³ã®ååæ¸ãã¾ããä½ã®ãµãã«ã¼ãã³ã<tt>import()</tt>ããããªããlonghandã§åç §ãããã®ãªããããã®ãªã¹ããæä¾ããªããã°ããã¾ããã: <tt>use Bugs::Caterpillar ()</tt>.</p></li> +</ol> + +<h2 class="original">Files</h2> +<h2>ãã¡ã¤ã«</h2> -<p>A <i>file handle</i> is a completely different object from a scalar, array or hash variable. File handles are customarily represented in <tt>ALLCAPS</tt>; three familiar built-in filehandles are <tt>STDIN</tt>, <tt>STDOUT</tt> and <tt>STDERR</tt>.</p> +<p class="original">A <i>file handle</i> is a completely different object from a scalar, array or hash variable. File handles are customarily represented in <tt>ALLCAPS</tt>; three familiar built-in filehandles are <tt>STDIN</tt>, <tt>STDOUT</tt> and <tt>STDERR</tt>.</p> +<p><i>ãã¡ã¤ã«ãã³ãã«</i>ã¯ãã¹ã«ã©ãé åãããã·ã¥ã¨ã¯å®å ¨ã«éã£ããã®ã§ãããã¡ã¤ã«ãã³ãã«ã¯éä¾<tt>å ¨ã¦å¤§æå</tt>ã§è¡¨ç¾ããã¾ã; ï¼ã¤ã®ããç¥ããã¦ããçµè¾¼ã®ãã¡ã¤ã«ãã³ãã«ã¨ãã¦ã<tt>STDIN</tt>, <tt>STDOUT</tt> and <tt>STDERR</tt>ãããã¾ãã</p> -<p>Filehandles don't need declaring explicitly using <tt>my</tt> or <tt>our</tt>. They pop into existence automatically. A file handle can be opened using <tt>open</tt>. <tt>open</tt> must be supplied with a <i>method</i>. The method <tt><</tt> indicates that we wish to open the file to read from it:</p> +<p class="original">Filehandles don't need declaring explicitly using <tt>my</tt> or <tt>our</tt>. They pop into existence automatically. A file handle can be opened using <tt>open</tt>. <tt>open</tt> must be supplied with a <i>method</i>. The method <tt><</tt> indicates that we wish to open the file to read from it:</p> +<p>ãã¡ã¤ã«ãã³ãã«ã¯<tt>my</tt>ã<tt>our</tt>ã使ã£ã¦ãæ示çã«å®£è¨ããå¿ è¦ã¯ããã¾ãããèªåçã«ã²ããã¨åå¨ãã¾ãããã¡ã¤ã«ãã³ãã«ã¯<tt>open</tt>ã使ã£ã¦éããã¾ãã<tt>open</tt>ã¯<i>æ¹å¼</i>ã¨ã¨ãã«ã使ãããªããã°ããã¾ããã æ¹å¼ã¯<tt><</tt> ãã¡ã¤ã«ãèªãããã«éããã¨ã示ãã¾ã:</p> <pre class="perl prettyprint"> my $f = "text.txt"; @@ -1718,23 +1736,28 @@ } </pre> -<p>As seen above, you should always check that the <tt>open</tt> operation completed successfully. If successful, <tt>open</tt> returns a true value. Otherwise, it returns <tt>undef</tt>. This checking procedure being rather tedious, a frequently-seen idiom is this:</p> +<p class="original">As seen above, you should always check that the <tt>open</tt> operation completed successfully. If successful, <tt>open</tt> returns a true value. Otherwise, it returns <tt>undef</tt>. This checking procedure being rather tedious, a frequently-seen idiom is this:</p> +<p>ä¸ã®ããã«ã<tt>open</tt> æ¼ç®åãå®å ¨ã«æåãããã常ã«ãã§ãã¯ãã¹ãã§ããæåããã°<tt>open</tt>çãè¿ãã¾ããããã§ãªããã°ã<tt>undef</tt>ãè¿ãã¾ãããã®ãã§ãã¯ã®æç¶ãã¯éå±ã§ãããããã¿ãããã¤ãã£ãªã ã¯æ¬¡ã®ãã®ã§ã:</p> <pre class="perl prettyprint"> open(INPUT, "<", $f) || die "Couldn't open ", $f, " for reading"; </pre> -<p>Notice how without the brackets, this would be:</p> +<p class="original">Notice how without the brackets, this would be:</p> +<p>ãã©ã±ãããç¡ãã¨ã©ããªãã注æãã¦ãã ããã次ã®ãã®ã¯:</p> <pre class="perl prettyprint"> open INPUT, "<", $f || die "Couldn't open ", $f, " for reading"; </pre> -<p>Which is the same as the nonsensical:</p> +<p class="original">Which is the same as the nonsensical:</p> +<p>ç¡æå³ãªãã®åãã§ã:</p> <pre class="perl prettyprint"> open INPUT, "<", ($f || die "Couldn't open ", $f, " for reading"); </pre> -<p>For this reason (and, as far as I can tell, solely this reason), Perl provides a completely separate operator, <tt>or</tt>, which works exactly like <tt>||</tt> except that it has extremely low precedence, making this possible:</p> +<p class="original">For this reason (and, as far as I can tell, solely this reason), Perl provides a completely separate operator, <tt>or</tt>, which works exactly like <tt>||</tt> except that it has extremely low precedence, making this possible:</p> +<p>ãã®çç±ã§(æããããéããåã«ãã®çç±ã§)ãPerlã¯å®å ¨ã«åºå¥ãããæ¼ç®åã®<tt>or</tt>ãããã¾ãã<tt>||</tt>ã¨åãã§ãããåªå é ä½ãé常ã«ä½ãããã次ã®ãã¨ãå¯è½ã§ã:</p> <pre class="perl prettyprint"> open INPUT, "<", $f or die "Couldn't open ", $f, " for reading"; </pre> -<p>To read a line of text from a filehandle, use the <tt>readline</tt> built-in function. <tt>readline</tt> returns a full line of text, with a line break intact at the end of it (except possibly for the final line of the file), or <tt>undef</tt> if you've reached the end of the file.</p> +<p class="original">To read a line of text from a filehandle, use the <tt>readline</tt> built-in function. <tt>readline</tt> returns a full line of text, with a line break intact at the end of it (except possibly for the final line of the file), or <tt>undef</tt> if you've reached the end of the file.</p> +<p>ãã¡ã¤ã«ãã³ãã«ããããã¹ãã®è¡ãèªãããã«ãçµè¾¼é¢æ°ã®<tt>readline</tt>ã使ãã¾ãã<tt>readline</tt>ã¯ãããã¹ãã®ä¸è¡å ¨ä½ãããã®çµããã«æ¹è¡ããã®ã¾ã¾å«ãã§è¿ã(ãã¶ããã¡ã¤ã«ã®æçµè¡ãé¤ãã¦)ãããã¡ã¤ã«ã®æå¾ã«éããã¨<tt>undef</tt>ãè¿ãã¾ãã</p> <pre class="perl prettyprint"> while(1) { my $line = readline INPUT; @@ -1742,51 +1765,64 @@ # process the line... } </pre> -<p>To truncate that possible trailing line break, use <tt>chomp</tt>:</p> +<p class="original">To truncate that possible trailing line break, use <tt>chomp</tt>:</p> +<p><tt>chomp</tt>ã使ãã¨æ¹è¡ãåãé¤ãã¾ã:</p> <pre class="perl prettyprint"> chomp $line; </pre> -<p>Note that <tt>chomp</tt> acts on <tt>$line</tt> in place. <tt>$line = chomp $line</tt> is probably not what you want.</p> -<p>You can also use <tt>eof</tt> to detect the end of the file:</p> +<p class="original">Note that <tt>chomp</tt> acts on <tt>$line</tt> in place. <tt>$line = chomp $line</tt> is probably not what you want.</p> +<p><tt>chomp</tt>㯠<tt>$line</tt>ã«å¯¾ãã¦ãã®å ´ã§åãã¾ãã<tt>$line = chomp $line</tt>ã¯å¤åãæã¿ã®ãï½oã§ã¯ãªãã§ãããã</p> +<p class="original">You can also use <tt>eof</tt> to detect the end of the file:</p> +<p><tt>eof</tt>ã使ã£ã¦ãã¡ã¤ã«ã®çµç«¯ãå¤æãããã¨ãã§ãã¾ã:</p> <pre class="perl prettyprint"> while(!eof INPUT) { my $line = readline INPUT; # process $line... } </pre> -<p>But beware of just using <tt>while(my $line = readline INPUT)</tt>, because if <tt>$line</tt> turns out to be <tt>"0"</tt>, the loop will terminate early. If you want to write something like that, Perl provides the <tt><></tt> operator which wraps up <tt>readline</tt> in a fractionally safer way. This is very commonly-seen and perfectly safe:</p> +<p class="original">But beware of just using <tt>while(my $line = readline INPUT)</tt>, because if <tt>$line</tt> turns out to be <tt>"0"</tt>, the loop will terminate early. If you want to write something like that, Perl provides the <tt><></tt> operator which wraps up <tt>readline</tt> in a fractionally safer way. This is very commonly-seen and perfectly safe:</p> +<p>ã§ããã<tt>while(my $line = readline INPUT)</tt>ã使ãã®ã¯æ³¨æãã¦ãã ããã<tt>$line</tt>ã<tt>"0"</tt>ã§åã£ãå ´åãã«ã¼ãã¯æ©ãã«çµãã£ã¦ãã¾ãã¾ãããã®ããã«æ¸ãããã®ãªãã°ãPerlã«ã¯<tt><></tt>æ¼ç®åããããå°ãå®å ¨ãªæ¹æ³ã§<tt>readline</tt>ãã©ãããã¦ãã¾ãã次ã®ãã®ã¯ãã¨ã¦ãããããå®å ¨ã«å®å ¨ãªãã®ã§ã:</p> <pre class="perl prettyprint"> while(my $line = <INPUT>) { # process $line... } </pre> -<p>And even:</p> +<p class="original">And even:</p> +<p>次ã®ããã«ãã:</p> <pre class="perl prettyprint"> while(<INPUT>) { # process $_... } </pre> -<p>To read a single line of user input:</p> +<p class="original">To read a single line of user input:</p> +<p>ã¦ã¼ã¶ã¼ã®å ¥åããä¸è¡èªã¿åºãã«ã¯:</p> <pre class="perl prettyprint"> my $line = <STDIN>; </pre> -<p>To just wait for the user to hit Enter:</p> +<p class="original">To just wait for the user to hit Enter:</p> +<p>ã¦ã¼ã¶ã¼ãã¨ã³ã¿ã¼ãæ¼ãã¾ã§å¾ ã¤ã ãã§ã:</p> <pre class="perl prettyprint"> <STDIN>; </pre> -<p>Calling <tt><></tt> with no filehandle reads data from standard input, or from any files named in arguments when the Perl script was called.</p> -<p>Writing to a file involves first opening it in a different mode. The method <tt>></tt> indicates that we wish to open the file to write to it. (<tt>></tt> will clobber the content of the target file if it already exists and has content. To merely append to an existing file, use mode <tt>>></tt>). Then, simply provide the filehandle as a zeroth argument for the <tt>print</tt> function.</p> +<p class="original">Calling <tt><></tt> with no filehandle reads data from standard input, or from any files named in arguments when the Perl script was called.</p> +<p><tt><></tt>ããã¡ã¤ã«ãã³ãã«ãªãã§å¼ã¶ã¨ãæ¨æºå ¥åãããã¼ã¿ãèªãããPerlã¹ã¯ãªãããå¼ã°ããæã®å¼æ°ã«æ¸¡ãããååã®ãã¡ã¤ã«ããèªã¿ã¾ãã</p> +<p class="original">Writing to a file involves first opening it in a different mode. The method <tt>></tt> indicates that we wish to open the file to write to it. (<tt>></tt> will clobber the content of the target file if it already exists and has content. To merely append to an existing file, use mode <tt>>></tt>). Then, simply provide the filehandle as a zeroth argument for the <tt>print</tt> function.</p> +<p>ãã¡ã¤ã«ã«æ¸ãè¾¼ãå ´åã¯ãéã£ãã¢ã¼ãã§æåã«éãã¾ããæ¹å¼<tt>></tt>ã¯ãæ¸ãè¾¼ã¿ç¨ã«ãã¡ã¤ã«ãéããã¨ãæ示ãã¾ãã(<tt>></tt>ã¯ãç®çã®ãã¡ã¤ã«ã®ä¸èº«ããåãã¾ããåç´ã«ã追å ãããå ´åã¯ã<tt>>></tt>ã®ã¢ã¼ãã使ãã¾ã)ãããããã +<tt>print</tt>é¢æ°ã®0çªç®ã®å¼æ°ã¨ãã¦ããã¡ã¤ã«ãã³ãã«ãåã«æ¸¡ãã¾ãã</p> <pre class="perl prettyprint"> open OUTPUT, ">", $f or die "Couldn't open ", $f, " for writing"; print OUTPUT "The eagles have left the nest"; </pre> -<p>Notice the absence of a comma between the filehandle and the first argument in <tt>print</tt>. As you've gathered, if the filehandle is omitted, <tt>STDOUT</tt> is used by default.</p> -<p>File handles are actually closed automatically at script exit time, but otherwise:</p> +<p class="original">Notice the absence of a comma between the filehandle and the first argument in <tt>print</tt>. As you've gathered, if the filehandle is omitted, <tt>STDOUT</tt> is used by default.</p> +<p><tt>print</tt>ã§ãã¡ã¤ã«ãã³ãã«ã¨æåã®å¼æ°ã®éã«ã«ã³ãããªããã¨ã«æ°ãã¤ãã¦ãã ãããæ¢ã«ç¥ã£ã¦ããã¨ããããã¡ã¤ã«ãã³ãã«ãçç¥ãããå ´åã¯ã<tt>STDOUT</tt>ãããã©ã«ãã§ä½¿ããã¾ãã</p> +<p class=original>File handles are actually closed automatically at script exit time, but otherwise:</p> +<p>ãã¡ã¤ã«ãã³ãã«ã¯å®éã«ã¯ã¹ã¯ãªãããçµããã¨ãã«èªåçã«éãããã¾ããããã§ãªããã°:</p> <pre class="perl prettyprint"> close INPUT; close OUTPUT; </pre> -<p>A scalar variable may hold a reference to a file handle instead of a variable:</p> +<p class="original">A scalar variable may hold a reference to a file handle instead of a variable:</p> +<p>ã¹ã«ã©å¤æ°ã§ãå¤æ°ã®ä»£ããã«ãã¡ã¤ã«ãã³ãã«ã®ãªãã¡ã¬ã³ã¹ãæã£ã¦ãããã§ã:</p> <pre class="perl prettyprint"> my $fh; open $fh, "<", "text.txt" or die; @@ -1795,22 +1831,30 @@ } close $fh; </pre> -<h2>System calls</h2> -<p>Apologies if you already know the following non-Perl-related facts. Every time a process finishes on a Windows or Linux system (and, I assume, on most other systems), it concludes with a 16-bit <i>status word</i>. The highest 8 bits constitute a <i>return code</i> between 0 and 255 inclusive, with 0 conventionally representing unqualified success, and other values representing various degrees of failure. The other 8 bits are less frequently examined - they "reflect mode of failure, like signal death and core dump information".</p> -<p>You can exit from a Perl script with the return code of your choice (from 0 to 255) using <tt>exit</tt>.</p> -<p>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 built-in variable <tt>$?</tt> (<tt>$CHILD_ERROR</tt>) 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: <tt>$? >> 8</tt>.</p> -<p>The <tt>system</tt> function can be used to invoke another program with the arguments listed. The value returned by <tt>system</tt> is the same value with which <tt>$?</tt> is populated:</p> +<h2 class="original">System calls</h2> +<h2>ã·ã¹ãã ã³ã¼ã«</h2> +<p class="original">Apologies if you already know the following non-Perl-related facts. Every time a process finishes on a Windows or Linux system (and, I assume, on most other systems), it concludes with a 16-bit <i>status word</i>. The highest 8 bits constitute a <i>return code</i> between 0 and 255 inclusive, with 0 conventionally representing unqualified success, and other values representing various degrees of failure. The other 8 bits are less frequently examined - they "reflect mode of failure, like signal death and core dump information".</p> +<p>æ¢ã«ä»¥ä¸ã®Perlã¨é¢ä¿ãªãäºå®ããåç¥ã§ãããç³ã訳æãã¾ãããWindowãLinux(ã¾ããå¤ãã®ä»ã®ã·ã¹ãã ã§ãåæ§ã¨æ³å®ããã¾ããã)ã®ã·ã¹ãã ã§ã¯ããã»ã¹ãçµãããã³ã«ã16ãããã®<i>ç¶æ ã¯ã¼ã</i>ã§çµäºãã¾ããä¸ä½ã®8ãããã¯0ã255ã®éãå«ã<i>ãªã¿ã¼ã³ã³ã¼ã</i>ãæ§æãã¾ãã0ã¯æ £ä¾çã«ãç¡æ¡ä»¶ã§æåãããã¨ã示ãã¾ããä»ã®å¤ã¯æ§ã ãªåº¦åãã®å¤±æã示ãã¾ããä»ã®8ãããã¯ãã¾ããã調ã¹ããã¾ãããããããã¯"ã·ã°ãã«ã®æ»äº¡ãã³ã¢ãã³ãã®æ å ±ã®ãããªå¤±æã®ã¢ã¼ããåæ ãã¾ã"ã</p> +<p class="original">You can exit from a Perl script with the return code of your choice (from 0 to 255) using <tt>exit</tt>.</p> +<p>Perlããçµäºããéã«ã<tt>exit</tt>ã使ã£ã¦(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 built-in variable <tt>$?</tt> (<tt>$CHILD_ERROR</tt>) 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: <tt>$? >> 8</tt>.</p> +<p>Perlã«ã¯ä¸ã¤ããå¤ãã® - åããã»ã¹ãç£ã - æ¹æ³ãããã¾ããç¾å¨ã®ã¹ã¯ãªãããæ¢ããåããã»ã¹ãçµãã£ãããç¾å¨ã®ã¹ã¯ãªããã®è§£éãç¶ãã¾ãã +ã©ã®æ¹æ³ã使ã£ã¦ãããã®ç´å¾ã§ãåããã»ã¹ã®çµäºæã«è¿ãããç¶æ ã¯ã¼ããå ¥ã£ã¦ãããçµè¾¼ã®å¤æ°<tt>$?</tt>(<tt>$CHILD_ERROR</tt>)ãè¦ã¤ãããã¾ãã16ãããã®ä¸ä½8ãåããã¨ã§ããªã¿ã¼ã³ã³ã¼ããå¾ããã¨ãã§ãã¾ã: <tt>$? >> 8</tt>ã</p> +<p class="original">The <tt>system</tt> function can be used to invoke another program with the arguments listed. The value returned by <tt>system</tt> is the same value with which <tt>$?</tt> is populated:</p> +<p><tt>system</tt>é¢æ°ã¯ä»ã®ããã°ã©ã ãå¼æ°ã®ãªã¹ãã¨ä¸ç·ã«å¼ã³åºãã¾ãã<tt>system</tt>ã«ãã£ã¦è¿ãããå¤ã¯ã<tt>$?</tt>ã«å ¥ãã®ã¨åãå¤ã§ã:</p> <pre class="perl prettyprint"> my $rc = system "perl", "anotherscript.pl", "foo", "bar", "baz"; $rc >>= 8; print $rc; # "37"; </pre> -<p>Alternatively, you can use backticks <tt>``</tt> to run an actual command at the command line and capture the standard output from that command. In scalar context the entire output is returned as a single string. In list context, the entire output is returned as an array of strings, each one representing a line of output.</p> +<p class="original">Alternatively, you can use backticks <tt>``</tt> to run an actual command at the command line and capture the standard output from that command. In scalar context the entire output is returned as a single string. In list context, the entire output is returned as an array of strings, each one representing a line of output.</p> +<p>代ããã«ãããã¯ã¯ã©ã¼ã<tt>``</tt>ã使ã£ã¦ãã³ãã³ãã©ã¤ã³ã§å®éã®ã³ãã³ããèµ°ããã¦ãã³ãã³ãããã®æ¨æºåºåããã£ããã£ã§ãã¾ããã¹ã«ã©ã³ã³ããã¹ãã§ã¯ãå ¨ã¦ã®åºåã¯åä¸ã®æååã¨ãã¦å¸°ãã¾ãããªã¹ãã³ã³ããã¹ãã§ã¯ãå ¨ã¦ã®åºåã¯ä¸è¡ãã¤ã®æååã®é åã¨ãã¦è¿ããã¾ãã</p> <pre class="perl prettyprint"> my $text = `perl anotherscript.pl foo bar baz`; print $text; # "foobarbaz" </pre> -<p>This is the behaviour which would be seen if <tt>anotherscript.pl</tt> contained, for example:</p> +<p class="original">This is the behaviour which would be seen if <tt>anotherscript.pl</tt> contained, for example:</p> +<p>ããã¯ã<tt>anotherscript.pl</tt>ãå«ãã§ããããè¦ãããæ¯ãèãã§ããä¾:</p> <pre class="perl prettyprint"> use strict; use warnings; @@ -1819,9 +1863,11 @@ exit(37); </pre> -<h2>Miscellaneous notes</h2> -<p>Perl provides a wide selection of quote-like operators in addition to what you've seen in these documents:</p> -<ul> +<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"> <li> <p>There's an alternate syntax, <tt>qw{ }</tt>, for declaring arrays. This often seen in <tt>use</tt> statements:</p> <pre class="perl prettyprint"> @@ -1846,13 +1892,43 @@ <p>And many more!</p> </li> </ul> -<p>Instead of braces, you can use any character you like as the delimiter in these alternate quote-like operators, as well as in <tt>m//</tt> regex matches and <tt>s///</tt> regex replacements. This is actually quite useful if your regex contains a lot of slashes or backslashes. For example, <tt>m!///!</tt> matches three literal forward slashes.</p> -<p>Perl does have <tt>CONSTANTS</tt>. These are discouraged now, but weren't always. Constants are actually just subroutine calls with omitted brackets.</p> -<p>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>If you see a block of unformatted code wrapped in a delimiter with double chevrons, like <tt><<EOF</tt>, the magic word to Google for is "heredoc".</p> -<p>The Data::Dumper module can be used to output an arbitrary scalar variable to the screen. This is an essential debug tool.</p> - -<p>Warning! Many built-in functions can be called with no arguments, <strong>causing them to operate on <tt>$_</tt> instead</strong>. Hopefully this will help you understand formations like:</p> +<ul> + <li> + <p>代ããã®ã·ã³ã¿ãã¯ã¹ã¨ãã¦ã<tt>qw{ }</tt>ããé åã®å®£è¨ã®ããã«ããã¾ãã<tt>use</tt>ã¹ãã¼ãã¡ã³ãã§ããè¦ããã¾ã:</p> +<pre class="perl prettyprint"> +use Account qw{create open close suspend delete}; +</pre> + </li> + <li> + <p><tt>qr//</tt>ã¯æ£è¦è¡¨ç¾ãã¹ã«ã©å¤æ°ã«ç½®ãã®ã«ä½¿ããã¾ããããã¯é常ã«ä¾¿å©ã§ãããªããªããæ£è¦è¡¨ç¾ã®åã³ã³ãã¤ã«ã«ã¯æéããããããã§ã:</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> + </li> + <li> + <p><tt>qx{ }</tt>㯠`ããã¯ã¯ã©ã¼ã`ã®ä»£ããã«ä½¿ããã¾ãã ããã°ã©ã ãå®è¡ãããã®åºåããã£ããã£ãã¾ã:</p> +<pre class="perl prettyprint"> +my $text = qx{perl anotherscript.pl foo bar baz}; +</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 <tt>m//</tt> regex matches and <tt>s///</tt> regex replacements. This is actually quite useful if your regex contains a lot of slashes or backslashes. For example, <tt>m!///!</tt> matches three literal forward slashes.</p> +<p>ãã¬ã¼ã¹ã®ä»£ããã«ãã¯ã©ã¼ãã®ä»£ããã®æ¼ç®åã¯ã©ã®ãããªæåã§ãåºåãã¨ãã¦ä½¿ãã¾ããã¡ããã©<tt>m//</tt>æ£è¦è¡¨ç¾ã®ãããã<tt>s///</tt>æ£è¦è¡¨ç¾ã®ç½®æã¨åæ§ã§ããããã¯ãæ£è¦è¡¨ç¾ãå¤ãã®ã¹ã©ãã·ã¥ãããã¯ã¹ã©ãã·ã¥ãå«ãå ´åã«ãé常ã«æç¨ã§ããä¾ãã°ã<tt>m!///!</tt>ã¯ãå ¨è²ã®3æåã®ã¹ã©ãã·ã¥ã«ããããã¾ãã</p> +<p class="original">Perl does have <tt>CONSTANTS</tt>. These are discouraged now, but weren't always. Constants are actually just subroutine calls with omitted brackets.</p> +<p>Perlã«ã¯<tt>å®æ°</tt>ãããã¾ãã æ°è½ã¡ãããããªãã®ã§ããã常ã«ã§ã¯ããã¾ãããå®æ°ã¯å®éã«ã¯ããã©ã±ãããçç¥ãããã ã®ãµãã«ã¼ãã³å¼ã³åºãã§ãã</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 <tt><<EOF</tt>, the magic word to Google for is "heredoc".</p> +<p><tt><<EOF</tt>ã®ããã«ã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 <tt>$_</tt> instead</strong>. Hopefully this will help you understand formations like:</p> +<p>注æ! å¤ãã®çµè¾¼é¢æ°ã§ã¯ãå¼æ°ãªãã§å¼ã¶ã¨<strong><tt>$_</tt>ã代ããã«æ¸¡ããã¾ã</strong>ãé¡ããã°ã次ã®ãããªå½¢ãç解ããå©ãã«ãªãã°è¯ãã®ã§ãã:</p> <pre class="perl prettyprint"> print foreach @array; @@ -1860,8 +1936,8 @@ next unless defined; } </pre> -<p>I dislike this formation because it can lead to problems when refactoring.</p> - +<p class="original">I dislike this formation because it can lead to problems when refactoring.</p> +<p>ç§ã¯ããã®å½¢ã¯å«ãã§ãããªãã¡ã¯ã¿ãªã³ã°ã®éã«ãåé¡ãèµ·ãããã¨ãããããã§ãã</p> <h3 class=original>File tests</h3> <h3>ãã¡ã¤ã«ãã¹ã</h3>