ktats****@users*****
ktats****@users*****
2014年 4月 17日 (木) 10:08:04 JST
Index: docs/articles/qntm.org/files/perl/perl.html diff -u docs/articles/qntm.org/files/perl/perl.html:1.17 docs/articles/qntm.org/files/perl/perl.html:1.18 --- docs/articles/qntm.org/files/perl/perl.html:1.17 Thu Apr 3 23:20:54 2014 +++ docs/articles/qntm.org/files/perl/perl.html Thu Apr 17 10:08:02 2014 @@ -1,29 +1,48 @@ -<!Doctype html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <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://qntm.org/files/perl/perl_jp.html --> + <link + rel="shortcut icon" + href="../../page/favicon.ico" + /> <title>2æéåã§å¦ã¶Perl</title> + <meta name="description" content="2æéåã§å¦ã¹ãPerlã®ãã¥ã¼ããªã¢ã«"> + <!-- linkto: http://qntm.org/files/perl/perl_jp.html --> + <style type="text/css"> + @font-face { + font-family: 'Ubuntu Mono'; + font-style: normal; + font-weight: 700; + src: local('Ubuntu Mono Bold'), local('UbuntuMono-Bold'), url('http://themes.googleusercontent.com/static/fonts/ubuntumono/v3/ceqTZGKHipo8pJj4molytjqR_3kx9_hJXbbyU8S6IN0.woff') format('woff'); + } body { padding: 2em; font-family: Tahoma; } pre { margin-left: 2em; + padding-left: 1em; } - pre, code { + code { color: #348275; + font-family: "Ubuntu Mono", monospace; + } + .original { + display: none; } </style> </head> - <body> + <!-- http://fonts.googleapis.com/css?family=Ubuntu+Mono:400,700|Roboto+Slab:400,700 --> + + <script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js"></script> + <body onload="prettyPrint();return true;"> <h1 class="original">Learn Perl in about 2 hours 30 minutes</h1> <h1>2æéåã§å¦ã¶Perl</h1> -<h2><a href="http://qntm.org/perl">By Sam Hughes</a>, translated by Kato Atsusi</h2> +<h2><a href="http://qntm.org/perl">By Sam Hughes</a>, translated by <a href="http://d.hatena.ne.jp/ktat/">Kato Atsusi</a></h2> <p class="original">Perl is a dynamic, dynamically-typed, high-level, scripting (interpreted) language most comparable with PHP and Python. Perl's syntax owes a lot to ancient shell scripting tools, and it is famed for its overuse of confusing symbols, the majority of which are impossible to Google for. Perl's shell scripting heritage makes it great for writing <i>glue code</i>: scripts which link together other scripts and programs. Perl is ideally suited for processing text data and producing more text data. Perl is widespread, popular, highly portable and well-supported. Perl was designed with the philosophy "There's More Than One Way To Do It" (TMTOWTDI) (contrast with Python, where "there should be one - and preferably only one - obvious way to do it").</p> @@ -55,7 +74,7 @@ <h2 class="original">Preliminary notes</h2> <h2>åæ¸ã</h2> -<ul class="original"> +<ul> <li><p class="original">The following can be said of almost every declarative statement in this document: "that's not, strictly speaking, true; the situation is actually a lot more complicated". If you see a serious lie, point it out, but I reserve the right to preserve certain critical lies-to-children. </p> <p>以ä¸ã®ãã¨ãããã®ããã¥ã¡ã³ãå ã®ã»ã¨ãã©ãã¹ã¦ã®å®£è¨æã§è¨ããã¨ãåºæ¥ã¾ã: "ããã¯ãå³æ ¼ãªæå³ã§ã®ãçå®ã§ã¯ããã¾ãã; ç¶æ³ã¯å®éã«ã¯ãã£ã¨è¤éã§ã"ãããæ·±å»ãªåãè¦ã¤ããããææãã¦ãã ãããã§ãããç§ã«ã¯åã©ãã«ã¤ãéè¦ãªåããã®ã¾ã¾ã«ãã¦ãã権å©ãããã¾ãã</p></li> @@ -72,7 +91,7 @@ <p>Perl<i>ã¹ã¯ãªãã</i>ã¯<code>.pl</code>ã¨ããæ¡å¼µåã®ããã¹ããã¡ã¤ã«ã§ãã</p> <p class="original">Here's the full text of <code>helloworld.pl</code>:</p> <p><code>helloworld.pl</code>ã¯ä»¥ä¸ã®ããã«ãªãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> use strict; use warnings; @@ -117,7 +136,7 @@ <li>ä»ã®å¤æ°ã¸ã®ãªãã¡ã¬ã³ã¹</li> </ul> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my $undef = undef; print $undef; # prints the empty string "" and raises a warning @@ -126,12 +145,12 @@ print $undef2; # prints "" and raises exactly the same warning </pre> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my $num = 4040.5; print $num; # "4040.5" </pre> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my $string = "world"; print $string; # "world" </pre> @@ -141,7 +160,7 @@ <p class="original">String concatenation using the <code>.</code> operator (same as PHP):</p> <p>æååã®é£çµã«ã¯<code>.</code>æ¼ç®åã使ãã¾ã(PHPã¨åã):</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> print "Hello ".$string; # "Hello world" </pre> @@ -167,7 +186,7 @@ <p class="original"><strong>It is impossible to determine whether a scalar contains a "number" or a "string".</strong> More precisely, it should never be necessary to do this. Whether a scalar behaves like a number or a string depends on the operator with which it is used. When used as a string, a scalar will behave like a string. When used as a number, a scalar will behave like a number (raising a warning if this isn't possible):</p> <p><strong>ã¹ã«ã©ã¼ã«"æ°å"ã"å¤æ°"ã®ãããããå ¥ã£ã¦ããã®ããå¤æãããã¨ã¯ã§ãã¾ããã</strong> ããæ£ç¢ºã«ã¯ããããªãã¨ã¯è¦å½éãã§ããPerlã¯ãã®ç¹ã§å¼±ãåä»ãã§ãã ã¹ã«ã©ãæ°åãæåã®ã©ã¡ããã®ããã«æ¯èããã¯ã使ãããæ¼ç®åã«ããã¾ããæååã¨ãã¦ä½¿ãã°ãã¹ã«ã©ã¯æååã®ããã«ãµãã¾ãã¾ããæ°åã¨ãã¦ä½¿ãã°ãã¹ã«ã©ã¯æ°åã®ããã«ãµãã¾ãã¾ã(ã¾ãããããããã¨ãåºæ¥ãªããã°ãè¦åãçºãã¾ã):</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my $str1 = "4G"; my $str2 = "4H"; @@ -187,7 +206,7 @@ # String operators: <a href="http://perldoc.perl.org/perlop.html#Equality-Operators">lt, gt, le, ge, eq, ne, cmp</a>, <a href="http://perldoc.perl.org/perlop.html#Multiplicative-Operators">.</a>, <a href="http://perldoc.perl.org/perlop.html#Multiplicative-Operators">x</a> </pre> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> # æ°åç¨ã®æ¼ç®å: <, >, <=, >=, ==, !=, <=> # æåç¨ã®æ¼ç®å: <a href="http://perldoc.jp/docs/perl/perlop.pod#Equality32Operators">lt, gt, le, ge, eq, ne, cmp</a>, <a href="http://perldoc.jp/docs/perl/perlop.pod#Multiplicative32Operators">.</a>, <a href="http://perldoc.jp/docs/perl/perlop.pod#Multiplicative32Operators">x</a> </pre> @@ -209,7 +228,7 @@ ); </pre> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my @array = ( "print", "these", @@ -224,7 +243,7 @@ <p>é åããå¤ã«ã¢ã¯ã»ã¹ããã¨ãã«ã¯ãã«è¨å·ã使ããªããã°ããã¾ããã<em>åããã</em>å¤ã¯é åã§ã¯ãªããã¹ã«ã©ã ããã§ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> print $array[0]; # "print" print $array[1]; # "these" print $array[2]; # "strings" @@ -237,7 +256,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 prettyprint"> +<pre class="prittyprint lang-perl"> print $array[-1]; # "me" print $array[-2]; # "for" print $array[-3]; # "out" @@ -254,7 +273,7 @@ <p class="original">To get an array's length:</p> <p>é åã®é·ããå¾ãã«ã¯:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> 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> @@ -264,7 +283,7 @@ <p class="original">Variables can be interpolated into strings:</p> <p>å¤æ°ãæååã®éã«å ¥ãããã¨ãã§ãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> print "Hello $string"; # "Hello world" print "@array"; # "print these strings out for me" </pre> @@ -272,7 +291,7 @@ <p class="original"><strong>Caution.</strong> One day you will put somebody's email address inside a string, <code>"jeff****@gmail*****"</code>. This will cause Perl to look for an array variable called <code>@gmail</code> 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> ããæ¥ã誰ãã®ã¡ã¼ã«ã¢ãã¬ã¹ã<code>"jeff****@gmail*****"</code>ãæååã«å ¥ããã¨ãã¾ãã ããã¯ãPerlã«<code>@gmail</code>ã¨ããé åå¤æ°ãæ¢ãããæååã®éã«å ¥ãããã¨ãã¾ãããããè¦ã¤ãããªããã°ãã¨ã©ã¼ã«ãªãã¾ããå¤æ°ã®å±éãé²ãã«ã¯2ã¤ã®æ¹æ³ãããã¾ã:ã·ã¸ã«ãã¨ã¹ã±ã¼ããããã¾ã¦ãããããã«ã¯ã©ã¼ãã®ä»£ããã«ã·ã³ã°ã«ã¯ã©ã¼ãã使ãã</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> print "Hello \$string"; # "Hello $string" print 'Hello $string'; # "Hello $string" print "\@array"; # "@array" @@ -285,7 +304,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 prettyprint"> +<pre class="prittyprint lang-perl"> my %scientists = ( "Newton" => "Isaac", "Einstein" => "Albert", @@ -306,7 +325,7 @@ print $scientists{"Dyson"}; # returns undef, prints "" and raises a warning </pre> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> print $scientists{"Newton"}; # "Isaac" print $scientists{"Einstein"}; # "Albert" print $scientists{"Darwin"}; # "Charles" @@ -320,7 +339,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 prettyprint"> +<pre class="prittyprint lang-perl"> my @scientists = %scientists; </pre> @@ -328,14 +347,14 @@ <p>ã§ãããé åã¨ã¯éããããã·ã¥ã®ãã¼ã¯<em>é çªãããã¾ãã</em>ãããå¹ççãªé çªã§è¿ã£ã¦ãã¾ãããã®ãããæ´åããç´ããã<em>é çª</em>ã«æ°ãã¤ãã¦ãã ãããããããçµæã®é åã®<em>ãã¢</em>ã¯ä¿æããã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> print "@scientists"; # something like "Einstein Albert Darwin Charles Newton Isaac" </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 prettyprint"> +<pre class="prittyprint lang-perl"> my $data = "orange"; my @data = ("purple"); my %data = ( "0" => "blue"); @@ -353,7 +372,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 prettyprint"> +<pre class="prittyprint lang-perl"> ( "print", "these", @@ -375,7 +394,7 @@ <p class="original">Okay. Remember that <code>=></code> is just <code>,</code> in disguise and then look at this example:</p> <p>ããã§ãããã <code>=></code>ã¯ããã ã®<code>,</code>ã§ãããã¨ãæãåºããè¿éããã¦ã次ã®ä¾ãè¦ã¦ãã ãã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> ("one", 1, "three", 3, "five", 5) ("one" => 1, "three" => 3, "five" => 5) </pre> @@ -383,7 +402,7 @@ <p class="original">The use of <code>=></code> 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><code>=></code>ã®ä½¿ãæ¹ãä¸æ¹ã®ãªã¹ããé åã®å®£è¨ã§ãããã¨ã示ããä»æ¹ã¯ããã·ã¥ã®å®£è¨ã§ãããã¨ã示ãã¦ãã¾ããã§ããã2ã¤ã¨ããããèªèº«ãä½ã®å®£è¨ã§ãããã¾ããããã ã®ãªã¹ãã§ãã<em>åä¸ã®</em>ãªã¹ãã§ããã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> () </pre> @@ -391,7 +410,7 @@ <p>ãªãã®ãã³ããåå¨ãã¾ããããã®ãªã¹ãã¯ç©ºã®é åã¨ãã¦å®£è¨ããã¦ããã®ã§ãããããããã¨ããã«ã©ã®ããã·ã¥ã¨ãã¦ã§ããããã<code>perl</code>ã¤ã³ã¿ã¼ããªã¿ã«ã¯ãæããã«ã©ã¡ãã¨ãå¤æãããã¨ãã§ãã¾ãããPerlã®å¤ãã£ãä¸é¢ã§ããã¨ç解ãããªãã次ã®äºå®ãçã§ãããã¨ãã¾ãç解ããã§ããã: <strong>ãªã¹ãã®å¤ã¯ãã¹ãã§ãã¾ããã</strong> 試ãã¦ã¿ã¦ãã ãã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my @array = ( "apples", "bananas", @@ -409,7 +428,7 @@ <p>Perlã«ã¯ã<code>("inner"(訳註:å å´ã®), "list"(訳註:ãªã¹ãã), "several"(訳註:è¤æ°ã®), "entries"(訳註:å¤))</code>ãå å ãããé åãªã®ããããã·ã¥ãªã®ãã©ããããããã¾ããããã®ãããPerlã¯ãããã§ããªãã¨æ³å®ãã<strong>ã²ã¨ã¤ã®é·ããªã¹ãã«å¹³æ¿åãã¾ã</strong>:</p> -<pre> +<pre class="perl original"> print $array[0]; # "apples" print $array[1]; # "bananas" print $array[2]; # "inner" @@ -439,7 +458,7 @@ print $hash{"eat"}; # undef, so prints "" and raises a warning </pre> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my %hash = ( "beer" => "good", "bananas" => ( @@ -459,7 +478,7 @@ <p class="original">Of course, this does make it easy to concatenate multiple arrays together:</p> <p>ãã¡ããããã®ãã¨ã¯è¤æ°ã®é åãä¸ç·ã«ãããããã¦ãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my @bones = ("humerus", ("jaw", "skull"), "tibia"); my @fingers = ("thumb", "index", "middle", "ring", "little"); my @parts = (@bones, @fingers, ("foot", "toes"), "eyeball", "knuckle"); @@ -480,13 +499,13 @@ <p><code>$scalar =</code>ã®ãããªã¹ã«ã©ã®å²ãå½ã¦ã¯ã¹ã«ã©ã³ã³ããã¹ãã¨ãã¦è©ä¾¡ããã¾ãããã®ã±ã¼ã¹ã§ã¯ããã®å¼ã¯<code>"Mendeleev"</code>ã¨ãè¿ãããå¤ã¯<code>"Mendeleev"</code>ã¨åãã¹ã«ã©ã«ãªãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my $scalar = "Mendeleev"; </pre> <p class="original">An array or hash assignment such as <code>@array =</code> or <code>%hash =</code> 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><code>@array =</code> ã <code>%hash =</code> ã®ãããªé åãããã·ã¥ã®å²ãå½ã¦ã¯ããªã¹ãã³ã³ããã¹ãã§è©ä¾¡ããã¾ãããªã¹ãã®å¤ã¯ãªã¹ãã³ã³ããã¹ãã§è©ä¾¡ããããªã¹ããè¿ãã¾ããé åãããã·ã¥ã«ä»£å ¥ãããããªã¨ãã§ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my @array = ("Alpha", "Beta", "Gamma", "Pie"); my %hash = ("Alpha" => "Beta", "Gamma" => "Pie"); </pre> @@ -497,25 +516,25 @@ <p class="original">A scalar expression evaluated in list context turns into a single-element list:</p> <p>ã¹ã«ã©ã®å¼ã¯ãªã¹ãã³ã³ããã¹ãã§è©ä¾¡ãããã¨ãã²ã¨ã¤ã®å¤ã®ãªã¹ãã¨ãªãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my @array = "Mendeleev"; # same as 'my @array = ("Mendeleev");' </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 prettyprint"> +<pre class="prittyprint lang-perl"> my $scalar = ("Alpha", "Beta", "Gamma", "Pie"); # Value of $scalar is now "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 prettyprint"> +<pre class="prittyprint lang-perl"> my @array = ("Alpha", "Beta", "Gamma", "Pie"); my $scalar = @array; # Value of $scalar is now 4 </pre> <p class="original">The <code><a href="http://perldoc.perl.org/functions/print.html">print</a></code> built-in function evaluates all of its arguments in list context. In fact, <code>print</code> accepts an unlimited list of arguments and prints each one after the other, which means it can be used to print arrays directly:</p> <p><code><a href="http://perldoc.jp/func/print">print</a></code> çµè¾¼é¢æ°ã¯å ¨ã¦ã®å¼æ°ããªã¹ãã³ã³ããã¹ãã§è©ä¾¡ãã¾ãã<code>print</code>ã¯ç¡å¶éã®ãªã¹ãã®å¼æ°ãååããä¸ã¤ä¸ã¤åºåãã¾ããã¤ã¾ããé åãç´æ¥ä¸ãããã¨ãåºæ¥ã¾ãã</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my @array = ("Alpha", "Beta", "Goo"); my $scalar = "-X-"; print @array; # "AlphaBetaGoo"; @@ -533,7 +552,7 @@ <p class="original">In the same way that lists cannot contain lists as elements, <strong>arrays and hashes cannot contain other arrays and hashes as elements.</strong> They can only contain scalars. Watch what happens when we try:</p> <p>ãªã¹ããè¦ç´ ã¨ãã¦ãªã¹ããå«ããªãã®ã¨åæ§ã<strong>é åã¨ããã·ã¥ã¯ä»ã®é åãããã·ã¥ãè¦ç´ ã¨ãã¦æã¦ã¾ãã</strong>ã 両æ¹ã¨ãã¹ã«ã©ããæã¦ã¾ããã ä»ãã試ããã¨ãããè¦ã¦ãã ãã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my @outer = ("Sun", "Mercury", "Venus", undef, "Mars"); my @inner = ("Earth", "Moon"); @@ -550,14 +569,14 @@ <p class="original">A reference is created using a backslash.</p> <p>ãªãã¡ã¬ã³ã¹ã¯ããã¯ã¹ã©ãã·ã¥ã使ã£ã¦ä½ããã¾ãã</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> 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 prettyprint"> +<pre class="prittyprint lang-perl"> print $colour; # "Indigo" print $scalarRef; # e.g. "SCALAR(0x182c180)" print ${ $scalarRef }; # "Indigo" @@ -566,14 +585,14 @@ <p class="original">As long as the result is not ambiguous, you can omit the braces too:</p> <p>çµæãææ§ã§ãªãéãããã¬ã¼ã¹ãçç¥ãããã¨ãã§ãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> 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, <code>-></code>:</p> <p>ãªãã¡ã¬ã³ã¹ãé åãããã·ã¥å¤æ°ã®ãªãã¡ã¬ã³ã¹ã®å ´åããã¬ã¼ã¹ãããä¸è¬çãªã¢ãã¼æ¼ç®åã<code>-></code>ã使ã£ã¦ãã¼ã¿ãåãåºãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my @colours = ("Red", "Orange", "Yellow", "Green", "Blue"); my $arrayRef = \@colours; @@ -595,7 +614,7 @@ <p class="original">Here are four examples, but in practice the last one is the most useful.</p> <p>4ã¤ã®ä¾ãããã¾ããã§ãããå®è·µçã«ã¯ãæå¾ã®ãã®ããã£ã¨ãæç¨ã§ãã</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my %owner1 = ( "name" => "Santa Claus", "DOB" => "1882-12-25", @@ -624,7 +643,7 @@ <p class="original">That's obviously unnecessarily laborious, because you can shorten it to:</p> <p>ããã¯ãæããã«ä¸å¿ è¦ã§éª¨ã®æãã¾ãããªããªãã次ã®ããã«çç¥ã§ãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my %owner1 = ( "name" => "Santa Claus", "DOB" => "1882-12-25", @@ -647,7 +666,7 @@ <p class="original">It is also possible to declare <i>anonymous</i> arrays and hashes using different symbols. Use square brackets for an anonymous array and braces for an anonymous hash. The value returned in each case is a <em>reference</em> to the anonymous data structure in question. Watch carefully, this results in exactly the same <code>%account</code> as above:</p> <p>å¥ã®è¨å·ã使ã£ã¦<i>ç¡å</i>é åãããã·ã¥ã宣è¨ãããã¨ãåºæ¥ã¾ããåè§ããã©ã±ãããç¡åé åã«ããã¬ã¼ã¹ãç¡åããã·ã¥ã«ä½¿ãã¾ãããããããè¿ãããå¤ã¯ãç¡åã®ãã¼ã¿æ§é ã®<em>ãªãã¡ã¬ã³ã¹</em>ã«ãªãã¾ãã注æãã¦è¦ã¦ãã ããã次ã®çµæã¯ãä¸ã®<code>%account</code>ã¨å ¨ãåãã§ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> # Braces denote an anonymous hash my $owner1Ref = { "name" => "Santa Claus", @@ -672,7 +691,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 prettyprint"> +<pre class="prittyprint lang-perl"> my %account = ( "number" => "31415926", "opened" => "3000-01-01", @@ -696,7 +715,7 @@ <p>ãã¦ãããããã¾ããããã«<code>%account</code>ãã¾ã ããã¨ãã¾ããããã§ãããå ¨ã¦ã®ä»ã®ãã®(ä»ã®ãã®ããã£ããªã)ã¯ãã¹ã³ã¼ãã®å¤ã«è½ã¡ã¾ããããããã®ã±ã¼ã¹ã§åãæé ãéåãã«ãããã¨ã§ãæ å ±ã表示ã§ãã¾ããããä¸åº¦ã4ã¤ã®ä¾ãããã¾ãããæå¾ã®ãã®ãä¸çªæç¨ã§ãã</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my $ownersRef = $account{"owners"}; my @owners = @{ $ownersRef }; my $owner1Ref = $owners[0]; @@ -713,7 +732,7 @@ <p class="original">Or, for short:</p> <p>ã¾ãã¯, çç¥ãã¦:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my @owners = @{ $account{"owners"} }; my %owner1 = %{ $owners[0] }; my %owner2 = %{ $owners[1] }; @@ -727,7 +746,7 @@ <p class="original">Or using references and the <code>-></code> operator:</p> <p>ã¾ãã¯ããªãã¡ã¬ã³ã¹ã¨<code>-></code> ã使ã£ã¦:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my $ownersRef = $account{"owners"}; my $owner1Ref = $ownersRef->[0]; my $owner2Ref = $ownersRef->[1]; @@ -741,7 +760,7 @@ <p class="original">And if we completely skip all the intermediate values:</p> <p>ããã¦ãå ¨ã¦ã®ä¸éã®å¤ãã¹ããããããªã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> print "Account #", $account{"number"}, "\n"; print "Opened on ", $account{"opened"}, "\n"; print "Joint owners:\n"; @@ -754,21 +773,21 @@ <p class="original">This array has five elements:</p> <p>次ã®é åã«ã¯5ã¤ã®è¦ç´ ãããã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my @array1 = (1, 2, 3, 4, 5); print @array1; # "12345" </pre> <p class="original">This array, however, has ONE element (which happens to be a reference to an anonymous, five-element array):</p> <p>ãããã次ã®é åã«ã¯*ã²ã¨ã¤*ã®è¦ç´ (ç¡åã®5ã¤ã®è¦ç´ ã®é åã®ãªãã¡ã¬ã³ã¹)ãããã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> 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 prettyprint"> +<pre class="prittyprint lang-perl"> my $array3Ref = [1, 2, 3, 4, 5]; print $array3Ref; # e.g. "ARRAY(0x22710c0)" print @{ $array3Ref }; # "12345" @@ -783,7 +802,7 @@ <p class="original">No surprises here, other than the spelling of <code>elsif</code>:</p> <p><code>elsif</code>ã®ã¹ãã«ä»¥å¤ã«ã¯é©ããã®ã¯ããã¾ãã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my $word = "antidisestablishmentarianism"; my $strlen = <a href="http://perldoc.jp/func/length">length</a> $word; @@ -799,13 +818,13 @@ <p class="original">Perl provides a shorter "<i>statement</i> <code>if</code> <i>condition</i>" syntax which is highly recommended for <strong>short</strong> statements:</p> <p>Perlã«ã¯ããçã "<i>ã¹ãã¼ãã¡ã³ã</i> <code>if</code> <i>æ¡ä»¶</i>"ã®ã·ã³ã¿ãã¯ã¹ãããã¾ãã<strong>çã</strong>ã¹ãã¼ãã¡ã³ãç¨ã«ãå¼·ãæ¨å¥¨ããã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> print "'", $word, "' is actually enormous" if $strlen >= 20; </pre> <h3><code>unless</code> ... <code>else</code> ...</h3> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my $temperature = 20; unless($temperature > 30) { @@ -821,7 +840,7 @@ <p class="original">This, by comparison, is highly recommended because it is so easy to read:</p> <p>ä¸æ¹ã§ã以ä¸ã¯èªã¿ãããã®ããã«ãå¼·ãæ¨å¥¨ããã¾ã</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> print "Oh no it's too cold" unless $temperature > 15; </pre> @@ -831,7 +850,7 @@ <p class="original">The ternary operator <code>?:</code> allows simple <code>if</code> statements to be embedded in a statement. The canonical use for this is singular/plural forms:</p> <p>ä¸é æ¼ç®å <code>?:</code> ã¯ãåç´ãª <code>if</code> ã¹ãã¼ãã¡ã³ããã²ã¨ã¤ã®ã¹ãã¼ãã¡ã³ãã«åãè¾¼ãã¾ããä¸é æ¼ç®åã®æ¨æºçãªä½¿ãæ¹ã¨ãã¦ãåæ°/è¤æ°ã®å½¢ãããã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my $gain = 48; print "You gained ", $gain, " ", ($gain == 1 ? "experience point" : "experience points"), "!"; </pre> @@ -839,7 +858,7 @@ <p class="original">Aside: singulars and plurals are best spelled out in full in both cases. Don't do something clever like the following, because anybody searching the codebase to replace the words "tooth" or "teeth" will never find this line:</p> <p>ä½è«: 両æ¹ã®ã±ã¼ã¹ã®åæ°ã¨è¤æ°ãå®å ¨ã«æ¸ãåºããã¦ãã¾ãã決ãã¦ä»¥ä¸ã®ãããªå·§å¦ãªãã¨ãããªãã§ãã ãããã³ã¼ããæ¤ç´¢ãã¦ã"tooth"ã"teeth"ã®åèªãç½®ãæãããã¨ãã¦ãããã®è¡ããè¦ã¤ãããã¨ãã§ãã¾ããã</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my $lost = 1; print "You lost ", $lost, " t", ($lost == 1 ? "oo" : "ee"), "th!"; </pre> @@ -847,7 +866,7 @@ <p class="original">Ternary operators may be nested:</p> <p>ä¸é æ¼ç®åã¯ãã¹ãã§ãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my $eggs = 5; print "You have ", $eggs == 0 ? "no eggs" : $eggs == 1 ? "an egg" : @@ -866,7 +885,7 @@ <p class="original">Perl has a conventional <code>while</code> loop:</p> <p>Perlã®æ £ä¾çãª<code>while</code> ã«ã¼ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my $i = 0; while($i < scalar @array) { print $i, ": ", $array[$i]; @@ -876,7 +895,7 @@ <p class="original">Perl also offers the <code>until</code> keyword:</p> <p>Perlã«ã¯<code>until</code>ãã¼ã¯ã¼ããããã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my $i = 0; until($i >= scalar @array) { print $i, ": ", $array[$i]; @@ -887,7 +906,7 @@ <p class="original">These <code>do</code> loops are <em>almost</em> equivalent to the above (a warning would be raised if <code>@array</code> were empty):</p> <p>ãããã®<code>do</code>ã«ã¼ãã¯ã<em>ã»ã¨ãã©</em>ä¸ã¨åãã§ã(<code>@array</code>ã空ã®å ´åè¦åãèµ·ãã¾ã):</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my $i = 0; do { print $i, ": ", $array[$i]; @@ -898,7 +917,7 @@ <p class="original">and</p> <p>ããã¦</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my $i = 0; do { print $i, ": ", $array[$i]; @@ -909,7 +928,7 @@ <p class="original">Basic C-style <code>for</code> loops are available too. Notice how we put a <code>my</code> inside the <code>for</code> statement, declaring <code>$i</code> only for the scope of the loop:</p> <p>åºæ¬çãªC-styleã®<code>for</code>ã«ã¼ããå©ç¨ã§ãã¾ãã<code>my</code>ã<code>for</code>æã®å å´ã«ç½®ãæ¹æ³ã«æ³¨æãã¦ãã ããã宣è¨ããã<code>$i</code>ã¯ã«ã¼ãã®ã¹ã³ã¼ãã§ã®ã¿æå¹ã§ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> for(my $i = 0; $i < scalar @array; $i++) { print $i, ": ", $array[$i]; } @@ -918,7 +937,7 @@ <p class="original">This kind of <code>for</code> loop is considered old-fashioned and should be avoided where possible. Native iteration over a list is much nicer. Note: unlike PHP, the <code>for</code> and <code>foreach</code> keywords are synonyms. Just use whatever looks most readable:</p> <p>ãã®ç¨®ã®<code>for</code>ã«ã¼ãã¯å¤èãããæ¹ãªã®ã§ãå¯è½ã§ããã°é¿ããã¹ãã§ãããªã¹ãã®ãã¤ãã£ãã®ã¤ãã¬ã¼ã·ã§ã³ã¯ããç°¡åã§ãã注æ: PHPã¨éãã <code>for</code> 㨠<code>foreach</code> ãã¼ã¯ã¼ãã¯ã·ããã ã§ãããã£ã¨ãèªã¿ããããã®ã®ããããã使ã£ã¦ãã ãã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> foreach my $string ( @array ) { print $string; } @@ -927,7 +946,7 @@ <p class="original">If you do need the indices, the <a href="http://perldoc.perl.org/perlop.html#Range-Operators">range operator</a> <code>..</code> creates an anonymous list of integers:</p> <p>è¤æ°ã®ã¤ã³ããã¯ã¹ãå¿ è¦ãªãã<a href="http://perldoc.jp/docs/perl/perlop.pod#Range32Operators">ç¯å²æ¼ç®å</a><code>..</code>ã§æ´æ°ã®ç¡åãªã¹ããä½ãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> foreach my $i ( 0 .. $#array ) { print $i, ": ", $array[$i]; } @@ -936,7 +955,7 @@ <p class="original">You can't iterate over a hash. However, you can iterate over its keys. Use the <code>keys</code> built-in function to retrieve an array containing all the keys of a hash. Then use the <code>foreach</code> approach that we used for arrays:</p> <p>ããã·ã¥ã¯ã¤ãã¬ã¼ãã§ãã¾ããããã®ãã¼ãã¤ãã¬ã¼ãã§ãã¾ããçµè¾¼é¢æ°ã®<code>keys</code>ã使ã£ã¦ãããã·ã¥ã®å ¨ã¦ã®ãã¼ãå«ãé åãåãåºãã¦ãã ããããããããé åã§ä½¿ã£ã<code>foreach</code>ã®ã¢ããã¼ãã使ãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> foreach my $key (keys %scientists) { print $key, ": ", $scientists{$key}; } @@ -944,7 +963,7 @@ <p class="original">Since a hash has no underlying order, the keys may be returned in any order. Use the <code>sort</code> built-in function to sort the array of keys alphabetically beforehand:</p> <p>ããã·ã¥ã«ã¯é çªãããã¾ããã®ã§ãkeysã¯ã©ã®ãããªé çªã§ãæ»ãã¾ããçµè¾¼ã®<code>sort</code>é¢æ°ã使ã£ã¦ãã¢ã«ãã¡ãããé ã§ãã¼ã®é åãã½ã¼ãã§ãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> foreach my $key (sort keys %scientists) { print $key, ": ", $scientists{$key}; } @@ -953,7 +972,7 @@ <p class="original">If you don't provide an explicit iterator, Perl uses a default iterator, <code>$_</code>. <code>$_</code> is the first and friendliest of the <a href="http://perldoc.perl.org/perlvar.html">built-in variables</a>:</p> <p>æ示çãªã¤ãã¬ã¼ã¿ã使ããªããã°ãPerlã¯ããã©ã«ãã®ã¤ãã¬ã¼ã¿ã¨ãã¦<code>$_</code>ã使ãã¾ãã<code>$_</code>ã¯æåã®æããã¬ã³ããªã¼ãª<a href="http://perldoc.jp/docs/perl/perlvar.pod">çµè¾¼ã®å¤æ°</a>ã§ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> foreach ( @array ) { print $_; } @@ -961,7 +980,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 prettyprint"> +<pre class="prittyprint lang-perl"> print $_ foreach @array; </pre> @@ -972,7 +991,7 @@ <p><code>next</code> 㨠<code>last</code>ã¯ã«ã¼ãé²ã¿ãå¶å¾¡ããã®ã«ä½¿ããã¾ããå¤ãã®ããã°ã©ãã³ã°è¨èªã§ã¯ãããããã<code>continue</code> 㨠<code>break</code>ã¨ãªã£ã¦ãã¾ãããªãã·ã§ã³ã§ãã©ã®ã«ã¼ãã«ãã©ãã«ãã¤ãããã¨ãã§ãã¾ããæ £ä¾ã«ãããã©ãã«ã¯<code>å ¨ã¦å¤§æåã§</code>æ¸ããã¨ã«ãªã£ã¦ãã¾ããã«ã¼ãã«ã©ãã«ãã¤ãããã¨ã§ã<code>next</code> 㨠<code>last</code> ã«ã©ãã«ã対象ã«ã§ãã¾ãã100以ä¸ã®ç´ æ°ãè¦ã¤ããä¾ã§ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> CANDIDATE: for my $candidate ( 2 .. 100 ) { for my $divisor ( 2 .. <a href="http://perldoc.jp/func/sqrt">sqrt</a> $candidate ) { next CANDIDATE if $candidate % $divisor == 0; @@ -991,42 +1010,42 @@ <p><code>@stack</code>ã使ã£ã¦ãã¢ãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my @stack = ("Fred", "Eileen", "Denise", "Charlie"); print @stack; # "FredEileenDeniseCharlie" </pre> <p class="original"><code><a href="http://perldoc.perl.org/functions/pop.html">pop</a></code> extracts and returns the final element of the array. This can be thought of as the top of the stack:</p> <p><code><a href="http://perldoc.jp/func/pop">pop</a></code> ã¯é åã®æå¾ã®è¦ç´ ãå¼ãåºãã¦è¿ãã¾ããã¹ã¿ãã¯ã®ä¸ã¨ãã¦èãããã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> print pop @stack; # "Charlie" print @stack; # "FredEileenDenise" </pre> <p class="original"><code><a href="http://perldoc.perl.org/functions/push.html">push</a></code> appends extra elements to the end of the array:</p> <p><code><a href="http://perldocjp/func/push">push</a></code> ã¯è¿½å ã®è¦ç´ ãé åã®æå¾ã«ä»å ãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> push @stack, "Bob", "Alice"; print @stack; # "FredEileenDeniseBobAlice" </pre> <p class="original"><code><a href="http://perldoc.perl.org/functions/shift.html">shift</a></code> extracts and returns the first element of the array:</p> <p><code><a href="http://perldoc.perl.org/func/shift">shift</a></code> ã¯é åã®æåã®è¦ç´ ãå¼ãåºãã¦è¿ãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> print shift @stack; # "Fred" print @stack; # "EileenDeniseBobAlice" </pre> <p class="original"><code><a href="http://perldoc.perl.org/functions/unshift.html">unshift</a></code> inserts new elements at the beginning of the array:</p> <p><code><a href="http://perldoc.jp/func/unshift">unshift</a></code> é åã®æåã«æ°ããè¦ç´ ãæ¿å ¥ãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> unshift @stack, "Hank", "Grace"; print @stack; # "HankGraceEileenDeniseBobAlice" </pre> <p class="original"><code>pop</code>, <code>push</code>, <code>shift</code> and <code>unshift</code> are all special cases of <code><a href="http://perldoc.perl.org/functions/splice.html">splice</a></code>. <code>splice</code> removes and returns an array slice, replacing it with a different array slice:</p> <p><code>pop</code>ã<code>push</code>ã <code>shift</code>ã<code>unshift</code> ã¯ãå ¨ã¦ã<code><a href="http://perldoc.jp/func/splice">splice</a></code>ã®ç¹å¥ãªã±ã¼ã¹ã§ãã<code>splice</code> ã¯ãé åã®ã¹ã©ã¤ã¹ãåé¤ãã¦ãè¿ãã¾ããå¥ã®é åã¹ã©ã¤ã¹ã§ãããç½®ãæãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> print splice(@stack, 1, 4, "<<<", ">>>"); # "GraceEileenDeniseBob" print @stack; # "Hank<<<>>>Alice" </pre> @@ -1040,7 +1059,7 @@ <p class="original">The <code><a href="http://perldoc.perl.org/functions/join.html">join</a></code> function concatenates many strings into one:</p> <p><code><a href="http://perldoc.jp/func/join">join</a></code> é¢æ°ã¯å¤ãã®æååãä¸ã¤ã«çµåãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my @elements = ("Antimony", "Arsenic", "Aluminum", "Selenium"); print @elements; # "AntimonyArsenicAluminumSelenium" print "@elements"; # "Antimony Arsenic Aluminum Selenium" @@ -1049,7 +1068,7 @@ <p class="original">In list context, the <code><a href="http://perldoc.perl.org/functions/reverse.html">reverse</a></code> function returns a list in reverse order. In scalar context, <code>reverse</code> concatenates the whole list together and then reverses it as a single word.</p> <p>ãªã¹ãã³ã³ããã¹ãã§ã¯ã<code><a href="http://perldoc.jp/func/reverse">reverse</a></code>é¢æ°ã¯éé ã®ãªã¹ããè¿ãã¾ããã¹ã«ã©ã¼ã³ã³ããã¹ãã§ã¯<code>reverse</code>ãªã¹ãã®å ¨ã¦ãã¤ãªãã¦ä¸ã¤ã®æååã¨ãã¦ããããéé ã«ãã¾ãã</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> print reverse("Hello", "World"); # "WorldHello" print reverse("HelloWorld"); # "HelloWorld" print scalar reverse("HelloWorld"); # "dlroWolleH" @@ -1059,7 +1078,7 @@ <p class="original">The <code><a href="http://perldoc.perl.org/functions/map.html">map</a></code> function takes an array as input and applies an operation to every scalar <code>$_</code> 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><code><a href="http://perldoc.jp/func/map">map</a></code>é¢æ°ã¯å ¥åã¨ãã¦é åãã¨ããé åå ã®å ¨ã¦ã®ã¹ã«ã© <code>$_</code>ãæä½ãã¾ããçµæã¨ãã¦æ°ããé åãä½ãã¾ããæä½ã¯ã²ã¨ã¤ã®ãã¬ã¼ã¹ã§æ¸¡ãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my @capitals = ("Baton Rouge", "Indianapolis", "Columbus", "Montgomery", "Helena", "Denver", "Boise"); print join ", ", map { uc $_ } @capitals; @@ -1069,7 +1088,7 @@ <p class="original">The <code><a href="http://perldoc.perl.org/functions/grep.html">grep</a></code> function takes an array as input and returns a filtered array as output. The syntax is similar to <code>map</code>. This time, the second argument is evaluated for each scalar <code>$_</code> in the input array. If a boolean true value is returned, the scalar is put into the output array, otherwise not.</p> <p><code><a href="http://perldoc.jp/func/grep">grep</a></code>é¢æ°ã¯å ¥åã¨ãã¦é åãã¨ãããã£ã«ã¿ã¼ãããé åãåºåãã¾ããã·ã³ã¿ãã¯ã¹ã¯<code>map</code>ã¨ä¼¼ã¦ãã¾ããä»åº¦ã¯ã第äºå¼æ°ã¯å ¥åãããé åã®åã¹ã«ã©<code>$_</code>ãè©ä¾¡ããã¾ãããã¼ãªã¢ã³ã§çã®å¤ãæ»ãã°ãã¹ã«ã©ã¯é åã¨ãã¦åºåããã¾ãããããã§ãªããã°ãåºåããã¾ããã</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> print join ", ", grep { length $_ == 6 } @capitals; # "Helena, Denver" </pre> @@ -1077,7 +1096,7 @@ <p class="original">Obviously, the length of the resulting array is the <em>number of successful matches</em>, which means you can use <code>grep</code> to quickly check whether an array contains an element:</p> <p>å½ç¶ãçµæã®é åã®é·ãã¯ã<em>ãããã«æåããæ°</em>ã«ãªãã¾ãããã®ã¨ãã¨ã¯ã<code>grep</code>ãé åã«è¦ç´ ããããã©ãããç´ æ©ããã§ãã¯ããã®ã«ä½¿ãããã¨ãæå³ãã¾ãã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> print scalar grep { $_ eq "Columbus" } @capitals; # "1" </pre> @@ -1087,7 +1106,7 @@ <p class="original">By default, the <code><a href="http://perldoc.perl.org/functions/sort.html">sort</a></code> function returns the input array, sorted into lexical (alphabetical) order:</p> <p>ããã©ã«ãã§ã¯ã<code><a href="http://perldoc.jp/func/sort">sort</a></code>é¢æ°ã¯å ¥åãããé åãæåé (ã¢ã«ãã¡ãããé )ã«ä¸¦ã³ããã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my @elevations = (19, 1, 2, 100, 3, 98, 100, 1056); print join ", ", sort @elevations; @@ -1100,7 +1119,7 @@ <p class="original">The <code>cmp</code> operator does exactly this for strings:</p> <p><code>cmp</code> æ¼ç®åã¯æååã«å¯¾ãã¦ãã¾ãã«ããããã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> print join ", ", sort { $a cmp $b } @elevations; # "1, 100, 100, 1056, 19, 2, 3, 98" </pre> @@ -1108,7 +1127,7 @@ <p class="original">The "spaceship operator", <code><=></code>, does the same for numbers:</p> <p>"ã¹ãã¼ã¹ã·ããæ¼ç®å", <code><=></code>ã¯ãæ°åã«å¯¾ãã¦åããã¨ããã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> print join ", ", sort { $a <=> $b } @elevations; # "1, 2, 3, 19, 98, 100, 100, 1056" </pre> @@ -1116,7 +1135,7 @@ <p class="original"><code>$a</code> and <code>$b</code> 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><code>$a</code> 㨠<code>$b</code> ã¯å¸¸ã«ã¹ã«ã©ã¼ã§ãããæ¯è¼ãé£ããé常ã«è¤éãªãªãã¸ã§ã¯ãã®ãªãã¡ã¬ã³ã¹ããããã¾ããæ¯è¼ã«ããã¹ãã¼ã¹ãå¿ è¦ãªããå¥ã®ãµãã«ã¼ãã³ãä½ãã代ããã«ãã®ååã渡ãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> sub comparator { # lots of code... # return -1, 0 or 1 @@ -1175,7 +1194,7 @@ <p class="original">Once you're inside a subroutine, the arguments are available using the <a href="http://perldoc.perl.org/perlvar.html">built-in array variable</a> <code>@_</code>. Example:</p> <p>ãµãã«ã¼ãã³ã®ä¸ã«å ¥ã£ã¦ãã¾ãã¨ã<a href="http://perldoc.jp/docs/perl/perlvar.pod">çµè¾¼ã®é åå¤æ°</a><code>@_</code>ã使ãã¾ããä¾:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> sub hyphenate { # Extract the first argument from the array, ignore everything else @@ -1197,7 +1216,7 @@ <p>å ¨ã¦ã®ä»ã®ä¸»è¦ãªããã°ã©ãã³ã°è¨èªã¨ã¯éããPerlã¯ãªãã¡ã¬ã³ã¹ã¨å¼ã³ã¾ããããã¯ããµãã«ã¼ãã³ã®å å´ã§å©ç¨å¯è½ãªå¤æ°ãå¤ããªãªã¸ãã«ã®ã³ãã¼ã§ã¯ãªããã¨ãæå³ãã¾ãã</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my $x = 7; sub reassign { @@ -1212,13 +1231,13 @@ <p>次ã®ãããªãã®ã試ãã¨</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> reassign(8); </pre> <p>then an error occurs and execution halts, because the first line of <code>reassign()</code> is equivalent to</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> 8 = 7; </pre> @@ -1239,7 +1258,7 @@ <p>ä¾ã®ãµãã«ã¼ãã³<code>left_pad</code>ã¯ã以ä¸ã®ä¾ã¯ã渡ãããè©°ãè¾¼ã¿æåã使ã£ã¦ãå¿ è¦ãªé·ãã«ãªãã¾ã§æååã«ä»å ãã¾ãã(<code>x</code>é¢æ°ã¯è¡ã«åãæååã®è¤æ°ã®ã³ãã¼ãã¤ãªãã¾ã)ã(注æ: ç°¡æ½ãã®ããã«ããããã®ãµãã«ã¼ãã³ã¯å ¨ã¦åºæ¬çãªã¨ã©ã¼ãã§ãã¯ãè¡ã£ã¦ãã¾ãããä¾ãã°ãè©°ãè¾¼ã¿æåã1æåã®ã¿ã§ãããã¨ãä¿è¨¼ããã¨ããé·ããæ¢åã®æååã®é·ã以ä¸ã§ããããå¿ è¦ãªå¼æ°ãå ¨ã¦æ¸¡ããã¦ãããã©ããããªã©)ã</p> <p class="original"><code>left_pad</code> is typically invoked as follows:</p> <p><code>left_pad</code> ã¯å ¸åçã«ã次ã®ããã«å¼ã°ãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> print left_pad("hello", 10, "+"); # "+++++hello" </pre> @@ -1247,7 +1266,7 @@ <li> <p class="original">Unpacking <code>@_</code> entry by entry is effective but not terribly pretty:</p> <p>ã¨ã³ããªã«ãã£ã¦ã<code>@_</code>ã¨ã³ããªãåãåºãã®ã¯ãå¹ççã§ããããã¾ãããããã¾ãã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> sub left_pad { my $oldString = $_[0]; my $width = $_[1]; @@ -1260,7 +1279,7 @@ <li> <p class="original">Unpacking <code>@_</code> by removing data from it using <code>shift</code> is recommended for up to 4 arguments:</p> <p><code>@_</code>ãåãåºãã®ã«ã<code>shift</code>ã使ã£ã¦ã@_ãããã¼ã¿ãåé¤ããã®ã¯ãå¼æ°ã4ã¤ã¾ã§ãªãæ¨å¥¨ããã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> sub left_pad { my $oldString = shift @_; my $width = shift @_; @@ -1271,7 +1290,7 @@ </pre> <p class="original">If no array is provided to the <code>shift</code> function, then it operates on <code>@_</code> implicitly. This approach is seen very commonly:</p> <p><code>shift</code>ã«é åã渡ããªããã°ãæé»ã«ã<code>@_</code>ã«å¯¾ãã¦æä½ãã¾ãããã®ã¢ããã¼ãã¯ã¨ã¦ãããè¦ããã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> sub left_pad { my $oldString = shift; my $width = shift; @@ -1286,7 +1305,7 @@ <li> <p class="original">You can unpack <code>@_</code> all in one go using multiple simultaneous scalar assignment. Again, this is okay for up to 4 arguments:</p> <p><code>@_</code>ã®åãåºãããåæã«å ¨ã¦ä¸åº¦ã«ã¹ã«ã©ã«å²ãå½ã¦ããã¨ãåºæ¥ã¾ãã ãã®æ¹æ³ããå¼æ°ã4ã¤ã¾ã§ãªãåé¡ããã¾ãã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> sub left_pad { my ($oldString, $width, $padChar) = @_; my $newString = ($padChar x ($width - length $oldString)) . $oldString; @@ -1297,12 +1316,12 @@ <li> <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 <code>@_</code> back into that hash of arguments. For this approach, our subroutine call would look a little different:</p> <p>å¼æ°ãå¤ããµãã«ã¼ãã³ããããã¤ãã®å¼æ°ããªãã·ã§ã³ã§ããã¨ããä»ã¨ã®çµã¿åããã§ä½¿ããªããªããæãè¯ãæ¹æ³ã¯ããµãã«ã¼ãã³ã®å¼ã³åºãæã«ãã¦ã¼ã¶ã«ããã·ã¥ã®å¼æ°ã渡ããããã¨ã§ããããã¦ã<code>@_</code>ãããã·ã¥ã«åãåºãã¾ãããã®ã¢ããã¼ãã®ããã«ããµãã«ã¼ãã³ã®å¼ã³åºãã¯ã¡ãã£ã¨éã£ããã®ã«ãªãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> print left_pad("oldString" => "pod", "width" => 10, "padChar" => "+"); </pre> <p class="original">And the subroutine itself looks like this:</p> <p>ããã¦ããµãã«ã¼ãã³èªèº«ã¯æ¬¡ã®ããã«ãªãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> sub left_pad { my %args = @_; my $newString = ($args{"padChar"} x ($args{"width"} - length $args{"oldString"})) . $args{"oldString"}; @@ -1315,9 +1334,9 @@ <h3>æ»ãå¤</h3> <p class="original">Like other Perl expressions, subroutine calls may display contextual behaviour. You can use the <code><a href="http://perldoc.perl.org/functions/wantarray.html">wantarray</a></code> function (which should be called <code>wantlist</code> but never mind) to detect what context the subroutine is being evaluated in, and return a result appropriate to that context:</p> -<p>ä»ã®Perlã®å¼ã¨åæ§ããµãã«ã¼ãã³å¼ã³åºãã¯ãã³ã³ããã¹ãä¾åã®æ¯ãèãããã¾ãã<code><a href="http://perldoc.jp/func/wantarray">wantarray</code>ã使ããã¨ãã§ãã¾ã(<code>wantlist</code>ã¨å¼ã°ããã¹ãã§ãããæ°ã«ããªãã§ãã ãã)ã使ã£ã¦ãã©ã®ã³ã³ããã¹ãã§ãµãã«ã¼ãã³ãè©ä¾¡ããã¦ãããããã§ãã¯ã§ããã³ã³ããã¹ãã«é©ããçµæãè¿ããã¨ãåºæ¥ã¾ã:</p> +<p>ä»ã®Perlã®å¼ã¨åæ§ããµãã«ã¼ãã³å¼ã³åºãã¯ãã³ã³ããã¹ãä¾åã®æ¯ãèãããã¾ãã<code><a href="http://perldoc.jp/func/wantarray">wantarray</a></code>ã使ããã¨ãã§ãã¾ã(<code>wantlist</code>ã¨å¼ã°ããã¹ãã§ãããæ°ã«ããªãã§ãã ãã)ã使ã£ã¦ãã©ã®ã³ã³ããã¹ãã§ãµãã«ã¼ãã³ãè©ä¾¡ããã¦ãããããã§ãã¯ã§ããã³ã³ããã¹ãã«é©ããçµæãè¿ããã¨ãåºæ¥ã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> sub contextualSubroutine { # Caller wants a list. Return a list return ("Everest", "K2", "Etna") if wantarray; @@ -1342,20 +1361,20 @@ <p>Perlã«ã¯ä¸ã¤ä»¥ä¸ã® - åããã»ã¹ãç£ã - æ¹æ³ãããã¾ããç¾å¨ã®ã¹ã¯ãªãããæ¢ããåããã»ã¹ãçµãã£ãããç¾å¨ã®ã¹ã¯ãªããã®è§£éãç¶ãã¾ããã©ã®æ¹æ³ã使ã£ã¦ãããã®ç´å¾ã§ãåããã»ã¹ã®çµäºæã«è¿ãããç¶æ ã¯ã¼ãã<a href="http://perldoc.jp/docs/perl/perlvar.pod">çµè¾¼ã®ã¹ã«ã©å¤æ°</a>ã®<code>$?</code>ã«å ¥ãã¾ããè¿ãããå¤ã®16ãããã®ä¸ä½8ãåããã¨ã§ããªã¿ã¼ã³ã³ã¼ããå¾ããã¨ãã§ãã¾ã: <code>$? >> 8</code>ã</p> <p class="original">The <code><a href="http://perldoc.perl.org/functions/system.html">system</a></code> function can be used to invoke another program with the arguments listed. The value returned by <code>system</code> is the same value with which <code>$?</code> is populated:</p> <p><code>system</code>é¢æ°ã¯ä»ã®ããã°ã©ã ãå¼æ°ã®ãªã¹ãã¨ä¸ç·ã«å¼ã³åºãã¾ãã<code>system</code>ã«ãã£ã¦è¿ãããå¤ã¯ã<code>$?</code>ã«å ¥ãã®ã¨åãå¤ã§ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my $rc = system "perl", "anotherscript.pl", "foo", "bar", "baz"; $rc >>= 8; print $rc; # "37" </pre> <p class="original">Alternatively, you can use backticks <code>``</code> 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>代ããã«ãããã¯ã¯ã©ã¼ã<code>``</code>ã使ã£ã¦ãã³ãã³ãã©ã¤ã³ã§å®éã®ã³ãã³ããèµ°ããã¦ãã³ãã³ãããã®æ¨æºåºåããã£ããã£ã§ãã¾ããã¹ã«ã©ã³ã³ããã¹ãã§ã¯ãå ¨ã¦ã®åºåã¯åä¸ã®æååã¨ãã¦å¸°ãã¾ãããªã¹ãã³ã³ããã¹ãã§ã¯ãå ¨ã¦ã®åºåã¯ä¸è¡ãã¤ã®æååã®é åã¨ãã¦è¿ããã¾ãã</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my $text = `perl anotherscript.pl foo bar baz`; print $text; # "foobarbaz" </pre> <p class="original">This is the behaviour which would be seen if <code>anotherscript.pl</code> contained, for example:</p> <p>ããã¯ã<code>anotherscript.pl</code>ãå«ãã§ããããè¦ãããæ¯ãèãã§ããä¾:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> use strict; use warnings; @@ -1372,7 +1391,7 @@ <p class="original">Use <code><a href="http://perldoc.perl.org/functions/open.html">open</a></code> to turn a scalar variable into a file handle. <code>open</code> must be supplied with a <i>mode</i>. The mode <code><</code> indicates that we wish to open the file to read from it:</p> <p><code><a href="http://perldoc.jp/func/open">open</a></code>ã使ã£ã¦ãã¹ã«ã©å¤æ°ããã¡ã¤ã«ãã³ãã«ã«ãã¾ãã<code>open</code>ã¯<i>ã¢ã¼ã</i>ã¨ã¨ãã«ä½¿ãããªããã°ããã¾ãããã¢ã¼ã <code><</code> ã¯ããã¡ã¤ã«ããèªã¿åºããããã¨ãæå³ãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my $f = "text.txt"; my $result = open my $fh, "<", $f; @@ -1385,7 +1404,7 @@ <p class="original">If successful, <code>open</code> returns a true value. Otherwise, it returns false and an error message is stuffed into the built-in variable <code>$!</code>. As seen above, you should always check that the <code>open</code> operation completed successfully. This checking being rather tedious, a common idiom is:</p> <p>æåããã°ã<code>open</code>ã¯çãè¿ãã¾ããããã§ãªããã°ãå½ãè¿ããã¨ã©ã¼ã¡ãã»ã¼ã¸ãçµã¿è¾¼ã¿ã®å¤æ°<code>$!</code>ã«å ¥ãã¾ããä¸ã§è¦ãããã«ã<code>open</code> æ¼ç®åãå®å ¨ã«æåãããã常ã«ãã§ãã¯ãã¹ãã§ãããã®ãã§ãã¯ã®æç¶ãã¯éå±ã§ãããããã¿ãããã¤ãã£ãªã ã¯æ¬¡ã®ãã®ã§ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> open(my $fh, "<", $f) || die "Couldn't open '".$f."' for reading because: ".$!; </pre> <p class="original">Note the need for parentheses around the <code>open</code> call's arguments.</p> @@ -1394,7 +1413,7 @@ <p class="original">To read a line of text from a filehandle, use the <code><a href="http://perldoc.perl.org/functions/readline.html">readline</a></code> built-in function. <code>readline</code> 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 <code>undef</code> if you've reached the end of the file.</p> <p>ãã¡ã¤ã«ãã³ãã«ããããã¹ãã®è¡ãèªãããã«ãçµè¾¼é¢æ°ã®<code><a href="http://perldoc.jp/func/readline">readline</a></code>ã使ãã¾ãã<code>readline</code>ã¯ãããã¹ãã®ä¸è¡å ¨ä½ãããã®çµããã«æ¹è¡ããã®ã¾ã¾å«ãã§è¿ã(ãã¶ããã¡ã¤ã«ã®æçµè¡ãé¤ãã¦)ãããã¡ã¤ã«ã®æå¾ã«éããã¨<code>undef</code>ãè¿ãã¾ãã</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> while(1) { my $line = readline $fh; last unless defined $line; @@ -1403,7 +1422,7 @@ </pre> <p class="original">To truncate that possible trailing line break, use <code><a href="http://perldoc.perl.org/functions/chomp.html">chomp</a></code>:</p> <p><code><a href="http://perldoc.jp/func/chomp">chomp</a></code>ã使ãã¨æ¹è¡ãåãé¤ãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> chomp $line; </pre> <p class="original">Note that <code>chomp</code> acts on <code>$line</code> in place. <code>$line = chomp $line</code> is probably not what you want.</p> @@ -1411,7 +1430,7 @@ <p class="original">You can also use <code><a href="http://perldoc.perl.org/functions/eof.html">eof</a></code> to detect that the end of the file has been reached:</p> <p><code><a href="http://perldoc.jp/func/eof">eof</a></code>ã使ã£ã¦ãã¡ã¤ã«ã®çµç«¯ãå¤æãããã¨ãã§ãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> while(!eof $fh) { my $line = readline $fh; # process $line... @@ -1420,21 +1439,21 @@ <p class="original">But beware of just using <code>while(my $line = readline $fh)</code>, because if <code>$line</code> turns out to be <code>"0"</code>, the loop will terminate early. If you want to write something like that, Perl provides the <code><></code> operator which wraps up <code>readline</code> in a fractionally safer way. This is very commonly-seen and perfectly safe:</p> <p>ã§ããã<code>while(my $line = readline $fh)</code>ã使ãã®ã¯æ³¨æãã¦ãã ããã<code>$line</code>ã<code>"0"</code>ã§åã£ãå ´åãã«ã¼ãã¯æ©ãã«çµãã£ã¦ãã¾ãã¾ãããã®ããã«æ¸ãããã®ãªãã°ãPerlã«ã¯<code><></code>æ¼ç®åããããå°ãå®å ¨ãªæ¹æ³ã§<code>readline</code>ãã©ãããã¦ãã¾ãã次ã®ãã®ã¯ãã¨ã¦ãããããå®å ¨ã«å®å ¨ãªãã®ã§ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> while(my $line = <$fh>) { # process $line... } </pre> <p class="original">And even:</p> <p>次ã®ããã«ãã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> while(<$fh>) { # process $_... } </pre> <p class="original">Writing to a file involves first opening it in a different mode. The mode <code>></code> indicates that we wish to open the file to write to it. (<code>></code> will clobber the content of the target file if it already exists and has content. To merely append to an existing file, use mode <code>>></code>.) Then, simply provide the filehandle as a zeroth argument for the <code>print</code> function.</p> <p>ãã¡ã¤ã«ã«æ¸ãè¾¼ãå ´åã¯ãéã£ãã¢ã¼ãã§æåã«éãã¾ããã¢ã¼ã <code>></code> ã¯ãæ¸ãè¾¼ã¿ç¨ã«ãã¡ã¤ã«ãéããã¨ãæ示ãã¾ãã(<code>></code>ã¯ãç®çã®ãã¡ã¤ã«ã®ä¸èº«ãå£ãã¾ããåç´ã«ã追å ãããå ´åã¯ã<code>>></code> ã®ã¢ã¼ãã使ãã¾ã)ãããããã<code>print</code>é¢æ°ã®0çªç®ã®å¼æ°ã¨ãã¦ããã¡ã¤ã«ãã³ãã«ãåã«æ¸¡ãã¾ãã</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> open(my $fh2, ">", $f) || die "Couldn't open '".$f."' for writing because: ".$!; print $fh2 "The eagles have left the nest"; </pre> @@ -1444,19 +1463,19 @@ <p class="original">File handles are actually closed automatically when they drop out of scope, but otherwise:</p> <p>ãã¡ã¤ã«ãã³ãã«ã¯ã¹ã³ã¼ããæããã¨èªåçã«éãããã¾ãããããã¯ã次ã®ããã«ãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> <a href="http://perldoc.jp/func/close">close</a> $fh2; close $fh; </pre> <p class="original">Three filehandles exist as global constants: <code>STDIN</code>, <code>STDOUT</code> and <code>STDERR</code>. These are open automatically when the script starts. To read a single line of user input:</p> <p>3ã¤ã®ãã¡ã¤ã«ãã³ãã«ãã°ãã¼ãã«ãªå®æ°ã¨ãã¦ããã¾ã: <code>STDIN</code>ã¨<code>STDOUT</code>ã¨<code>STDERR</code>ãããã¾ãããããã¯ã¹ã¯ãªãããéå§ãããã¨ãã«èªåçã«éããã¾ããã¦ã¼ã¶ã¼ã®å ¥åãä¸è¡èªãã«ã¯:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my $line = <STDIN>; </pre> <p class="original">To just wait for the user to hit Enter:</p> <p>ã¦ã¼ã¶ã¼ãã¨ã³ã¿ã¼ãæ¼ãã¾ã§å¾ ã¤ã ãã§ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> <STDIN>; </pre> <p class="original">Calling <code><></code> with no filehandle reads data from <code>STDIN</code>, or from any files named in arguments when the Perl script was called.</p> @@ -1469,7 +1488,7 @@ <p class="original">The function <code>-e</code> is a built-in function which tests whether the named file exists.</p> <p>é¢æ°<code>-e</code> ã¯çµè¾¼ã®é¢æ°ã§ä¸ããããååã®ãã¡ã¤ã«ãåå¨ãããã©ããããã¹ããã¾ãã</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> print "what" unless -e "/usr/bin/perl"; </pre> <p class="original">The function <code>-d</code> is a built-in function which tests whether the named file is a directory.</p> @@ -1488,7 +1507,7 @@ <p class="original">Match operations are performed using <code>=~ m//</code>. In scalar context, <code>=~ m//</code> returns true on success, false on failure.</p> <p>ãããã¯ããããæ¼ç®å<code>=~ m//</code>ã使ã£ã¦ããã¾ããã¹ã«ã©ã³ã³ããã¹ãã§ã¯ã<code>=~ m//</code>ã¯ãæåãªãçãè¿ãã失æãªãå½ãè¿ãã¾ãã</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my $string = "Hello world"; if($string =~ m/(\w+)\s+(\w+)/) { print "success"; @@ -1496,14 +1515,14 @@ </pre> <p class="original">Parentheses perform sub-matches. After a successful match operation is performed, the sub-matches get stuffed into the built-in variables <code>$1</code>, <code>$2</code>, <code>$3</code>, ...:</p> <p>æ¬å¼§ã¯ãµããããã«ãªãã¾ããããããæåãããããµããããã¯çµè¾¼å¤æ°ã®<code>$1</code>, <code>$2</code>, <code>$3</code>, ...ã«ããããããã®ãå ¥ãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> print $1; # "Hello" print $2; # "world" </pre> <p class="original">In list context, <code>=~ m//</code> returns <code>$1</code>, <code>$2</code>, ... as a list.</p> <p>ãªã¹ãã³ã³ããã¹ãã§ã¯ã<code>=~ m//</code> ã¯<code>$1</code>, <code>$2</code>, ... ããªã¹ãã¨ãã¦è¿ãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my $string = "colourless green ideas sleep furiously"; my @matches = $string =~ m/(\w+)\s+((\w+)\s+(\w+))\s+(\w+)\s+(\w+)/; @@ -1513,7 +1532,7 @@ <p class="original">Substitution operations are performed using <code>=~ s///</code>.</p> <p>ç½®ææä½ã¯<code>=~ s///</code>ã§è¡ãã¾ãã</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my $string = "Good morning world"; $string =~ s/world/Vietnam/; print $string; # "Good morning Vietnam" @@ -1527,7 +1546,7 @@ <p class="original">In scalar context, each <code>=~ m//g</code> call finds another match after the previous one, returning true on success, false on failure. You can access <code>$1</code> and so on afterwards in the usual way. For example:</p> <p>ã¹ã«ã©ã³ã³ããã¹ãã§ã¯ãããããã®<code>=~ m//g</code> å¼ã³åºãã¯åã®ãã®ã®å¾ã®ä»ã®ããããæ¢ããæåããã¨çãè¿ãã失æããã¨å¤ãè¿ãã¾ããããããæ¹æ³ã§<code>$1</code>ãªã©ã«ã¢ã¯ã»ã¹åºæ¥ã¾ãã ä¾:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my $string = "a tonne of feathers or a tonne of bricks"; while($string =~ m/(\w+)/g) { print "'".$1."'\n"; @@ -1536,7 +1555,7 @@ <p class="original">In list context, an <code>=~ m//g</code> call returns all of the matches at once.</p> <p>ãªã¹ãã³ã³ããã¹ãã§ã¯ã<code>=~ m//g</code>å¼ã³åºãã¯ãããããããã®ãä¸åº¦ã«å ¨é¨è¿ãã¾ãã</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my @matches = $string =~ m/(\w+)/g; print join ", ", map { "'".$_."'" } @matches; </pre> @@ -1544,7 +1563,7 @@ <p class="original">An <code>=~ s///g</code> call performs a global search/replace and returns the number of matches. Here, we replace all vowels with the letter "r".</p> <p><code>=~ s///g</code>å¼ã³åºãã¯ã°ãã¼ãã«ãªæ¤ç´¢/ç½®æã§ãããããæ°ãè¿ãã¾ããããã§ã¯ããã¹ã¦ã®æ¯é³ãæå"r"ã«ç½®æãã¦ãã¾ãã</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> # Try once without /g. $string =~ s/[aeiou]/r/; print $string; # "r tonne of feathers or a tonne of bricks" @@ -1562,7 +1581,7 @@ <p><code>/i</code> ãã©ã°ã¯ãããã¨ç½®æãã±ã¼ã¹ã¤ã³ã»ã³ã·ãã£ãã«ãã¾ãã</p> <p class="original">The <code>/x</code> flag allows your regular expression to contain whitespace (e.g., line breaks) and comments.</p> <p class="original"><code>/x</code>ãã©ã°ã¯æ£è¦è¡¨ç¾ã®ä¸ã«ç©ºç½(e.g. æ¹è¡)ãã³ã¡ã³ããå«ãããã¨ãã§ããããã«ãã¾ãã</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> "Hello world" =~ m/ (\w+) # one or more word characters [ ] # single literal space, stored inside a character class @@ -1584,7 +1603,7 @@ <p class="original">A <i>module</i> is a <code>.pm</code> file that you can <i>include</i> in another Perl file (script or module). A module is a text file with exactly the same syntax as a <code>.pl</code> Perl script. An example module might be located at <code>C:\foo\bar\baz\Demo\StringUtils.pm</code> or <code>/foo/bar/baz/Demo/StringUtils.pm</code>, and read as follows:</p> <p>A <i>ã¢ã¸ã¥ã¼ã«</i>ã¯ãä»ã®Perlãã¡ã¤ã«(ã¹ã¯ãªãããã¢ã¸ã¥ã¼ã«)ã«<i>å«ãã</i>ãã¨ãåºæ¥ã<code>.pm</code>ãã¡ã¤ã«ã§ããã¢ã¸ã¥ã¼ã«ã¯ <code>.pl</code> Perlã¹ã¯ãªããã¨ã¾ã£ããåãã·ã³ã¿ãã¯ã®ã¹ããã¹ããã¡ã¤ã«ã§ããä¾ã®ã¢ã¸ã¥ã¼ã«ã¯ã<code>C:\foo\bar\baz\Demo\StringUtils.pm</code> ã <code>/foo/bar/baz/Demo/StringUtils.pm</code>ã«ããã¾ããç¶ããèªãã§ãã ãã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> use strict; use warnings; @@ -1614,7 +1633,7 @@ <p class="original">Once the Perl module is created and <code>perl</code> knows where to look for it, you can use the <code><a href="http://perldoc.perl.org/functions/require.html">require</a></code> built-in function to search for and execute it during a Perl script. For example, calling <code>require Demo::StringUtils</code> causes the Perl interpreter to search each directory listed in <code>PERL5LIB</code> in turn, looking for a file called <code>Demo/StringUtils.pm</code>. After the module has been executed, the subroutines that were defined there suddenly become available to the main script. Our example script might be called <code>main.pl</code> and read as follows:</p> <p>Perlã¢ã¸ã¥ã¼ã«ãä½ããã¦ã<code>perl</code>ããããã©ãã«ããããç¥ã£ã¦ããã°ãçµè¾¼ã®<code>require</code>é¢æ°ã使ã£ã¦æ¢ããPerlã®ã¹ã¯ãªããä¸ã§å®è¡ãããã¨ãã§ãã¾ããä¾ãã°ã<code>require Demo::StringUtils</code>ãå¼ã¶ã¨ãPerlã¤ã³ã¿ã¼ããªã¿ã¯<code>PERL5LIB</code>ã«ãªã¹ãããã¦ãããã£ã¬ã¯ããªãé çªã«ã <code>Demo/StringUtils.pm</code>ã¨ãããã¡ã¤ã«ãæ¢ãã¾ããã¢ã¸ã¥ã¼ã«ããã¼ããããããããã§å®ç¾©ããããµãã«ã¼ãã³ã¯ãçªç¶ã«ã¡ã¤ã³ã¹ã¯ãªããããå©ç¨ã§ããããã«ãªãã¾ãããã®ä¾ã®ã¹ã¯ãªããã<code>main.pl</code>ã¨å¼ã³ã¾ããããç¶ãã¦èªãã§ããã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> use strict; use warnings; @@ -1635,7 +1654,7 @@ <p class="original">A <i>package</i> is a namespace in which subroutines can be declared. Any subroutine you declare is implicitly declared within the current package. At the beginning of execution, you are in the <code>main</code> package, but you can switch package using the <code><a href="http://perldoc.perl.org/functions/package.html">package</a></code> built-in function:</p> <p><i>package</i>ã¯åå空éã§ããã®ä¸ã§ããµãã«ã¼ãã³ã宣è¨ã§ãã¾ãã宣è¨ãããµãã«ã¼ãã³ã¯ãæé»çã«ãç¾å¨ã®ããã±ã¼ã¸å ã«å®£è¨ããã¾ããå®è¡ã®æåã¯ã<code>main</code>ããã±ã¼ã¸ã«ãªãã¾ãããçµè¾¼é¢æ°ã®<code>package</code>ã使ã£ã¦ãããã±ã¼ã¸ãåãæ¿ãããã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> use strict; use warnings; @@ -1657,7 +1676,7 @@ <p class="original">Any time you call a subroutine, you implicitly call a subroutine which is inside the current package. Alternatively, you can explicitly provide a package. See what happens if we continue the above script:</p> <p>ãµãã«ã¼ãã³ãå¼ãã ã¨ãã¯ãã¤ã§ããæé»ã«ç¾å¨ã®ããã±ã¼ã¸å ã®ãµãã«ã¼ãã³ãå¼ãã§ãã¾ãã代ããã«ãããã±ã¼ã¸ãæ示çã«æ¸ããã¨ãã§ãã¾ããä¸ã®ã¹ã¯ãªãããå®è¡ããããä½ãèµ·ããã§ãããã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> subroutine(); # "kingedward" main::subroutine(); # "universe" Food::Potatoes::subroutine(); # "kingedward" @@ -1666,7 +1685,7 @@ <p class="original">So the logical solution to the problem described above is to modify <code>C:\foo\bar\baz\Demo\StringUtils.pm</code> or <code>/foo/bar/baz/Demo/StringUtils.pm</code> to read:</p> <p>ã§ãã®ã§ãä¸ã§è¿°ã¹ãåé¡ã®è«ççãªè§£æ±ºçã¯<code>C:\foo\bar\baz\Demo\StringUtils.pm</code>ã<code>/foo/bar/baz/Demo/StringUtils.pm</code>ãå¤æ´ãããã¨ã§ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> use strict; use warnings; @@ -1684,7 +1703,7 @@ <p class="original">And modify <code>main.pl</code> to read:</p> <p>ããã¦ã<code>main.pl</code>ãå¤æ´ãã¾ã</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> use strict; use warnings; @@ -1738,7 +1757,7 @@ <p class="original">A quick example makes this clearer. An example module <code>Animal.pm</code> containing a class <code>Animal</code> reads like this:</p> <p>以ä¸ãç°¡åãªä¾ã§ãããã¯ã£ããããã¾ãã ä¾ã®ã¢ã¸ã¥ã¼ã«ã¨ãã¦<code>Animal.pm</code>ã®ã¯ã©ã¹<code>Animal</code>ã¯ã次ã®ããã«ãªãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> use strict; use warnings; @@ -1768,7 +1787,7 @@ <p class="original">And we might make use of this class like so:</p> <p>ãã®ã¯ã©ã¹ã使ã次ã®ããã«ä½¿ãã§ããã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> require Animal; my $animal = { @@ -1785,13 +1804,13 @@ <p class="original">You can still work with the original hash in the usual way:</p> <p>ã¾ã ãé常ã®ããæ¹ã§ãªãªã¸ãã«ã®ããã·ã¥ãæä½ã§ãã¾ã</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> print "Animal has ", $animal->{"legs"}, " leg(s)"; </pre> <p class="original">But you can now also call methods on the object using the same <code>-></code> operator, like so:</p> <p>ã§ãããåã<code>-></code>ãªãã¬ã¼ã¿ã§ãªãã¸ã§ã¯ãããã¡ã½ãããå¼ã¶ãã¨ãã§ãã¾ãã次ã®ããã«ãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> $animal->eat("insects", "curry", "eucalyptus"); </pre> <p class="original">This final call is equivalent to <code>Animal::eat($animal, "insects", "curry", "eucalyptus")</code>.</p> @@ -1802,7 +1821,7 @@ <p class="original">A constructor is a class method which returns a new object. If you want one, just declare one. You can use any name you like. For class methods, the first argument passed is not an object but a class name. In this case, <code>"Animal"</code>:</p> <p>ã³ã³ã¹ãã©ã¯ã¿ã¯ã¯ã©ã¹ã¡ã½ããã§ãæ°ãããªãã¸ã§ã¯ããè¿ãã¾ããã³ã³ã¹ãã©ã¯ã¿ã欲ãããã°ãããã宣è¨ããã ãã§ãã好ããªååã使ãã¾ããã¯ã©ã¹ã¡ã½ããã«ã¯ãæåã®å¼æ°ã¨ãã¦ããªãã¸ã§ã¯ãã§ã¯ãªãã¯ã©ã¹åã渡ãã¾ãããã®ã±ã¼ã¹ã§ã¯ã<code>"Animal"</code>ã§ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> use strict; use warnings; @@ -1818,7 +1837,7 @@ <p class="original">And then use it like so:</p> <p>次ã®ããã«ä½¿ãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> my $animal = Animal->new(); </pre> @@ -1828,7 +1847,7 @@ <p class="original">To create a class inheriting from a parent class, use <code>use parent</code>. Let's suppose we subclassed <code>Animal</code> with <code>Koala</code>, located at <code>Koala.pm</code>:</p> <p>ãã¼ã¹ã¯ã©ã¹ããç¶æ¿ãã¦ããã¯ã©ã¹ãä½ãã«ã¯ã<code>use parent</code>ã使ãã¾ãã<code>Animal</code>ããµãã¯ã©ã¹åãã¦<code>Koala</code>ã§ãµãã¯ã©ã¹ãä½ãã¨ãã¾ããå ´æã¯<code>Koala.pm</code>ã«ãªãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> use strict; use warnings; @@ -1850,7 +1869,7 @@ <p class="original">And some sample code:</p> <p>ãµã³ãã«ã³ã¼ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> use strict; use warnings; @@ -1870,7 +1889,7 @@ <p class="original">A <code><a href="http://perldoc.perl.org/functions/BEGIN.html">BEGIN</a></code> block is executed as soon as <code>perl</code> has finished parsing that block, even before it parses the rest of the file. It is ignored at execution time:</p> <p><code><a href="http://perldoc.jp/func/BEGIN">BEGIN</a></code>ãããã¯ã¯<code>perl</code>ããã®ãããã¯ããã¼ã¹ãçµããã¨ããã«å®è¡ããã¾ããã³ã³ãã¤ã©ãã¾ã å ¨ã¦ããã¼ã¹ãã¦ããªãã¦ãã§ããBEGINãããã¯ã¯å®è¡æã«ã¯ç¡è¦ããã¾ãã</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> use strict; use warnings; @@ -1887,7 +1906,7 @@ <p class="original">A <code>BEGIN</code> block is executed as soon as the block has been parsed. Once this is done, <em>parsing</em> resumes at the end of the <code>BEGIN</code> block. Only once the whole script or module has been parsed is any of the code outside of <code>BEGIN</code> blocks executed.</p> <p><code>BEGIN</code>ãããã¯ã¯ãã®ãããã¯ã解éãããã¨åæã«å®è¡ããã¾ããä¸åº¦å®è¡ãããã¨ã<em>ã³ã¼ãã®ãã¼ã¹</em>ã¯<code>BEGIN</code>ãããã¯ã®çµããã§åéããã¾ããä¸åº¦ã¹ã¯ãªããå ¨ä½ãã¢ã¸ã¥ã¼ã«ããã¼ã¹ããã¦ããã°ã<code>BEGIN</code>ãããã¯ã®å¤å´ã®ãããããå®è¡ããã¾ãã </p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> use strict; use warnings; @@ -1903,7 +1922,7 @@ </pre> <p class="original">Because they are executed at compilation time, a <code>BEGIN</code> block placed inside a conditional block will <em>still</em> be executed first, even if the conditional evaluates to false and despite the fact that the conditional <em>has not been evaluated at all yet</em> and in fact <em>may never be evaluated</em>.</p> <p>ã³ã³ãã¤ã«æã«å®è¡ãããã®ã§ã<code>BEGIN</code>ãããã¯ãæ¡ä»¶ãããã¯ã®ä¸ã«ãã£ã¦ãã<em>ã¾ã </em>æåã«å®è¡ããã¾ãããã¨ããæ¡ä»¶ã®è©ä¾¡ãå½ã§ãããæ¡ä»¶ã<em>ã¾ã ã¾ã£ããè©ä¾¡ããã¦ããªã</em>ã«ãããããããå®éã«ã¯ã<em>è©ä¾¡ããããã¨ããªã</em>ã¨ãã¦ãã§ãã</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> if(0) { BEGIN { print "This will definitely get printed"; @@ -1914,7 +1933,7 @@ <p class="original"><strong>Do not put <code>BEGIN</code> blocks in conditionals!</strong> If you want to do something conditionally at compile time, you need to put the conditional <em>inside</em> the <code>BEGIN</code> block:</p> <p> <strong><code>BEGIN</code>ãããã¯ãæ¡ä»¶ã®ä¸ã«ç½®ãã¦ã¯è¡ãã¾ãã!</strong> ã³ã³ãã¤ã«æã«ä½ãããã®æ¡ä»¶ä»ãã®ãã¨ãããããã°ã<code>BEGIN</code>ãããã¯ã®<em>ä¸ã«</em>æ¡ä»¶æãç½®ããªããã°ãªãã¾ãã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> BEGIN { if($condition) { # etc. @@ -1927,14 +1946,14 @@ <p>ããã§ãããããããããã«ãããããã±ã¼ã¸ãã¢ã¸ã¥ã¼ã«ã¯ã©ã¹ã¡ã½ããã¨<code>BEGIN</code>ãããã¯ã®æå³ãç解ããã®ã§ãããè¦ããã<code><a href="http://perldoc.jp/func/use">use</a></code>é¢æ°ã«ã¤ãã¦èª¬æã§ãã¾ãã</p> <p class="original">The following three statements:</p> <p>以ä¸ã®3ã¤ã®ã¹ãã¼ãã¡ã³ãã¯:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> use Caterpillar ("crawl", "pupate"); use Caterpillar (); use Caterpillar; </pre> <p class="original">are respectively equivalent to:</p> <p>以ä¸ã¨ããããç価ã§ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> BEGIN { require Caterpillar; Caterpillar->import("crawl", "pupate"); @@ -1969,7 +1988,7 @@ <p class="original">This concept is easiest to grasp using an example. Here's what <code>Caterpillar.pm</code> looks like:</p> <p>ãã®ã³ã³ã»ããã¯ä¾ã使ãã¨ææ¡ããããã§ãããã<code>Caterpillar.pm</code>ã¯æ¬¡ã®ãããªãã®ã§ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> use strict; use warnings; @@ -1992,7 +2011,7 @@ <p class="original">Another piece of code may then <code>import()</code> these subroutines by name, typically using a <code>use</code> statement:</p> <p>ã³ã¼ãã®å¥ã®é¨åããååã§ãããã®ãµãã«ã¼ãã³ã<code>import()</code>ããã§ããããå ¸åçã«ã<code>use</code>ã¹ãã¼ãã¡ã³ãã使ãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> use strict; use warnings; @@ -2007,7 +2026,7 @@ <p class="original">Note: regardless of the content of <code>@EXPORT_OK</code>, every method can always be called "longhand":</p> <p>注æ: <code>@EXPORT_OK</code>ã®å 容ã¯ç¡è¦ãã¦ããã¹ã¦ã®ã¡ã½ããã¯ã常ã«"longhand"ã§å¼ã¶ãã¨ãã§ãã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> use strict; use warnings; use Caterpillar (); # no subroutines named, no import() call made @@ -2026,7 +2045,7 @@ <p class="original">The Exporter module also defines a package variable called <code>@EXPORT</code>, which can also be populated with a list of subroutine names.</p> <p>Exporter ã¢ã¸ã¥ã¼ã«ã¯ <code>@EXPORT</code>ã¨å¼ã°ããããã±ã¼ã¸å¤æ°ãå®ç¾©ãã¾ããããã«ããµãã«ã¼ãã³åã®ãªã¹ããå ¥ãã¾ãã</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> use strict; use warnings; @@ -2047,7 +2066,7 @@ <p class="original">The subroutines named in <code>@EXPORT</code> are exported if <code>import()</code> is called with no arguments at all, which is what happens here:</p> <p><code>@EXPORT</code>ã«æ¸ããããµãã«ã¼ãã³ã¯ <code>import()</code>ãå¼æ°ãªãã§å¼ã°ããå ´åã«ã¨ã¯ã¹ãã¼ãããã¾ãããã®ä¾ã§èµ·ãããã¨ãèµ·ãã¾ãã</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> use strict; use warnings; use Caterpillar; # calls import() with no arguments @@ -2079,7 +2098,7 @@ <li> <p class="original">There's an alternate syntax, <code>qw{ }</code>, for declaring arrays. This is often seen in <code>use</code> statements:</p> <p>é åã宣è¨ããããã®ä»£ããã®ã·ã³ã¿ãã¯ã¹ã<code>qw{ }</code>ãããã¾ãã<code>use</code>ã¹ãã¼ãã¡ã³ãã§ããè¦ããã¾ã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> use Account qw{create open close suspend delete}; </pre> <p class="original">There are <a href="http://perldoc.perl.org/perlop.html#Quote-and-Quote-like-Operators">many other quote-like operators</a>.</p> @@ -2104,12 +2123,12 @@ <li> <p class="original">Warning! Many built-in functions can be called with no arguments, <strong>causing them to operate on <code>$_</code> instead</strong>. Hopefully this will help you understand formations like:</p> <p>注æ! å¤ãã®çµè¾¼é¢æ°ã§ã¯ãå¼æ°ãªãã§å¼ã¶ã¨<strong><code>$_</code>ã代ããã«æ¸¡ããã¾ã</strong>ãé¡ããã°ã次ã®ãããªå½¢ãç解ããå©ãã«ãªãã°è¯ãã®ã§ãã:</p> -<pre class="perl prettyprint"> +<pre class="prittyprint lang-perl"> print foreach @array; </pre> <p class="original">and</p> <p>ããã¦</p> -<pre class="perl prettyprint"> +<pre class="prettyprint lang-perl"> foreach ( @array ) { next unless defined; }