ktats****@users*****
ktats****@users*****
2012年 8月 31日 (金) 01:27:35 JST
Index: docs/articles/qntm.org/files/perl/perl.html diff -u docs/articles/qntm.org/files/perl/perl.html:1.3 docs/articles/qntm.org/files/perl/perl.html:1.4 --- docs/articles/qntm.org/files/perl/perl.html:1.3 Sun Aug 5 18:49:22 2012 +++ docs/articles/qntm.org/files/perl/perl.html Fri Aug 31 01:27:34 2012 @@ -2,6 +2,8 @@ <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" /> + <meta name="description" content="2æéåã§å¦ã¹ãPerlã®ãã¥ã¼ããªã¢ã«"> + <!-- linkto: http://yahoo.co.jp/ --> <title>2æéåã§å¦ã¶Perl</title> <style type="text/css"> @@ -74,7 +76,7 @@ <p>Perl<i>ã¹ã¯ãªãã</i>ã¯<tt>.pl</tt>ã¨ããæ¡å¼µåã®ããã¹ããã¡ã¤ã«ã§ãã</p> <p class=original>Here's the full text of <tt>helloworld.pl</tt>:</p> <p><tt>helloworld.pl</tt>ã¯ä»¥ä¸ã®ããã«ãªãã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> use strict; use warnings; @@ -128,7 +130,7 @@ print $undef2; # exactly the same warning </pre> -<pre class="perl"> +<pre class="perl prettyprint"> my $undef = undef; print $undef; # warning @@ -137,12 +139,12 @@ print $undef2; # ã¾ã£ããåãwarning </pre> -<pre class="perl"> +<pre class="perl prettyprint"> my $num = 4040.5; print $num; # "4040.5" </pre> -<pre class="perl"> +<pre class="perl prettyprint"> my $string = "world"; print $string; # "world" </pre> @@ -152,19 +154,19 @@ <p class=original>String concatenation using the <tt>.</tt> operator (same as PHP):</p> <p>æååã®é£çµã«ã¯<tt>.</tt>æ¼ç®åã使ãã¾ã(PHPã¨åã):</p> -<pre class="perl"> +<pre class="perl prettyprint"> print "Hello ".$string; # "Hello world" </pre> <p class=original>String concatenation by passing multiple arguments to <tt>print</tt>:</p> <p>è¤æ°ã®å¼æ°ã<tt>print</tt>ã«æ¸¡ããã¨ã§æååãé£çµã§ãã¾ã:</p> -<pre class="perl">print "Hello ", $string; # "Hello world"</pre> +<pre class="perl prettyprint">print "Hello ", $string; # "Hello world"</pre> <p class=original><strong>It is impossible to determine whether a scalar contains a "number" or a "string".</strong> More precisely, it is irrelevant. Perl is weakly typed in this respect. 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 (or raise 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"; @@ -182,7 +184,7 @@ # String operators: lt, gt, le, ge, eq, ne, cmp </pre> -<pre class="perl"> +<pre class="perl prettyprint"> # æ°åç¨ã®æ¼ç®å: <, >, <=, >=, ==, !=, <=> # æåç¨ã®æ¼ç®å: lt, gt, le, ge, eq, ne, cmp </pre> @@ -220,7 +222,7 @@ ); </pre> -<pre class="perl"> +<pre class="perl prettyprint"> my @array = ( "print", "these", @@ -235,7 +237,7 @@ <p>é åããå¤ã«ã¢ã¯ã»ã¹ããã¨ãã«ã¯ãã«è¨å·ã使ããªããã°ããã¾ããã<em>åããã</em>å¤ã¯é åã§ã¯ãªããã¹ã«ã©ã ããã§ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> print $array[0]; # "print" print $array[1]; # "these" print $array[2]; # "strings" @@ -248,7 +250,7 @@ <p class=original>You can use negative indices to retrieve entries starting from the end and working backwards:</p> <p>è² ã®ã¤ã³ããã¯ã¹ããå¾ãããå¤ãåãã®ã«ä½¿ãã¾ããéåãã«ãªãã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> print $array[-1]; # "me" print $array[-2]; # "for" print $array[-3]; # "out" @@ -265,7 +267,7 @@ <p class=original>To get an array's length:</p> <p>é åã®é·ããå¾ãã«ã¯:</p> -<pre class="perl"> +<pre class="perl prettyprint"> print "This array has ", (scalar @array), "elements"; # "This array has 6 elements" print "The last populated index is ", $#array; # "The last populated index is 5" </pre> @@ -273,14 +275,14 @@ <p class=original>String concatenation using the <tt>.</tt> operator:</p> <p>æååã®çµåã«ã¯<tt>.</tt>æ¼ç®åã使ãã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> print $array[0].$array[1].$array[2]; # "printthesestrings" </pre> <p class=original>String concatenation by passing multiple arguments to <tt>print</tt>:</p> <p>è¤æ°ã®å¼æ°ã<tt>print</tt>ã«æ¸¡ããã¨ã§æååãé£çµã§ãã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> print @array; # "printthesestringsoutforme" </pre> @@ -289,7 +291,7 @@ <p class=original>Variables can be interpolated into strings:</p> <p>å¤æ°ãæååã®éã«å ¥ãããã¨ãã§ãã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> print "Hello $string"; # "Hello world" print "@array"; # "print these strings out for me" </pre> @@ -297,7 +299,7 @@ <p class=original><strong>Caution.</strong> One day you will put somebody's email address inside a string, <tt>"jeff****@gmail*****"</tt>. This will cause Perl to look for an array variable called <tt>@gmail</tt> to interpolate into the string, and not find it, resulting in a runtime error. Interpolation can be prevented in two ways: by backslash-escaping the sigil, or by using single quotes instead of double quotes.</p> <p><strong>注æã</strong> ããæ¥ã誰ãã®ã¡ã¼ã«ã¢ãã¬ã¹ã<tt>"jeff****@gmail*****"</tt>ãæååã«å ¥ããã¨ãã¾ãã ããã¯ãPerlã«<tt>@gmail</tt>ã¨ããé åå¤æ°ãæ¢ãããæååã®éã«å ¥ãããã¨ãã¾ãããããè¦ã¤ãããªããã°ãã¨ã©ã¼ã«ãªãã¾ããå¤æ°ã®å±éãé²ãã«ã¯2ã¤ã®æ¹æ³ãããã¾ã:ã·ã¸ã«ãã¨ã¹ã±ã¼ããããã¾ã¦ãããããã«ã¯ã©ã¼ãã®ä»£ããã«ã·ã³ã°ã«ã¯ã©ã¼ãã使ãã</p> -<pre class="perl"> +<pre class="perl prettyprint"> print "Hello \$string"; # "Hello $string" print 'Hello $string'; # "Hello $string" print "\@array"; # "@array" @@ -310,7 +312,7 @@ <p class=original>A hash variable is a list of scalars indexed by strings. In Python this is known as a <i>dictionary</i>, and in PHP it is known as an <i>array</i>.</p> <p>ããã·ã¥å¤æ°ã¯æåã§ã¤ã³ããã¯ã¹ãããç´ ããã®ãªã¹ãã§ããPythonã§ã¯<i>dictionary</i>ãPHPã§ã¯<i>array</i>ã«ãªãã¾ãã</p> -<pre class="perl"> +<pre class="perl prettyprint"> my %scientists = ( "Newton" => "Isaac", "Einstein" => "Albert", @@ -331,7 +333,7 @@ print $scientists{"Dyson"}; # runtime error - key not set </pre> -<pre class="perl"> +<pre class="perl prettyprint"> print $scientists{"Newton"}; # "Isaac" print $scientists{"Einstein"}; # "Albert" print $scientists{"Darwin"}; # "Charles" @@ -345,7 +347,7 @@ <p class=original>You can convert a hash straight to an array with twice as many entries, alternating between key and value (and the reverse is equally easy):</p> <p>ã¨ã³ããªã2åã«ãã¦ããã·ã¥ãé åã«ç´æ¥å¤æãããã¨ãããã¼ã¨å¤ãå¤æ´ãããã¨ãã§ãã¾ã(ãã®éãã¾ãç°¡åã§ã):</p> -<pre class="perl"> +<pre class="perl prettyprint"> my @scientists = %scientists; </pre> @@ -353,14 +355,14 @@ <p>ã§ãããé åã¨ã¯éããããã·ã¥ã®ãã¼ã¯<em>é çªãããã¾ãã</em>ãããå¹ççãªé çªã§è¿ã£ã¦ãã¾ãããã®ãããæ´åããç´ããã<em>é çª</em>ã«æ°ãã¤ãã¦ãã ãããããããçµæã®é åã®<em>ãã¢</em>ã¯ä¿æããã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> print @scientists; # something like "EinsteinAlbertDarwinCharlesNewtonIsaac" </pre> <p class=original>To recap, you have to use <strong>square brackets</strong> to retrieve a value from an array, but you have to use <strong>braces</strong> to retrieve a value from a hash. The square brackets are effectively a numerical operator and the braces are effectively a string operator. The fact that the <em>index</em> supplied is a number or a string is of absolutely no significance:</p> <p>è¦ç¹ãã¾ã¨ããã¨ãé åããå¤ãåãåºãã®ã«ã¯<strong>åè§ããã©ã±ãã</strong>ã使ããªããã°ããã¾ããããããã·ã¥ããå¤ãåãåºãã®ã¯<strong>ãã¬ã¼ã¹</strong>ã使ããªããã°ããã¾ãããæä¾ããã<em>ã¤ã³ããã¯ã¹</em>ãæ°åã§ãããæååã§ããã¨ãããã¨ã«ã¯ãéè¦æ§ã¯ããã¾ãã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> my $data = "orange"; my @data = ("purple"); my %data = ( "0" => "blue"); @@ -378,7 +380,7 @@ <p class=original>A <i>list</i> in Perl is a different thing again from either an array or a hash. You've just seen several lists:</p> <p>Perlã«ããã<i>ãªã¹ã</i>ã¯é åãããã·ã¥ã¨ã¯éããã®ã§ããæ¢ã«ããã¤ãã®ãªã¹ããããã¾ãã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> ( "print", "these", @@ -400,7 +402,7 @@ <p class=original>Okay. Remember that <tt>=></tt> is just <tt>,</tt> in disguise and then look at this example:</p> <p>ããã§ãããã <tt>=></tt>ã¯ããã ã®<tt>,</tt>ã§ãããã¨ãæãåºããè¿éããã¦ã次ã®ä¾ãè¦ã¦ãã ãã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> (0, 1, 2, 3, 4, 5) (0 => 1, 2 => 3, 4 => 5) </pre> @@ -408,7 +410,7 @@ <p class=original>The use of <tt>=></tt> hints that one of these lists is an array declaration and the other is a hash declaration. But on their own, neither of them are declarations of anything. They are just lists. <em>Identical</em> lists. Also:</p> <p><tt>=></tt>ã®ä½¿ãæ¹ãä¸æ¹ã®ãªã¹ããé åã®å®£è¨ã§ãããã¨ã示ããä»æ¹ã¯ããã·ã¥ã®å®£è¨ã§ãããã¨ã示ãã¦ãã¾ããã§ããã2ã¤ã¨ããããèªèº«ãä½ã®å®£è¨ã§ãããã¾ããããã ã®ãªã¹ãã§ãã<em>åä¸ã®</em>ãªã¹ãã§ããã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> () </pre> @@ -416,7 +418,7 @@ <p>ãªãã®ãã³ããåå¨ãã¾ããããã®ãªã¹ãã¯ç©ºã®é åã¨ãã¦å®£è¨ããã¦ããã®ã§ãããããããã¨ããã«ã©ã®ããã·ã¥ã¨ãã¦ã§ããããã<tt>perl</tt>ã¤ã³ã¿ã¼ããªã¿ã«ã¯ãæããã«ã©ã¡ãã¨ãå¤æãããã¨ãã§ãã¾ãããPerlã®å¤ãã£ãä¸é¢ã§ããã¨ç解ãããªãã次ã®äºå®ãçã§ãããã¨ãã¾ãç解ããã§ããã: <strong>ãªã¹ãã®å¤ã¯ãã¹ãã§ãã¾ããã</strong> 試ãã¦ã¿ã¦ãã ãã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> my @array = ( "apples", "bananas", @@ -472,7 +474,7 @@ print $hash{"bananas"}{"yellow"}; # error </pre> -<pre class="perl"> +<pre class="perl prettyprint"> my %hash = ( "beer" => "good", "bananas" => ( @@ -506,13 +508,13 @@ <p><tt>my $scalar =</tt>ã®ãããªã¹ã«ã©ã®å®£è¨ã¯ã¹ã«ã©ã³ã³ããã¹ãã¨ãã¦è©ä¾¡ããã¾ãã<tt>"Mendeleev"</tt>ã®ãããªã¹ã«ã©ã®å¤ãã¹ã«ã©ä»æµãã¨ã§è©ä¾¡ãããã¹ã«ã©ãè¿ãã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> my $scalar = "Mendeleev"; </pre> <p class=original>An array or hash declaration such as <tt>my @array =</tt> or <tt>my %hash =</tt> evaluates its expression in list context. A list value evaluated in list context returns the list, which then gets fed in to populate the array or hash:</p> <p><tt>my @array =</tt> ã <tt>my %hash =</tt> ã®ãããªé åãããã·ã¥ã®å®£è¨ã¯ããªã¹ãã³ã³ããã¹ãã§è©ä¾¡ããã¾ãããªã¹ãã®å¤ã¯ãªã¹ãã³ã³ããã¹ãã§è©ä¾¡ããããªã¹ããè¿ãã¾ããé åãããã·ã¥ã«ä»£å ¥ãããããªã¨ãã§ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> my @array = ("Alpha", "Beta", "Gamma", "Pie"); my %hash = ("Alpha" => "Beta", "Gamma" => "Pie"); </pre> @@ -523,21 +525,21 @@ <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"; print $array[0]; # "Mendeleev" print scalar @array; # "1" </pre> <p class=original>A list expression evaluated in scalar context returns <em>the final scalar in the list</em>:</p> <p>ãªã¹ãã®å¼ãã¹ã«ã©ã³ã³ããã¹ãã§è©ä¾¡ãããã¨ã<em>ãªã¹ãã®æå¾ã®ã¹ã«ã©</em>ãè¿ãã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> my $scalar = ("Alpha", "Beta", "Gamma", "Pie"); print $scalar; # "Pie" </pre> <p class=original>An array expression (an array is different from a list, remember?) evaluated in scalar context returns <em>the length of the array</em>:</p> <p>é åã®å¼(é åã¯ãªã¹ãã¨éãã¾ããè¦ãã¦ãï¼)ã¯ãã¹ã«ã©ã³ã³ããã¹ãã§ã¯<em>é åã®é·ã</em>ãè¿ãã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> my @array = ("Alpha", "Beta", "Gamma", "Pie"); my $scalar = @array; print $scalar; # "4" @@ -550,13 +552,13 @@ <!-- <p>As a more complicated example, let's examine what happens when we perform a regular expression match. A regex match expression evaluated in scalar context returns <tt>""</tt> (the empty string) if the match fails, <tt>1</tt> on success:</p> -<pre class="perl"> +<pre class="perl prettyprint"> my $bill = "The amount was \$123.45. You must pay immediately."; my $scalar = ($bill =~ m/^The amount was \$(\d+\.\d\d)\. You must (\w+) ([\w\s]+)\.$/); print $scalar; # "1" </pre> <p>The same expression evaluated in list context returns a list of matches:</p> -<pre class="perl"> +<pre class="perl prettyprint"> my $bill = "The amount was \$123.45. You must pay immediately."; my @array = ($bill =~ m/^The amount was \$(\d+\.\d\d)\. You must (\w+) ([\w\s]+)\.$/); print @array; # "123.45payimmediately" @@ -569,7 +571,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. For example:</p> <p>ãªã¹ããè¦ç´ ã¨ãã¦ãªã¹ããå«ããªãã®ã¨åæ§ã<strong>é åã¨ããã·ã¥ã¯ä»ã®é åãããã·ã¥ãè¦ç´ ã¨ãã¦æã¦ã¾ãã</strong>ã 両æ¹ã¨ãã¹ã«ã©ããæã¦ã¾ããã ä¾ãã°:</p> -<pre class="perl"> +<pre class="perl prettyprint"> my @outer = (); my @inner = ("Mercury", "Venus", "Earth"); @@ -587,14 +589,14 @@ <p class=original>A reference is created using a backslash.</p> <p>ãªãã¡ã¬ã³ã¹ã¯ããã¯ã¹ã©ãã·ã¥ã使ã£ã¦ä½ããã¾ãã</p> -<pre class="perl"> +<pre class="perl prettyprint"> my $colour = "Indigo"; my $scalarRef = \$colour; </pre> <p class=original>Any time you would use the name of a variable, you can instead just put some braces in, and, within the braces, put a <em>reference</em> to a variable instead.</p> <p>ãã¤ã§ããå¤æ°ã®ååã使ãã¾ãã代ããã«ãã¬ã¼ã¹ãç½®ãã¦ããã¬ã¼ã¹å ã«å¤æ°ã¸ã®<em>ãªãã¡ã¬ã³ã¹</em>ãç½®ãã¾ãã</p> -<pre class="perl"> +<pre class="perl prettyprint"> print $colour; # "Indigo" print $scalarRef; # e.g. "SCALAR(0x182c180)" print ${ $scalarRef }; # "Indigo" @@ -603,14 +605,14 @@ <p class=original>As long as the result is not ambiguous, you can omit the braces too:</p> <p>çµæãææ§ã§ãªãéãããã¬ã¼ã¹ãçç¥ãããã¨ãã§ãã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> print $$scalarRef; # "Indigo" </pre> <p class=original>If your reference is a reference to an array or hash variable, you can get data out of it using braces or using the more popular arrow operator, <tt>-></tt>:</p> <p>ãªãã¡ã¬ã³ã¹ãé åãããã·ã¥å¤æ°ã®ãªãã¡ã¬ã³ã¹ã®å ´åããã¬ã¼ã¹ãããä¸è¬çãªã¢ãã¼æ¼ç®åã<tt>-></tt>ã使ã£ã¦ãã¼ã¿ãåãåºãã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> my @colours = ("Red", "Orange", "Yellow", "Green", "Blue"); my $arrayRef = \@colours; @@ -629,7 +631,7 @@ <h3 class=original>Declaring a data structure</h3> <h3>ãã¼ã¿æ§é ã宣è¨ãã</h3> -<pre class="perl"> +<pre class="perl prettyprint"> my %owner1 = ( "name" => "Santa Claus", "DOB" => "1882-12-25", @@ -658,7 +660,7 @@ <p class=original>That's obviously unnecessarily laborious, because you can shorten it to:</p> <p>ããã¯ãæããã«ä¸å¿ è¦ã§éª¨ã®æãã¾ãããªããªãã次ã®ããã«çç¥ã§ãã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> my %owner1 = ( "name" => "Santa Claus", "DOB" => "1882-12-25", @@ -682,7 +684,7 @@ <p> å¥ã®è¨å·ã使ã£ã¦<i>ç¡å</i>é åãããã·ã¥ã宣è¨ãããã¨ãåºæ¥ã¾ããåè§ããã©ã±ãããç¡åé åã«ããã¬ã¼ã¹ãç¡åããã·ã¥ã«ä½¿ãã¾ãããããããè¿ãããå¤ã¯ãç¡åã®ãã¼ã¿æ§é ã®<em>ãªãã¡ã¬ã³ã¹</em>ã«ãªãã¾ãã注æãã¦è¦ã¦ãã ããã次ã®çµæã¯ãä¸ã®<tt>%account</tt>ã¨å ¨ãåãã§ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> # Braces denote an anonymous hash my $owner1Ref = { "name" => "Santa Claus", @@ -707,7 +709,7 @@ <p class=original>Or, for short (and this is the form you should <em>actually</em> use when declaring complex data structures in-line):</p> <p>ã¾ãã¯ãçç¥ããããã¨(ããã¦ãè¡ã§ãã¼ã¿è¤éãªæ§é ã宣è¨ããæã«ã<em>å®éã«</em>使ãã¹ãå½¢ã§ã):</p> -<pre class="perl"> +<pre class="perl prettyprint"> my %account = ( "number" => "31415926", "opened" => "3000-01-01", @@ -731,7 +733,7 @@ <p>ãã¦ãããããã¾ãã<tt>%account</tt>ãã¾ã ããã¨ãã¾ããããã§ãããå ¨ã¦ã®ä»ã®ãã®(ä»ã®ãã®ããã£ããªã)ã¯ãã¹ã³ã¼ãã®å¤ã«è½ã¡ã¾ããããããã®ã±ã¼ã¹ã§åãæé ãéåãã«ãããã¨ã§ãæ å ±ã表示ã§ãã¾ãã</p> -<pre class="perl"> +<pre class="perl prettyprint"> my $ownersRef = $account{"owners"}; my @owners = @{ $ownersRef }; my $owner1Ref = $owners[0]; @@ -748,7 +750,7 @@ <p class=original>Or, for short:</p> <p>ã¾ãã¯, çç¥ãã¦:</p> -<pre class="perl"> +<pre class="perl prettyprint"> my @owners = @{ $account{"owners"} }; my %owner1 = %{ $owners[0] }; my %owner2 = %{ $owners[1] }; @@ -762,7 +764,7 @@ <p class=original>Or using references:</p> <p>ã¾ãã¯ããªãã¡ã¬ã³ã¹ã使ã£ã¦:</p> -<pre class="perl"> +<pre class="perl prettyprint"> my $ownersRef = $account{"owners"}; my $owner1Ref = $ownersRef->[0]; my $owner2Ref = $ownersRef->[1]; @@ -776,7 +778,7 @@ <p class=original>And if we completely skip all the intermediate values:</p> <p>ããã¦ãå ¨ã¦ã®ä¸éã®å¤ãã¹ããããããªã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> print "Account #", $account{"number"}, "\n"; print "Opened on ", $account{"opened"}, "\n"; print "Joint owners:\n"; @@ -789,21 +791,21 @@ <p class=original>This array has five elements:</p> <p>ãã®é åã«ã¯5ã¤ã®è¦ç´ ãããã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> my @array1 = (1, 2, 3, 4, 5); print @array1; # "12345" </pre> <p class=original>This array has one element (which happens to be a reference to an anonymous, five-element array):</p> <p>ãã®é åã«ã¯ã²ã¨ã¤ã®è¦ç´ (ç¡åã®5ã¤ã®è¦ç´ ã®é åã®ãªãã¡ã¬ã³ã¹)ãããã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> my @array2 = [1, 2, 3, 4, 5]; print @array2; # e.g. "ARRAY(0x182c180)" </pre> <p class=original>This <em>scalar</em> is a reference to an anonymous, five-element array:</p> <p>ãã® <em>ã¹ã«ã©</em> ã¯ãç¡åã®5ã¤ã®è¦ç´ ã®é åã®ãªãã¡ã¬ã³ã¹ã«ãªãã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> my $array3Ref = [1, 2, 3, 4, 5]; print $array3Ref; # e.g. "ARRAY(0x22710c0)" print @{ $array3Ref }; # "12345" @@ -818,7 +820,7 @@ <p class=original>No surprises here, other than the spelling of <tt>elsif</tt>:</p> <p><tt>elsif</tt>ã®ã¹ãã«ä»¥å¤ã«ã¯é©ããã®ã¯ããã¾ãã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> my $word = "antidisestablishmentarianism"; my $strlen = length $word; @@ -834,13 +836,13 @@ <p class=original>Perl provides a shorter "<i>statement</i> <tt>if</tt> <i>condition</i>" syntax which is highly recommended for short statements:</p> <p>Perlã«ã¯ããçã "<i>ã¹ãã¼ãã¡ã³ã</i> <tt>if</tt> <i>æ¡ä»¶</i>"ã®ã·ã³ã¿ãã¯ã¹ãããã¾ããçãã¹ãã¼ãã¡ã³ãç¨ã«ãå¼·ãæ¨å¥¨ããã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> print "'", $word, "' is actually enormous" if $strlen >= 20; </pre> <h3><tt>unless</tt> ... <tt>else</tt> ...</h3> -<pre class="perl"> +<pre class="perl prettyprint"> my $temperature = 20; unless($temperature > 30) { @@ -856,7 +858,7 @@ <p class=original>This, by comparison, is highly recommended because it is so easy to read:</p> <p>ä¸æ¹ã§ã以ä¸ã¯èªã¿ãããã®ããã«ãå¼·ãæ¨å¥¨ããã¾ã</p> -<pre class="perl"> +<pre class="perl prettyprint"> print "Oh no it's too cold" unless $temperature > 15; </pre> @@ -866,7 +868,7 @@ <p class=original>The ternary operator <tt>?:</tt> allows simple <tt>if</tt> statements to be embedded in a statement. The canonical use for this is singular/plural forms:</p> <p>ä¸é æ¼ç®å <tt>?:</tt> ã¯ãåç´ãª <tt>if</tt> ã¹ãã¼ãã¡ã³ããã²ã¨ã¤ã®ã¹ãã¼ãã¡ã³ãã«åãè¾¼ãã¾ããä¸é æ¼ç®åã®æ¨æºçãªä½¿ãæ¹ã¨ãã¦ãåæ°/è¤æ°ã®å½¢ãããã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> my $gain = 48; print "You gained ", $gain, " ", ($gain == 1 ? "experience point" : "experience points"), "!"; </pre> @@ -875,7 +877,7 @@ <p>ä½è«: 両æ¹ã®ã±ã¼ã¹ã®åæ°ã¨è¤æ°ãå®å ¨ã«æ¸ãåºããã¦ãã¾ãã決ãã¦ä»¥ä¸ã®ãããªå·§å¦ãªãã¨ãããªãã§ãã ããã ã³ã¼ããæ¤ç´¢ãã¦ã"tooth"ã"teeth"ã®åèªãç½®ãæãããã¨ãã¦ãããã®è¡ããè¦ã¤ãããã¨ãã§ãã¾ããã</p> -<pre class="perl"> +<pre class="perl prettyprint"> my $lost = 1; print "You lost ", $lost, " t", ($lost == 1 ? "oo" : "ee"), "th!"; </pre> @@ -883,7 +885,7 @@ <p class=original>Ternary operators may be nested:</p> <p>ä¸é æ¼ç®åã¯ãã¹ãã§ãã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> my $eggs = 5; print "You have ", $eggs == 0 ? "no eggs" : $eggs == 1 ? "an egg" : @@ -903,7 +905,7 @@ <p class=original>Basic C-style <tt>for</tt> loops are available, but these are obtuse and old-fashioned and should be avoided. Notice how we have to put a <tt>my</tt> in front of our iterator <tt>$i</tt>, in order to declare it:</p> <p>åºæ¬çãªC-styleã®<tt>for</tt>ã«ã¼ãã¯å©ç¨ã§ãã¾ããããããã«ãããå¤èãã®ã§ã使ãã¹ãã§ã¯ããã¾ããã<tt>my</tt>ãã¤ãã¬ã¼ã¿<tt>$i</tt>ã®åã«æªãã¦ã次ã®ããã«å®£è¨ãã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> for(my $i = 0; $i < scalar @array; $i++) { print $i, ": ", $array[$i]; } @@ -911,7 +913,7 @@ <p class=original>Native iteration over an array is much nicer. Note: unlike PHP, the <tt>for</tt> and <tt>foreach</tt> keywords are synonyms. Just use whatever looks most readable:</p> <p>é åã®ãã¤ãã£ãã®ã¤ãã¬ã¼ã·ã§ã³ã¯ããç°¡åã§ãã注æ: PHPã¨éãã <tt>for</tt> 㨠<tt>foreach</tt> ãã¼ã¯ã¼ãã¯ã·ããã ã§ãããã£ã¨ãèªã¿ããããã®ã®ããããã使ã£ã¦ãã ãã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> foreach my $string ( @array ) { print $string; } @@ -920,7 +922,7 @@ <p class=original>If you do need the indices, the range operator <tt>..</tt> creates an anonymous array of integers:</p> <p>è¤æ°ã®ã¤ã³ããã¯ã¹ãå¿ è¦ãªããç¯å²æ¼ç®å<tt>..</tt>ã§æ´æ°ã®ç¡åé åãä½ãã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> foreach my $i ( 0 .. $#array ) { print $i, ": ", $array[$i]; } @@ -931,7 +933,7 @@ If you don't provide an explicit iterator, Perl uses a default iterator, . <tt>$_</tt>ã¯æåã®ãã¬ã³ããªã¼ãªçµè¾¼ã®å¤æ°ã§ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> foreach ( @array ) { print $_; } @@ -939,7 +941,7 @@ <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"> +<pre class="perl prettyprint"> print $_ foreach @array; </pre> @@ -952,7 +954,7 @@ <p class=original>You can't iterate over a hash. However, you can iterate over its keys. Use the <tt>keys</tt> built-in function to retrieve an array containing all the keys of a hash. Then use the <tt>foreach</tt> approach that we used for arrays:</p> <p>ããã·ã¥ã¯ã¤ãã¬ã¼ãã§ãã¾ããããã®ãã¼ãã¤ãã¬ã¼ãã§ãã¾ããçµè¾¼é¢æ°ã®<tt>keys</tt>ã使ã£ã¦ãããã·ã¥ã®å ¨ã¦ã®ãã¼ãå«ãé åãåãåºãã¦ãã ããããããããé åã§ä½¿ã£ã<tt>foreach</tt>ã®ã¢ããã¼ãã使ãã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> foreach my $key (keys %scientists) { print $key, ": ", $scientists{$key}; } @@ -960,7 +962,7 @@ <p class=original>Since a hash has no underlying order, the keys may be returned in any order. Use the <tt>sort</tt> built-in function to sort the array of keys alphabetically beforehand:</p> <p>ããã·ã¥ã«ã¯é çªãããã¾ããã®ã§ãkeysã¯ã©ã®ãããªé çªã§ãæ»ãã¾ããçµè¾¼ã®<tt>sort</tt>é¢æ°ã使ã£ã¦ãã¢ã«ãã¡ãããé ã§ãã¼ã®é åãã½ã¼ãã§ãã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> foreach my $key (sort keys %scientists) { print $key, ": ", $scientists{$key}; } @@ -969,7 +971,7 @@ <p class=original>There is also a special <tt>each</tt> built-in function which retrieves key/value pairs one at a time. Every time <tt>each</tt> is called, it returns an array containing two values, until the end of the array is reached, when a false value is returned. We assign the values of two scalars to the values of the array, simultaneously:</p> <p>ç¹å¥ãªçµè¾¼é¢æ°<tt>each</tt>ããããããã¯ããã¼/å¤ã®ãã¢ãä¸åº¦ã«ã²ã¨ã¤åãåºãã¾ãã<tt>each</tt>ãå¼ã°ãããã³ã«ãé åã®æå¾ã«è³ãã¾ã§ãå½ã®å¤ãæ»ãã¾ã§ã2ã¤ã®å¤ããã¤é åãè¿ãã¾ããé åã®å¤ã2ã¤ã®ã¹ã«ã©ã«ãåæã«å²ãå½ã¦ã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> while( my ($key, $value) = each %scientists ) { print $key, ": ", $value; } @@ -982,7 +984,7 @@ <p><tt>next</tt> 㨠<tt>last</tt>ã¯ã«ã¼ãé²ã¿ãå¶å¾¡ããã®ã«ä½¿ããã¾ããå¤ãã®ããã°ã©ãã³ã°è¨èªã§ã¯ãããããã<tt>continue</tt> 㨠<tt>break</tt>ã¨ãªã£ã¦ãã¾ãããªãã·ã§ã³ã§ãã©ã®ã«ã¼ãã«ãã©ãã«ãã¤ãããã¨ãã§ãã¾ããæ £ä¾ã«ãããã©ãã«ã¯ <tt>å ¨ã¦å¤§æåã§</tt>æ¸ããã¨ã«ãªã£ã¦ãã¾ããã«ã¼ãã«ã©ãã«ãã¤ãããã¨ã§ã<tt>next</tt> 㨠<tt>last</tt> ã«ã©ãã«ã対象ã«ã§ãã¾ããé åããå ¨ã¦ã®æ¶ç©ºã§ãªãåç©ããªã¹ãããä¾ã§ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> my @input = ( "dragon", "camel", "cow", "pangolin", "unicorn", "pig", "sheep", "donkey", "pig", "basilisk", @@ -1016,42 +1018,42 @@ <p>We'll use <tt>@stack</tt> to demonstrate these:</p> -<pre class="perl"> +<pre class="perl prettyprint"> my @stack = ("Fred", "Eileen", "Denise", "Charlie"); print @stack; # "FredEileenDeniseCharlie" </pre> <p class=original><tt>pop</tt> extracts and returns the final element of the array. This can be thought of as the top of the stack:</p> <p><tt>pop</tt> ã¯é åã®æå¾ã®è¦ç´ ãå¼ãåºãã¦è¿ãã¾ããã¹ã¿ãã¯ã®ä¸ã¨ãã¦èãããã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> print pop @stack; # "Charlie" print @stack; # "FredEileenDenise" </pre> <p class=original><tt>push</tt> appends extra elements to the end of the array:</p> <p><tt>push</tt> ã¯è¿½å ã®è¦ç´ ãé åã®æå¾ã«ä»å ãã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> push @stack, "Bob", "Alice"; print @stack; # "FredEileenDeniseBobAlice" </pre> <p class=original><tt>shift</tt> extracts and returns the first element of the array:</p> <p><tt>shift</tt> ã¯é åã®æåã®è¦ç´ ãå¼ãåºãã¦è¿ãã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> print shift @stack; # "Fred" print @stack; # "EileenDeniseBobAlice" </pre> <p class=original><tt>unshift</tt> inserts new elements at the beginning of the array:</p> <p><tt>unshift</tt> é åã®æåã«æ°ããè¦ç´ ãæ¿å ¥ãã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> unshift @stack, "Hank", "Grace"; print @stack; # "HankGraceEileenDeniseBobAlice" </pre> <p class=original><tt>pop</tt>, <tt>push</tt>, <tt>shift</tt> and <tt>unshift</tt> are all special cases of <tt>splice</tt>. <tt>splice</tt> removes and returns an array slice, replacing it with a different array slice:</p> <p><tt>pop</tt>ã<tt>push</tt>ã <tt>shift</tt>ã<tt>unshift</tt> ã¯ãå ¨ã¦ã<tt>splice</tt>ã®ç¹å¥ãªã±ã¼ã¹ã§ãã<tt>splice</tt> ã¯ãé åã®ã¹ã©ã¤ã¹ãåé¤ãã¦ãè¿ãã¾ããå¥ã®é åã¹ã©ã¤ã¹ã§ãããç½®ãæãã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> print splice(@stack, 1, 4, "<<<", ">>>"); # "GraceEileenDeniseBob" print @stack; # "Hank<<<>>>Alice" </pre> @@ -1067,7 +1069,7 @@ <p class="original">The <tt>join</tt> function concatenates many strings into one:</p> <p><tt>join</tt> é¢æ°ã¯å¤ãã®æååãä¸ã¤ã«çµåãã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> my @elements = ("Antimony", "Arsenic", "Aluminum", "Selenium"); print @elements; # "AntimonyArsenicAluminumSelenium" print "@elements"; # "Antimony Arsenic Aluminum Selenium" @@ -1079,7 +1081,7 @@ <p class=original>The <tt>reverse</tt> function returns an array in reverse order:</p> <p><tt>reverse</tt> é¢æ°ã¯é çªãéã«ããé åãè¿ãã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> my @numbers = ("one", "two", "three"); print reverse(@numbers); # "threetwoone" </pre> @@ -1089,7 +1091,7 @@ <p class=original>The <tt>map</tt> function takes an array as input and applies an operation to every scalar <tt>$_</tt> in this array. It then constructs a new array out of the results. The operation to perform is provided in the form of a single expression inside braces:</p> <p><tt>map</tt>é¢æ°ã¯å ¥åã¨ãã¦é åãã¨ããé åå ã®å ¨ã¦ã®ã¹ã«ã© <tt>$_</tt>ãæä½ãã¾ããçµæã¨ãã¦æ°ããé åãä½ãã¾ããæä½ã¯ã²ã¨ã¤ã®ãã¬ã¼ã¹ã§æ¸¡ãã¾ã::</p> -<pre class="perl"> +<pre class="perl prettyprint"> my @capitals = ("Baton Rouge", "Indianapolis", "Columbus", "Montgomery", "Helena", "Denver", "Boise"); print join ", ", map { uc $_ } @capitals; @@ -1101,7 +1103,7 @@ <p class=original>The <tt>grep</tt> function takes an array as input and returns a filtered array as output. The syntax is similar to <tt>map</tt>. This time, the second argument is evaluated for each scalar <tt>$_</tt> in the input array. If a boolean true value is returned, the scalar is put into the output array, otherwise not.</p> <p><tt>grep</tt>é¢æ°ã¯å ¥åã¨ãã¦é åãã¨ãããã£ã«ã¿ã¼ãããé åãåºåãã¾ããã·ã³ã¿ãã¯ã¹ã¯<tt>map</tt>ã¨ä¼¼ã¦ãã¾ããä»åº¦ã¯ã第äºå¼æ°ã¯å ¥åãããé åã®åã¹ã«ã©<tt>$_</tt>ãè©ä¾¡ããã¾ãããã¼ãªã¢ã³ã§çã®å¤ãæ»ãã°ãã¹ã«ã©ã¯é åã¨ãã¦åºåããã¾ãããããã§ãªããã°ãåºåããã¾ããã</p> -<pre class="perl"> +<pre class="perl prettyprint"> print join ", ", grep { length $_ == 6 } @capitals; # "Helena, Denver" </pre> @@ -1109,7 +1111,7 @@ <p class="original">Instead of a single Perl expression, you may supply a regular expression. In this case, the scalar is put into the output array only if the regular expression matches <tt>$_</tt>:</p> <p>åä¸ã®Perlã®å¼ã§ã¯ãªããæ£è¦è¡¨ç¾ã渡ãã¾ãã次ã®ã±ã¼ã¹ã§ã¯ãæ£è¦è¡¨ç¾ã<tt>$_</tt>ã«ãããããå ´åã®ã¿ãã¹ã«ã©ãé åã«åºåããã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> print join ", ", grep m/^[B-H]/, @capitals; # "Baton Rouge, Columbus, Helena, Denver, Boise" </pre> @@ -1117,7 +1119,7 @@ <p class=original>Obviously, the length of the resulting array is the <em>number of successful matches</em>, which means you can use <tt>grep</tt> to quickly check whether an array contains an element:</p> <p>å½ç¶ãçµæã®é åã®é·ãã¯ã<em>ãããã«æåããæ°</em>ã«ãªãã¾ãããã®ã¨ãã¨ã¯ã<tt>grep</tt>ãé åã«è¦ç´ ããããã©ãããç´ æ©ããã§ãã¯ããã®ã«ä½¿ãããã¨ãæå³ãã¾ãã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> print scalar grep { $_ eq "Columbus" } @capitals; # "1" </pre> @@ -1129,7 +1131,7 @@ <p class=original>By default, the <tt>sort</tt> function returns the input array, sorted into alphabetical order:</p> <p>ããã©ã«ãã§ã¯ã<tt>sort</tt>é¢æ°ã¯å ¥åãããé åãã¢ã«ãã¡ãããé ã«ä¸¦ã³ããã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> my @elevations = (19, 1, 2, 100, 3, 98, 100, 1056); print join ", ", sort @elevations; @@ -1142,7 +1144,7 @@ <p class=original>The <tt>cmp</tt> operator does exactly this for strings:</p> <p><tt>cmp</tt> æ¼ç®åã¯æååã«å¯¾ãã¦ãã¾ãã«ããããã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> print join ", ", sort { $a cmp $b } @elevations; # "1, 100, 100, 1056, 19, 2, 3, 98" </pre> @@ -1150,7 +1152,7 @@ <p class=original>The "spaceship operator", <tt><=></tt>, does the same for numbers:</p> <p>"ã¹ãã¼ã¹ã·ããæ¼ç®å", <tt><=></tt>ã¯ãæ°åã«å¯¾ãã¦åããã¨ããã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> print join ", ", sort { $a <=> $b } @elevations; # "1, 2, 3, 19, 98, 100, 100, 1056" </pre> @@ -1158,7 +1160,7 @@ <p class=original><tt>$a</tt> and <tt>$b</tt> are always scalars, but they can be references to quite complex objects which are difficult to compare. If you need more space for the comparison, you can create a separate subroutine and provide its name instead:</p> <p><tt>$a</tt> 㨠<tt>$b</tt> ã¯å¸¸ã«ã¹ã«ã©ã¼ã§ãããæ¯è¼ãé£ããé常ã«è¤éãªãªãã¸ã§ã¯ãã®ãªãã¡ã¬ã³ã¹ããããã¾ããæ¯è¼ã«ããã¹ãã¼ã¹ãå¿ è¦ãªããå¥ã®ãµãã«ã¼ãã³ãä½ãã代ããã«ãã®ååã渡ãã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> sub comparator { # lots of code... # return -1, 0 or 1 @@ -1208,28 +1210,34 @@ <h2 class=original>User-defined subroutines</h2> <h2>ã¦ã¼ã¶ã¼å®ç¾©ã®ãµãã«ã¼ãã³</h2> +<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>Subroutines are declared using the <tt>sub</tt> keyword. In contrast with built-in functions, user-defined subroutines always accept the same input: a list of scalars. That list may of course have a single element, or be empty. A single scalar is taken as a list with a single element. A hash with <var>N</var> elements is taken as a list with 2<var>N</var> elements.</p> <p>ãµãã«ã¼ãã³ã¯<tt>sub</tt> ãã¼ã¯ã¼ãã使ã£ã¦å®£è¨ããã¾ããçµè¾¼é¢æ°ã¨å¯¾ç §çã«ãã¦ã¼ã¶ã¼å®ç¾©ã®ãµãã«ã¼ãã³ã¯å¸¸ã«åãå ¥åãåãä»ãã¾ã: ã¹ã«ã©ã®ãªã¹ãã§ãããã®ãªã¹ãã¯ããã¡ãããã²ã¨ã¤ã®è¦ç´ ãã空ã®å ´åãããã¾ããã²ã¨ã¤ã®ã¹ã«ã©ã¯ä¸ã¤ã®è¦ç´ ã®ãªã¹ãã§ãã<var>N</var>è¦ç´ ã®ããã·ã¥ã¯2<var>N</var>è¦ç´ ã®ãªã¹ãã§ãã</p> -<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 built-in array variable <tt>@_</tt>. Examples follow.</p> <p>ãµãã«ã¼ãã³ã®ä¸ã§ã¯ãçµè¾¼ã®é åå¤æ°<tt>@_</tt>ã使ãã¾ããä¾ã¯ä»¥ä¸ã«ç¶ãã¾ãã</p> -<h3>Unpacking arguments</h3> -<p>There's More Than One Way To unpack these arguments, but some are superior to others.</p> -<p>The example subroutine <tt>leftPad</tt> below pads a string out to the required length using the supplied pad character. (The <tt>x</tt> function concatenates multiple copies of the same string in a row.) (Note: for brevity, these subroutines all lack some elementary error checking, i.e. ensuring the pad character is only 1 character, checking that the width is greater than or equal to the length of existing string, checking that all needed arguments were passed at all.)</p> -<p><tt>leftPad</tt> is typically invoked as follows:</p> -<pre class="perl"> +<h3 class=original>Unpacking arguments</h3> +<h3>å¼æ°ãåãåºã</h3> +<p class=original>There's More Than One Way To unpack these arguments, but some are superior to others.</p> +<p>å¼æ°ãåãåºãã®ã«ããã¤ãããæ¹ãããã¾ãã ã§ãããããã¤ãã¯ãã»ãã®ãã®ãããããè¯ãã§ãã</p> + +<p class=original>The example subroutine <tt>leftPad</tt> below pads a string out to the required length using the supplied pad character. (The <tt>x</tt> function concatenates multiple copies of the same string in a row.) (Note: for brevity, these subroutines all lack some elementary error checking, i.e. ensuring the pad character is only 1 character, checking that the width is greater than or equal to the length of existing string, checking that all needed arguments were passed at all.)</p> +<p>ä¾ã®ãµãã«ã¼ãã³<tt>leftPad</tt>ã¯ã以ä¸ã®ä¾ã¯ã渡ãããè©°ãè¾¼ã¿æåã使ã£ã¦ãå¿ è¦ãªé·ãã«ãªãã¾ã§æååã«ä»å ãã¾ãã(<tt>x</tt>é¢æ°ã¯è¡ã«åãæååã®è¤æ°ã®ã³ãã¼ãã¤ãªãã¾ã)ã(注æ: ç°¡æ½ãã®ããã«ããããã®ãµãã«ã¼ãã³ã¯å ¨ã¦åºæ¬çãªã¨ã©ã¼ãã§ãã¯ãè¡ã£ã¦ãã¾ãããä¾ãã°ãè©°ãè¾¼ã¿æåã1æåã®ã¿ã§ãããã¨ãä¿è¨¼ããã¨ããé·ããæ¢åã®æååã®é·ã以ä¸ã§ããããå¿ è¦ãªå¼æ°ãå ¨ã¦æ¸¡ããã¦ãããã©ããããªã©)ã</p> +<p class=original><tt>leftPad</tt> is typically invoked as follows:</p> +<p><tt>leftPad</tt> ã¯å ¸åçã«ã次ã®ããã«å¼ã°ãã¾ã:</p> +<pre class="perl prettyprint"> print leftPad("hello", 10, "+"); # "+++++hello" </pre> <ol> <li> - <p>Some people don't unpack the arguments at all and use <tt>@_</tt> "live". This is unreadable and discouraged:</p> -<pre class="perl"> + <p class=original>Some people don't unpack the arguments at all and use <tt>@_</tt> "live". This is unreadable and discouraged:</p> + <p>人ã«ãã£ã¦ã¯ãå¼æ°ãåãåºããã«<tt>@_</tt>ã"ãã®ã¾ã¾"使ãããããã¾ãããããã¯ãèªã¿ã«ãããæ¨å¥¨ããã¾ãã:</p> +<pre class="perl prettyprint"> sub leftPad { my $newString = ($_[2] x ($_[1] - length $_[0])) . $_[0]; return $newString; @@ -1237,8 +1245,9 @@ </pre> </li> <li> - <p>Unpacking <tt>@_</tt> is only slightly less strongly discouraged:</p> -<pre class="perl"> + <p class=original>Unpacking <tt>@_</tt> is only slightly less strongly discouraged:</p> + <p><tt>@_</tt>ã®åãåºãã¯ãã»ãã®å°ããã¤ããã®ã¯ãå¼·ãæ¨å¥¨ããã¾ãã:</p> +<pre class="perl prettyprint"> sub leftPad { my $oldString = $_[0]; my $width = $_[1]; @@ -1249,8 +1258,8 @@ </pre> </li> <li> - <p>Unpacking <tt>@_</tt> by removing data from it using <tt>shift</tt> is highly recommended for up to 4 arguments:</p> -<pre class="perl"> + <p class=original>Unpacking <tt>@_</tt> by removing data from it using <tt>shift</tt> is highly recommended for up to 4 arguments:</p + <p><tt>@_</tt>ãåãåºãã®ã«ã<tt>shift</tt>ã使ã£ã¦ã@_ãããã¼ã¿ãåé¤ããã®ã¯ãå¼æ°ã4ã¤ã¾ã§ãªãå¼·ãæ¨å¥¨ããã¾ã:</p<pre class="perl prettyprint"> sub leftPad { my $oldString = shift @_; my $width = shift @_; @@ -1259,8 +1268,9 @@ return $newString; } </pre> - <p>If no array is provided to the <tt>shift</tt> function, then it operates on <tt>@_</tt> implicitly. This approach is seen very commonly:</p> -<pre class="perl"> + <p class=original>If no array is provided to the <tt>shift</tt> function, then it operates on <tt>@_</tt> implicitly. This approach is seen very commonly:</p> + <p><tt>shift</tt>ã«é åã渡ããªããã°ãæé»ã«ã<tt>@_</tt>ã«å¯¾ãã¦æä½ãã¾ãããã®ã¢ããã¼ãã¯ã¨ã¦ãããè¦ããã¾ã:</p> + <pre class="perl prettyprint"> sub leftPad { my $oldString = shift; my $width = shift; @@ -1269,11 +1279,13 @@ return $newString; } </pre> - <p>Beyond 4 arguments it becomes hard to keep track of what is being assigned where.</p> + <p class=original>Beyond 4 arguments it becomes hard to keep track of what is being assigned where.</p> + <p>å¼æ°ã5ã¤ä»¥ä¸ã«ãªããªããã©ãã§å²ãå½ã¦ããã追ãç¶ããã®ãé£ãããªãã§ãããã</p> </li> <li> - <p>You can unpack <tt>@_</tt> all in one go using multiple simultaneous scalar assignment. Again, this is okay for up to 4 arguments:</p> -<pre class="perl"> + <p class=original>You can unpack <tt>@_</tt> all in one go using multiple simultaneous scalar assignment. Again, this is okay for up to 4 arguments:</p> + <p><tt>@_</tt>ã®åãåºãããåæã«å ¨ã¦ä¸åº¦ã«ã¹ã«ã©ã«å²ãå½ã¦ããã¨ãåºæ¥ã¾ãã ãã®æ¹æ³ããå¼æ°ã4ã¤ã¾ã§ãªãåé¡ããã¾ãã:</p> +<pre class="perl prettyprint"> sub leftPad { my ($oldString, $width, $padChar) = @_; my $newString = ($padChar x ($width - length $oldString)) . $oldString; @@ -1282,12 +1294,14 @@ </pre> </li> <li> - <p>For subroutines with large numbers of arguments or where some arguments are optional or cannot be used in combination with others, best practice is to require the user to provide a hash of arguments when calling the subroutine, and then unpack <tt>@_</tt> back into that hash of arguments. For this approach, our subroutine call would look a little different:</p> -<pre class="perl"> + <p class=original>For subroutines with large numbers of arguments or where some arguments are optional or cannot be used in combination with others, best practice is to require the user to provide a hash of arguments when calling the subroutine, and then unpack <tt>@_</tt> back into that hash of arguments. For this approach, our subroutine call would look a little different:</p> + <p>å¼æ°ãå¤ããµãã«ã¼ãã³ããããã¤ãã®å¼æ°ããªãã·ã§ã³ã§ããã¨ããä»ã¨ã®çµã¿åããã§ä½¿ããªããªããæãè¯ãæ¹æ³ã¯ããµãã«ã¼ãã³ã®å¼ã³åºãæã«ãã¦ã¼ã¶ã«ããã·ã¥ã®å¼æ°ã渡ããããã¨ã§ããããã¦ã<tt>@_</tt>ãããã·ã¥ã«åãåºãã¾ãããã®ã¢ããã¼ãã®ããã«ããµãã«ã¼ãã³ã®å¼ã³åºãã¯ã¡ãã£ã¨éã£ããã®ã«ãªãã¾ã:</p> +<pre class="perl prettyprint"> print leftPad("oldString" => "pod", "width" => 10, "padChar" => "+"); </pre> - <p>And the subroutine itself looks like this:</p> -<pre class="perl"> + <p class=original>And the subroutine itself looks like this:</p> + <p>ããã¦ããµãã«ã¼ãã³èªèº«ã¯æ¬¡ã®ããã«ãªãã¾ã:</p> +<pre class="perl prettyprint"> sub leftPad { my %args = @_; my $newString = ($args{"padChar"} x ($args{"width"} - length $args{"oldString"})) . $args{"oldString"}; @@ -1296,9 +1310,13 @@ </pre> </li> </ol> -<h3>Returning values</h3> -<p>Like other Perl expressions, subroutine calls may display contextual behaviour. You can use the <tt>wantarray</tt> function (which should be called <tt>wantlist</tt> but never mind) to detect what context the subroutine is being evaluated in, and return a result appropriate to that context:</p> -<pre class="perl"> +<h3 class=original>Returning values</h3> +<h3>æ»ãå¤</h3> +<p class=original>Like other Perl expressions, subroutine calls may display contextual behaviour. You can use the <tt>wantarray</tt> function (which should be called <tt>wantlist</tt> but never mind) to detect what context the subroutine is being evaluated in, and return a result appropriate to that context:</p> + +<p>ä»ã®Perlã®å¼ã¨åæ§ããµãã«ã¼ãã³å¼ã³åºãã¯ãã³ã³ããã¹ãä¾åã®æ¯ãèãããã¾ãã<tt>wantlist</tt>(<tt>wantlist</tt>ã¨å¼ã°ããã¹ãã§ãããæ°ã«ããªãã§ãã ãã)ã使ã£ã¦ãã©ã®ã³ã³ããã¹ãã§ãµãã«ã¼ãã³ãè©ä¾¡ããã¦ãããããã§ãã¯ã§ããã³ã³ããã¹ãã«é©ããçµæãè¿ããã¨ãåºæ¥ã¾ã:</p> + +<pre class="perl prettyprint"> sub contextualSubroutine { # Caller wants a list. Return a list return ("Everest", "K2", "Etna") if wantarray; @@ -1314,9 +1332,12 @@ print $scalar; # "Everest ::: K2 ::: Etna" </pre> -<h2>A quick note on variables</h2> -<p>All the variables you have seen so far are <i>lexical</i> variables, which are declared using the <tt>my</tt> keyword and last until the end of the enclosing block or file.</p> -<p><i>Package</i> variables, which we are about to meet, are declared using the <tt>our</tt> keyword and are effectively global in scope.</p> +<h2 class=original>A quick note on variables</h2> +<h2>å¤æ°ã«ã¤ãã¦ã®ç°¡åãªã¡ã¢</h2> +<p class=original>All the variables you have seen so far are <i>lexical</i> variables, which are declared using the <tt>my</tt> keyword and last until the end of the enclosing block or file.</p> +<p>ä»ã¾ã§è¦ã¦ããå ¨ã¦ã®å¤æ°ã¯ã<i>ã¬ãã·ã«ã«</i>å¤æ°ã§ãããããã¯ã<tt>my</tt>ãã¼ã¯ã¼ãã使ã£ã¦å®£è¨ããã¦ããããããã¯ããã¡ã¤ã«ã®çµããã¾ã§æå¹ã§ãã</p> +<p class=original><i>Package</i> variables, which we are about to meet, are declared using the <tt>our</tt> keyword and are effectively global in scope.</p> +<p><i>ããã±ã¼ã¸</i>å¤æ°ã¯ãããã«åºã¦ãã¾ããã<tt>our</tt>ãã¼ã¯ã¼ãã使ã£ã¦å®£è¨ãããã¹ã³ã¼ãå ã®ã°ãã¼ãã«å¤æ°ã¨ãã¦æå¹ã«ãªãã¾ã</p> <!-- <h3>Built-in variables</h3> <p><strong>The complete list of built-in variables is <a href="http://perldoc.perl.org/perlvar.html">perlvar</a>.</strong></p> @@ -1328,15 +1349,19 @@ <li><tt>use English</tt>. This call creates human-readable aliases for all built-in variables. For example, <tt>$"</tt>, <tt>$,</tt> and <tt>$!</tt> can be used as <tt>$LIST_SEPARATOR</tt>, <tt>$OUTPUT_FIELD_SEPARATOR</tt> and <tt>$OS_ERROR</tt> respectively.</li> </ul> --> -<h2>Packages and modules</h2> +<h2 class=original>Packages and modules</h2> +<h2>ããã±ã¼ã¸ã¨ã¢ã¸ã¥ã¼ã«</h2> -<p>In Perl, packages and modules are different things.</p> +<p class=original>In Perl, packages and modules are different things.</p> +<p>Perlã§ã¯ãããã±ã¼ã¸ã¨ã¢ã¸ã¥ã¼ã«ã¯éããã®ã§ãã</p> -<h3>Packages</h3> +<h3 class=original>Packages</h3> +<h3>ããã±ã¼ã¸</h3> -<p>A <i>package</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 <tt>main</tt> package, but you can switch package using the <tt>package</tt> built-in function:</p> +<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 <tt>main</tt> package, but you can switch package using the <tt>package</tt> built-in function:</p> +<p><i>package</i>ã¯åå空éã§ããã®ä¸ã§ã宣è¨ãããµãã«ã¼ãã³ãããã±ã¼ã¸å¤æ°ã¯ãæé»çã«ãç¾å¨ã®ããã±ã¼ã¸å ã§å®£è¨ããã¾ããå®è¡ã®æåã¯ã<tt>main</tt>ããã±ã¼ã¸ã«ãªãã¾ãããçµè¾¼é¢æ°ã®<tt>package</tt>ã使ã£ã¦ãããã±ã¼ã¸ãåãæ¿ãããã¾ã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> sub subroutine { print "universe"; } @@ -1353,9 +1378,10 @@ our $variable = "mashed"; </pre> -<p>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 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> -<pre class="perl"> +<pre class="perl prettyprint"> subroutine(); # "kingedward" print $variable; # "mashed" @@ -1365,11 +1391,12 @@ print $Food::Potatoes::variable; # "mashed" </pre> -<h3>Modules</h3> - -<p>A <i>module</i> is a <tt>.pm</tt> 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 <tt>.pl</tt> Perl script. An example module might be located at <tt>C:\foo\bar\baz\Mathematics\Powers.pm</tt> or <tt>/foo/bar/baz/Mathematics/Powers.pm</tt>, and read as follows:</p> +<h3 class=original>Modules</h3> +<h3>ã¢ã¸ã¥ã¼ã«</h3> -<pre class="perl"> +<p class=original>A <i>module</i> is a <tt>.pm</tt> 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 <tt>.pl</tt> Perl script. An example module might be located at <tt>C:\foo\bar\baz\Mathematics\Powers.pm</tt> or <tt>/foo/bar/baz/Mathematics/Powers.pm</tt>, and read as follows:</p> +<p>A <i>ã¢ã¸ã¥ã¼ã«</i>ã¯ãä»ã®Perlãã¡ã¤ã«(ã¹ã¯ãªãããã¢ã¸ã¥ã¼ã«)ã«å«ãããã¨ãåºæ¥ã<tt>.pm</tt>ãã¡ã¤ã«ã§ããã¢ã¸ã¥ã¼ã«ã¯ <tt>.pl</tt> Perlã¹ã¯ãªããã¨ã¾ã£ããåãã·ã³ã¿ãã¯ã®ã¹ããã¹ããã¡ã¤ã«ã§ããä¾ã®ã¢ã¸ã¥ã¼ã«ã¯ã<tt>C:\foo\bar\baz\Mathematics\Powers.pm</tt> ã <tt>/foo/bar/baz/Mathematics/Powers.pm</tt>ã«ããã¾ããç¶ããèªãã§ãã ãã:</p> +<pre class="perl prettyprint"> use strict; use warnings; @@ -1384,9 +1411,12 @@ 1; </pre> -<p>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. <tt>return 1</tt> would suffice. If you don't use <tt>return</tt>, the value returned is <em>the value returned when the most recent statement was evaluated</em>. So, you will often see <tt>1</tt> at the bottom of a Perl module, as shown above.</p> +<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. <tt>return 1</tt> would suffice. If you don't use <tt>return</tt>, the value returned is <em>the value returned when the most recent statement was evaluated</em>. So, you will often see <tt>1</tt> at the bottom of a Perl module, as shown above.</p> +<p>ã¢ã¸ã¥ã¼ã«ã¯ãã¼ããããã¨ãæåããæå¾ã¾ã§å®è¡ãããã®ã§ããã¼ããæåãããã¨ã示ãããã«ãæå¾ã«çã®å¤ãè¿ãå¿ è¦ãããã¾ãã<tt>return 1</tt>ã§ååã§ãã<tt>return</tt>ã使ããªããã°ãè¿ãããå¤ã¯ã<em>ç´è¿ã®ã¹ãã¼ãã¡ã³ããè©ä¾¡ãããã¨ãã®æ»ãå¤</em>ã«ãªãã¾ãããã®ããã«ãä¸ã§è¦ãããã«ãPerlã¢ã¸ã¥ã¼ã«ã®æå¾ã«<tt>1</tt>ãè¯ãè¦ããããã¨ã«ãªãã¾ãã</p> + +<p class=original>So that the Perl interpreter can find them, directories containing Perl modules should be listed in your environment variable <tt>PERL5LIB</tt> beforehand. List the root directory containing the modules, don't list the module directories or the modules themselves:</p> +<p>Perlã¤ã³ã¿ã¼ããªã¿ã¯ããããè¦ã¤ãããã¨ãã§ããããã«ã¯ããã®åã«ãPerlã¢ã¸ã¥ã¼ã«ãå ¥ã£ã¦ãããã£ã¬ã¯ããªããç°å¢å¤æ°<tt>PERL5LIB</tt>ã«ãªã¹ãããã¦ããã¹ãã§ããã¢ã¸ã¥ã¼ã«ãå ¥ã£ã¦ããã«ã¼ããã£ã¬ã¯ããªããªã¹ããã¦ãã ãããã¢ã¸ã¥ã¼ã«ã®ãã£ã¬ã¯ããªãã¢ã¸ã¥ã¼ã«èªèº«ããªã¹ããã¦ã¯ããã¾ããã</p> -<p>So that the Perl interpreter can find them, directories containing Perl modules should be listed in your environment variable <tt>PERL5LIB</tt> beforehand. List the root directory containing the modules, don't list the module directories or the modules themselves:</p> <pre class="bash"> set PERL5LIB=C:\foo\bar\baz;%PERL5LIB% </pre> @@ -1396,8 +1426,9 @@ </pre> <p>Once the Perl module is created and <tt>perl</tt> knows where to look for it, you can use the <tt>require</tt> built-in function to search for and execute it during a Perl script. For example, calling <tt>require Mathematics::Powers</tt> causes the Perl interpreter to search each directory listed in <tt>PERL5LIB</tt> in turn, looking for a file called <tt>Mathematics/Powers.pm</tt>. After the module has been loaded, the subroutines and variables that were defined there suddenly become available in the main script. Our example script might be called <tt>powers.pl</tt> and read as follows:</p> +<p class=original>Perlã¢ã¸ã¥ã¼ã«ãä½ããã¦ã<tt>perl</tt>ããããã©ãã«ããããç¥ã£ã¦ããã°ãçµè¾¼ã®<tt>require</tt>é¢æ°ã使ã£ã¦æ¢ããPerlã®ã¹ã¯ãªããä¸ã§å®è¡ãããã¨ãã§ãã¾ããä¾ãã°ã<tt>require Mathematics::Powers</tt>ãå¼ã¶ã¨ãPerlã¤ã³ã¿ã¼ããªã¿ã¯<tt>PERL5LIB</tt>ã«ãªã¹ãããã¦ãããã£ã¬ã¯ããªãé çªã«ã<tt>Mathematics/Powers.pm</tt>ã¨ãããã¡ã¤ã«ãæ¢ãã¾ããã¢ã¸ã¥ã¼ã«ããã¼ããããããçªç¶ã«ãµãã«ã¼ãã³ã¨å¤æ°ãã¡ã¤ã³ã¹ã¯ãªããã«å®ç¾©ããã¾ãããã®ä¾ã®ã¹ã¯ãªããã<tt>powers.pl</tt>ã¨å¼ã³ã¾ããããç¶ãã¦èªãã§ããã:</p> -<pre class="perl"> +<pre class="perl prettyprint"> use strict; use warnings; @@ -1406,8 +1437,11 @@ print Mathematics::Powers::exp(2); # "7.3890461584" </pre> -<p>Now read this next bit carefully.</p> -<p>Packages and modules are two completely separate and distinct features of the Perl programming language. The fact that they both use the same double colon delimiter is a monumental red herring. It is possible to switch packages multiple times over the course of a script or module, and it is possible to use the same package declaration in multiple locations in multiple files. Calling <tt>require Foo::Bar</tt> <em>does not</em> look for and load a file with a <tt>package Foo::Bar</tt> declaration somewhere inside it. Calling <tt>require Foo::Bar</tt> <em>does not</em> necessarily load subroutines or package variables in the <tt>Foo::Bar</tt> namespace. Calling <tt>require Foo::Bar</tt> merely loads a file called <tt>Foo/Bar.pm</tt>, which need not have <em>any</em> kind of package declaration inside it at all, and in fact might declare <tt>package Baz::Qux</tt> and other nonsense inside it for all you know.</p> +<p class=original>Now read this next bit carefully.</p> +<p>ã§ã¯ã次ã¯ã¡ãã£ã¨æ³¨æãã¦èªãã§ãã ããã</p> +<p class=original>Packages and modules are two completely separate and distinct features of the Perl programming language. The fact that they both use the same double colon delimiter is a monumental red herring. It is possible to switch packages multiple times over the course of a script or module, and it is possible to use the same package declaration in multiple locations in multiple files. Calling <tt>require Foo::Bar</tt> <em>does not</em> look for and load a file with a <tt>package Foo::Bar</tt> declaration somewhere inside it. Calling <tt>require Foo::Bar</tt> <em>does not</em> necessarily load subroutines or package variables in the <tt>Foo::Bar</tt> namespace. Calling <tt>require Foo::Bar</tt> merely loads a file called <tt>Foo/Bar.pm</tt>, which need not have <em>any</em> kind of package declaration inside it at all, and in fact might declare <tt>package Baz::Qux</tt> and other nonsense inside it for all you know.</p> +<p>ããã±ã¼ã¸ã¨ã¢ã¸ã¥ã¼ã«ã®2ã¤ã¯ãããã°ã©ãã³ã°è¨èªPerlã«ããã¦å®å ¨ã«åããã¦ãããåºå¥ãããæ©è½ã§ãããã®2ã¤ãåãããã«ã³ãã³ã®ããªãã¿ã使ã£ã¦ãããã¨ã¯ãè¨å¿µç¢çãªèµ¤ãã·ã³ã§ããããã¯ãã¹ã¯ãªããã¾ãã¯ã¢ã¸ã¥ã¼ã«ã®ã³ã¼ã¹çµç±ã§ããã±ã¼ã¸ãè¤æ°ååãæ¿ãããã¨ãã§ããã¾ããè¤æ°ã®ãã¡ã¤ã«å ã®è¤æ°ã®å ´æã§åä¸ã®ããã±ã¼ã¸å®£è¨ã使ããã¨ãã§ãã¾ãã<tt>require Foo::Bar</tt>ãå¼ã¶ãã¨ã¯<tt>Foo::Bar</tt>åå空éã«ãããµãã«ã¼ãã³ãããã±ã¼ã¸å¤æ°ãå¿ ããããã¼ã<em>ãã¾ãã</em>ã<tt>require Foo::Bar</tt>ãå¼ã¶ãã¨ã¯ãåã«<tt>Foo/Bar.pm</tt>ã¨ãããã¡ã¤ã«ããã¼ãããã ãã§ããããã®ãã¡ã¤ã«ã«ã<em>ã©ã®ãããª</em>種é¡ã®ããã±ã¼ã¸å®£è¨ãå¿ è¦æãã¾ãããå®éã«ã¯ãããªãã®ç¥ã£ã¦ããå ¨ã¦ã®<tt>package Baz ::Qux</tt>ã®ãããªæå³ã®ãªããã®ã宣è¨ã§ãã¾ãã</p> + <p>Likewise, a subroutine call <tt>Baz::Qux::processThis()</tt> need not necessarily have been declared inside a file named <tt>Baz/Qux.pm</tt>. It could have been declared <em>literally anywhere</em>.</p> <p>Separating these two concepts is one of the stupidest features of Perl, and treating them as separate concepts invariably results in chaotic, maddening code. Fortunately for us, the majority of Perl programmers obey the following two laws:</p> <ol> @@ -1428,7 +1462,7 @@ <p>A quick example. An example module <tt>Animals/Animals.pm</tt> containing a class <tt>Animals::Animal</tt> reads like this:</p> -<pre class="perl"> +<pre class="perl prettyprint"> use strict; use warnings; @@ -1456,7 +1490,7 @@ <p>And a Perl script making use of this class might read:</p> -<pre class="perl"> +<pre class="perl prettyprint"> use strict; use warnings; @@ -1478,7 +1512,7 @@ <p>To create a class inheriting from a base class, populate the <tt>@ISA</tt> package variable. Let's suppose we subclassed <tt>Animals::Animal</tt> with <tt>Animals::Bear</tt>, located at <tt>Animals/Bear.pm</tt>:</p> -<pre class="perl"> +<pre class="perl prettyprint"> use strict; use warnings; @@ -1501,7 +1535,7 @@ <p>And some sample code:</p> -<pre class="perl"> +<pre class="perl prettyprint"> use strict; use warnings; @@ -1519,7 +1553,7 @@ <p>A <tt>BEGIN</tt> block is executed as soon as the compiler has finished parsing it, even before the compiler parses the rest of the file. It is ignored at execution time.</p> -<pre class="perl"> +<pre class="perl prettyprint"> use strict; use warnings; @@ -1534,7 +1568,7 @@ <p>A <tt>BEGIN</tt> block is always executed first. If you create multiple <tt>BEGIN</tt> blocks (don't), they are executed in order from top to bottom as the compiler encounters them. A <tt>BEGIN</tt> block always executes first even if it is placed halfway through a script (don't do this) or even at the end (or this).</p> <p>Because they are executed at compilation time, a <tt>BEGIN</tt> block placed inside a conditional block will <em>still</em> be executed first, even if the conditional evaluates to false and despite the fact that the conditional <em>has not been evaluated at all yet</em> and in fact <em>may never be evaluated</em>. <strong>Do not put <tt>BEGIN</tt> blocks in conditionals!</strong> If you want to do something conditionally at compile time, you need to put the conditional <em>inside</em> the <tt>BEGIN</tt> block:</p> -<pre class="perl"> +<pre class="perl prettyprint"> BEGIN { if($condition) { # etc. @@ -1545,13 +1579,13 @@ <h2><tt>use</tt></h2> <p>Okay. Now that you understand the obtuse behaviour and semantics of packages, modules, class methods and <tt>BEGIN</tt> blocks, I can explain the exceedingly commonly-seen <tt>use</tt> function.</p> <p>The following three statements:</p> -<pre class="perl"> +<pre class="perl prettyprint"> use Bugs::Caterpillar ("crawl", "pupate"); use Bugs::Caterpillar (); use Bugs::Caterpillar; </pre> <p>are respectively equivalent to:</p> -<pre class="perl"> +<pre class="perl prettyprint"> BEGIN { require Bugs::Caterpillar; Bugs::Caterpillar->import("crawl", "pupate"); @@ -1577,7 +1611,7 @@ <p>This concept is easiest to grasp using an example. Here's what <tt>Bugs/Caterpillar.pm</tt> looks like:</p> -<pre class="perl"> +<pre class="perl prettyprint"> use strict; use warnings; @@ -1599,7 +1633,7 @@ <p>And here's a script which makes use of the <tt>Bugs/Caterpillar.pm</tt> module:</p> -<pre class="perl"> +<pre class="perl prettyprint"> use strict; use warnings; @@ -1630,7 +1664,7 @@ <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> -<pre class="perl"> +<pre class="perl prettyprint"> my $f = "text.txt"; my $result = open INPUT, "<", $f; @@ -1640,23 +1674,23 @@ </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> -<pre class="perl"> +<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> -<pre class="perl"> +<pre class="perl prettyprint"> open INPUT, "<", $f || die "Couldn't open ", $f, " for reading"; </pre> <p>Which is the same as the nonsensical:</p> -<pre class="perl"> +<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> -<pre class="perl"> +<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> -<pre class="perl"> +<pre class="perl prettyprint"> while(1) { my $line = readline INPUT; last unless defined $line; @@ -1664,51 +1698,51 @@ } </pre> <p>To truncate that possible trailing line break, use <tt>chomp</tt>:</p> -<pre class="perl"> +<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> -<pre class="perl"> +<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> -<pre class="perl"> +<pre class="perl prettyprint"> while(my $line = <INPUT>) { # process $line... } </pre> <p>And even:</p> -<pre class="perl"> +<pre class="perl prettyprint"> while(<INPUT>) { # process $_... } </pre> <p>To read a single line of user input:</p> -<pre class="perl"> +<pre class="perl prettyprint"> my $line = <STDIN>; </pre> <p>To just wait for the user to hit Enter:</p> -<pre class="perl"> +<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> -<pre class="perl"> +<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> -<pre class="perl"> +<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> -<pre class="perl"> +<pre class="perl prettyprint"> my $fh; open $fh, "<", "text.txt" or die; while(<$fh>) { @@ -1721,18 +1755,18 @@ <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> -<pre class="perl"> +<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> -<pre class="perl"> +<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> -<pre class="perl"> +<pre class="perl prettyprint"> use strict; use warnings; @@ -1745,13 +1779,13 @@ <ul> <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"> +<pre class="perl prettyprint"> use Account qw{create open close suspend delete}; </pre> </li> <li> <p><tt>qr//</tt> 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> -<pre class="perl"> +<pre class="perl prettyprint"> my @capitals = ("Baton Rouge", "Indianapolis", "Columbus", "Montgomery", "Helena", "Denver", "Boise"); my $regex = qr/^[B-H]/; print join ", ", grep /$regex/, @capitals; @@ -1759,7 +1793,7 @@ </li> <li> <p><tt>qx{ }</tt> can be used instead of `backticks` to invoke a program and capture its output:</p> -<pre class="perl"> +<pre class="perl prettyprint"> my $text = qx{perl anotherscript.pl foo bar baz}; </pre> </li> @@ -1774,7 +1808,7 @@ <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> -<pre class="perl"> +<pre class="perl prettyprint"> print foreach @array; foreach ( @array ) { @@ -1784,17 +1818,20 @@ <p>I dislike this formation because it can lead to problems when refactoring.</p> -<h3>File tests</h3> +<h3 class=original>File tests</h3> +<h3>ãã¡ã¤ã«ãã¹ã</h3> -<p>The function <tt>-e</tt> is a built-in function which tests whether the named file exists.</p> -<pre class="perl"> +<p class=original>The function <tt>-e</tt> is a built-in function which tests whether the named file exists.</p> +<p>é¢æ°<tt>-e</tt> ã¯çµè¾¼ã®é¢æ°ã§ä¸ããããååã®ãã¡ã¤ã«ãåå¨ãããã©ããããã¹ããã¾ãã</p> +<pre class="perl prettyprint"> print "what" unless -e "/usr/bin/perl"; </pre> -<p>The function <tt>-d</tt> is a built-in function which tests whether the named file is a directory.</p> -<p>The function <tt>-f</tt> is a built-in function which tests whether the named file is a plain file.</p> -<p>These are just three of a large class of functions of the form <tt>-X</tt> where <tt>X</tt> 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 class=original>The function <tt>-d</tt> is a built-in function which tests whether the named file is a directory.</p> +<p>é¢æ°<tt>-d</tt> ã¯çµè¾¼ã®é¢æ°ã§ä¸ããããååã®ãã¡ã¤ã«ããã£ã¬ã¯ããªãã©ããããã¹ããã¾ãã</p> +<p class=original>The function <tt>-f</tt> is a built-in function which tests whether the named file is a plain file.</p> +<p>é¢æ°<tt>-f</tt> ã¯çµè¾¼ã®é¢æ°ã§ä¸ããããååã®ãã¡ã¤ã«ãæ®éã®ãã¡ã¤ã«ãã©ããããã¹ããã¾ãã</p> +<p class=original>These are just three of a large class of functions of the form <tt>-X</tt> where <tt>X</tt> 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><tt>-X</tt>å½¢å¼ã®å¤§ããªã¯ã©ã¹ã®é¢æ°ã®3ã¤ã®é¢æ°ã§ãã<tt>X</tt>ã¯å°æåã大æåã§ãããããã®é¢æ°ã¯<i>ãã¡ã¤ã«ãã¹ã</i>ã¨å¼ã°ãã¾ãããã¤ãã¹è¨å·ãå ã«æ¥ããã¨ã«æ°ãã¤ãã¦ãã ãããGoogleã¯ã¨ãªã§ã¯ããã¤ãã¹è¨å·ã¯ããã®è¨èãçµæã«å«ããªããã¨ãæ示ãã¾ãããã®ããããã¡ã¤ã«ãã¹ããGoogleã§æ¤ç´¢ãã¥ãããã¦ãã¾ã! 代ããã«ã"perl file test"ã§æ¤ç´¢ãã¦ä¸ããã</p> <script type="text/javascript"><!-- var sc_project=667681;