ktats****@users*****
ktats****@users*****
2012年 8月 5日 (日) 04:24:08 JST
Index: docs/articles/qntm.org/files/perl/perl.html diff -u /dev/null docs/articles/qntm.org/files/perl/perl.html:1.1 --- /dev/null Sun Aug 5 04:24:08 2012 +++ docs/articles/qntm.org/files/perl/perl.html Sun Aug 5 04:24:08 2012 @@ -0,0 +1,1764 @@ +<!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" /> + <title>Perl</title> + + <style type="text/css"> + body { + padding: 2em; + font-family: Tahoma; + } + pre { + margin-left: 2em; + } + pre, tt { + color: #348275; + } + </style> + </head> + + <body> +<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></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> + +<p>Perlã¯åçãªãåçåä»ãã®ãé«ç´ãªãã¹ã¯ãªãã(ã¤ã³ã¿ã¼ããªã¿ã§è§£éããã)è¨èªã§ãPHPãPythonã¨ã¨ãã«ããæ¯è¼ããã¾ããPerlã®ã·ã³ã¿ãã¯ã¹ã¯ãæã®shellã¹ã¯ãªãããã¼ã«ããå¤ãã®é¨åãåãã¦ãããããã«ããæ··ä¹±ãããã·ã³ãã«ã使ãéãã¦ãããã¨ã§æåã§ãããããã®å¤§å¤æ°ãGoogleã§æ¤ç´¢ãããã¨ã¯ä¸å¯è½ã§ããPerlã®shellã¹ã¯ãªããããåãç¶ãã éºç£ã¯ã<i>glueã³ã¼ã</i>(ä»ã®ã¹ã¯ãªãããããã°ã©ã ã¨ä¸ç·ã«ãªã³ã¯ããã¦ããã¹ã¯ãªãã)ãæ¸ãã®ã«ã大å¤å½¹ã«ç«ã¡ã¾ããPerlã¯ãçæ³çã«ããã¹ããã¼ã¿ãå¦çãããã¨ããããã¹ããã¼ã¿ãçã¿åºããã¨ã«é©ãã¦ãã¾ããPerlã¯åºãè¡ã渡ããæåã§ããã極ãã¦ãã¼ã¿ãã«ã§ãååã«ãµãã¼ãããã¦ãã¾ããPerlã¯"There's More Than One Way To Do It(ããæ¹ã¯ã²ã¨ã¤ãããªã )" (TMTOWTDI)ã®å²å¦ã§ãã¶ã¤ã³ããã¾ãã(Pythonã¨æ¯è¼ããã¨ã"there should be one - and preferably only one - obvious way to do it(æãããªããæ¹ã¯ãã²ã¨ã¤ã§ããã¹ãã§ããããããã²ã¨ã¤ã®ã¿ã§ããã¹ã)")ã</p> + +<p class=original>Perl has horrors, but it also has some great redeeming features. In this respect it is like every other programming language ever created.</p> +<p>Perlã«ã¯æãããã¨ãããããã¾ããããããè£ã£ã¦ä½ãããã¨ãããããã¾ãããã®ç¹ã§ãä»ã¾ã§ã«ä½ãããå ¨ã¦ã®ä»ã®ããã°ã©ãã³ã°è¨èªã¨åãã§ãã</p> +<p class=original>This document is intended to be informative, not evangelical. It is aimed at people who, like me:</p> +<p>ãã®ããã¥ã¡ã³ãã¯ãæ å ±ãä¸ãããã¨ãæå³ãã¦ãããç±å¿ã«æ®åããããã®ã§ã¯ããã¾ããã以ä¸ã®ãããªãç§ã®ãããªäººåãã®ãã®ã§ã:</p> +<ul class=original> + <li>dislike the official Perl documentation at <a href="http://perl.org">http://perl.org/</a> for being intensely technical and giving far too much space to very unusual edge cases</li> + <li>learn new programming languages most quickly by "axiom and example"</li> + <li>wish Larry Wall would get to the point</li> + <li>already know how to program in general terms</li> + <li>don't care about Perl beyond what's necessary to get the job done.</li> +</ul> + +<ul> + <li>æè¡çéãã¦ãä¸è¬çã§ã¯ãªãã¨ãã¸ã±ã¼ã¹ã«å¤ãã®é¨åãå²ãã¦ãã<a href="http://perl.org">http://perl.org/</a>ã«ãããå ¬å¼ã®Perlã®ããã¥ã¡ã³ããå«ã</li> + <li>"åçã¨ä¾"ã§ãã£ãã¨å¦ã³ãã</li> + <li>Larry Wallã«ã¯ã£ããè¨ã£ã¦ã»ãã</li> + <li>ä¸è¬çãªç¨èªã§ããã°ã©ã ãããã¨ããã§ã«ç¥ã£ã¦ãã</li> + <li>ä»äºãçµããããã«ä½ãå¿ è¦ãã¨ãããã¨ä»¥ä¸ã®ãã¨ãPerlã«ã¤ãã¦æ°ã«ããªã</li> +</ul> + +<p class=original>This document is intended to be as short as possible, but no shorter.</p> + +<p>ãã®ããã¥ã¡ã³ãã¯å¯è½ãªéãçããã¦ãã¾ããã足ããªããã¨ã®ãªãããã«ãã¦ãã¾ãã</p> + +<h2 class=original>Preliminary notes</h2> +<h2>åæ¸ã</h2> +<ul class=original> +<li><p>The following can be said of almost every declarative statement in this document: "that's not, strictly speaking, true; the situation is actually a lot more complicated". I've deliberately omitted or neglected to bother to research the "full truth" of the matter for the same reason that there's no point in starting off a Year 7 physics student with the Einstein field equations. If you see a serious lie, point it out, but I reserve the right to preserve certain critical lies-to-children.</p></li> +<li><p>Throughout this document I'm using example <tt>print</tt> statements to output data but not explicitly appending line breaks. This is done to prevent me from going crazy and to give greater attention to the actual string being printed in each case, which is invariably more important. In many examples, this results in alotofwordsallsmusheduptogetherononeline if the code is run in reality. Try to ignore this. Or, in your head or in practice, set <tt>$\</tt> (also known as <tt>$OUTPUT_RECORD_SEPARATOR</tt>) to <tt>"\n"</tt>, which adds the line breaks automatically. Or substitute the <tt>say</tt> function.</p></li> +<li><p>Perl docs all have short, memorable names, such as <a href="http://perldoc.perl.org/perlsyn.html">perlsyn</a> which explains Perl syntax, <a href="http://perldoc.perl.org/perlop.html">perlop</a> (operators/precedence), <a href="http://perldoc.perl.org/perlfunc.html">perlfunc</a> (built-in functions) et cetera. <strong><a href="http://perldoc.perl.org/perlvar.html">perlvar</a> is the most important of these</strong>, because this is where you can look up un-Googlable variable names like <tt>$_</tt>, <tt>$"</tt> and <tt>$|</tt>.</p></li> +</ul> + +<ul> +<li><p>以ä¸ã®ãã¨ãããã®ããã¥ã¡ã³ãå ã®ã»ã¨ãã©ãã¹ã¦ã®å®£è¨æã§è¨ããã¨ãåºæ¥ã¾ã: "ããã¯ãå³æ ¼ãªæå³ã§ã®ãçå®ã§ã¯ããã¾ãã; ç¶æ³ã¯å®éã«ã¯ãã£ã¨è¤éã§ã"ãç§ã¯æ æã«åé¡ã®"å®å ¨ãªçå®"ããããã調ã¹ããã¨ããçç¥ããããç¡è¦ãã¦ãã¾ãããããã¯ãã¢ã¤ã³ã·ã¥ã¿ã¤ã³ã®å ´ã®æ¹ç¨å¼ã§7å¹´çã®ç©çå¦ã®å¦çãå§ããã®ã«æå³ããªãã®ã¨åãçç±ã§ããããæ·±å»ãªåãè¦ã¤ããããææãã¦ãã ãããã§ãããç§ã«ã¯åã©ãã«ã¤ãéè¦ãªåããã®ã¾ã¾ã«ãã¦ãã権å©ãããã¾ãã</p></li> +<li><p>ãã®ããã¥ã¡ã³ããéãã¦ãä¾ã«ããã¦<tt>print</tt>ã¹ãã¼ãã¡ã³ãããã¼ã¿ãåºåããããã«ä½¿ã£ã¦ãã¾ãããæ示çã«æ¹è¡ã追å ãã¦ãã¾ããã +ããã¯ãç§ãæ°éãã«ãããªãããã§ãããããããã®ã±ã¼ã¹ã§åºåããããããéè¦ãªå®éã®æååã«å¤ãã®æ³¨æãåããããããã§ããå¤ãã®ä¾ã§ãå®éã«ã³ã¼ããåããã¨ãalotofwordsallsmusheduptogetherononelineã®ãããªçµæã«ãªãã¾ããç¡è¦ãã¦ãã ãããã¾ãã¯ãé ã®ä¸ã§ãç·´ç¿ã§ã<tt>$\</tt>(<tt>$OUTPUT_RECORD_SEPARATOR</tt>ã¨ãè¨ã)ãã<tt>"\n"</tt>ã«ãã¦ãã ãããããã§ãè¡æ«ã«æ¹è¡ãèªåçã«è¿½å ããã¾ããã¾ãã¯ã代ããã«ã<tt>say</tt>é¢æ°ã使ã£ã¦ãã ããã</p></li> +<li><p>Perlã®ããã¥ã¡ã³ãã¯ãã¹ã¦ãçããè¨æ¶ã«æ®ãååã«ãªã£ã¦ãã¾ããä¾ãã°<a href="http://perldoc.jp/docs/perl/5.14.1/perlsyn.pod">perlsyn</a>ã¯ãPerlã®ã·ã³ã¿ãã¯ã¹ã説æãã¦ãã¾ã, <a href="http://perldoc.jp/docs/perl/5.14.1/perlop.pod">perlop</a> (ãªãã¬ã¼ã¿/åªå é ä½), <a href="http://perldoc.jp/docs/perl/5.14.1/perlfunc.pod">perlfunc</a> (çµè¾¼é¢æ°) ãªã©ã <strong><a href="http://perldoc.jp/docs/perl/5.14.1/perlvar.pod">perlvar</a> ã¯ãããã®ä¸ã§æãéè¦ãªãã®ã§ãã</strong>ãªããªããããã«ã¯ãGoogleã§æ¤ç´¢ã§ããªã<tt>$_</tt>, <tt>$"</tt> and <tt>$|</tt>ã®ãããªå¤æ°åãããããã§ãã</p></li> +</ul> + +<h2 class="original">Hello world</h2> + +<h2>Hello world</h2> + +<p class=original>A Perl <i>script</i> is a text file with the extension <tt>.pl</tt>.</p> +<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"> +use strict; +use warnings; + +print "Hello world"; +</pre> +<p class=original>Perl has no explicit compilation step (there <em>is</em> a "compilation" step, but it is performed automatically before execution and no compiled binary is generated). Perl scripts are interpreted by the Perl interpreter, <tt>perl</tt> or <tt>perl.exe</tt>:</p> +<p>Perlã«ã¯ãæ確ãªã³ã³ãã¤ã«ã¹ãããã¯ããã¾ãã("ã³ã³ãã¤ã«"ã¯<em>ããã¾ã</em>ãã§ãããããã¯å®è¡åã«èªåçã«è¡ãããã³ã³ãã¤ã«ããããã¤ããªã¯ä½ããã¾ãã)ã Perlã¹ã¯ãªããã¯Perlã¤ã³ã¿ã¼ããªã¿ã<tt>perl</tt>ã<tt>perl.exe</tt>ã§è§£éããã¾ã:</p> + +<pre class="bash"> +perl helloworld.pl [arg0 [arg1 [arg2 ...]]] +</pre> +<p class=original>A few immediate notes. Perl's syntax is highly permissive and it will allow you to do things which result in ambiguous-looking statements with unpredictable behaviour. There's no point in me explaining what these behaviours are, because you want to avoid them. The way to avoid them is to put <tt>use strict; use warnings;</tt> at the very top of every Perl script or module that you create. Statements of the form <tt>use</tt> <i><whatever></i> are <i>pragmas</i>. A pragma is a signal to the Perl compiler, and changes the way in which the initial syntactic validation is performed. These lines take effect at compile time, and have no effect when the interpreter encounters them at run time.</p> + +<p>å½é¢ã®ã¡ã¢ã¨ãã¦ããã¤ããPerlã®ã·ã³ã¿ãã¯ã¹ã¯ããªãå¯å¤§ãªã®ã§ãçµæã¨ãã¦æ¯ãèããäºæ¸¬ã§ããªãææ§ã«è¦ãããããªã»ã³ãã³ã¹ãæ¸ããã¨ãããªãã«è¨±ãã¾ããç§ã«ã¨ã£ã¦ã¯æå³ããªãã®ã§ããããã®æ¯ãèãã«é¢ãã¦èª¬æãã¾ããããªããªããããªãã¯ããããé¿ãããã§ããããããããããé¿ããæ¹æ³ã¯ã<tt>use strict;use warnings;</tt>ããããªãã®ä½ãå ¨ã¦ã®perlã¹ã¯ãªãããã¢ã¸ã¥ã¼ã«ã®ä¸ã®æ¹ã«ç½®ããã¨ã§ãã<tt>use</tt> <i><ä½ã§ã></i>ã®ã¹ãã¼ãã¡ã³ãã¯<i>ãã©ã°ã</i>ã§ãããã©ã°ãã¯Perlã³ã³ãã¤ã©ã¼ã¸ã®ã·ã°ãã«ã§ããæåã®ã·ã³ã¿ãã¯ã¹ãã§ãã¯ãããæ¹æ³ãå¤æ´ãã¾ãããããã®è¡ã¯ã³ã³ãã¤ã«æã«å½±é¿ããå®è¡æã«ã¯ã¤ã³ã¿ã¼ããªã¿ãåºãããã¦ãå½±é¿ãåãã¾ããã</p> +<p class="original">The symbol <tt>#</tt> begins a comment. A comment lasts until the end of the line. Perl has no block comment syntax.</p> +<p>ã·ã³ãã«<tt>#</tt>ã¯ã³ã¡ã³ãã®éå§ã§ããã³ã¡ã³ãã¯è¡æ«ã§çµããã¾ããPerlã«ã¯ããããã¯ã³ã¡ã³ãã®ã·ã³ã¿ãã¯ã¹ã¯ããã¾ããã</p> + +<h2 class="original">Variables</h2> + +<h2>å¤æ°</h2> + +<p class=original>Perl variables come in three types: <i>scalars</i>, <i>arrays</i> and <i>hashes</i>. Each type has its own <i>sigil</i>: <tt>$</tt>, <tt>@</tt> and <tt>%</tt> respectively. Variables are declared using <tt>my</tt>.</p> + +<p>Perlã®å¤æ°ã«ã¯æ¬¡ã®3ã¿ã¤ãããã¾ã: <i>ã¹ã«ã©</i>ã<i>é å</i>ã <i>ããã·ã¥</i>ã§ã. ããããã®ã¿ã¤ãã«ã¯ãèªèº«ã®<i>ã·ã¸ã«(sigil)</i>ãããã¾ã: ããããã<tt>$</tt>, <tt>@</tt> and <tt>%</tt>ã å¤æ°ã¯<tt>my</tt>ã§å®£è¨ããã¾ãã</p> + +<h3 class=original>Scalar variables</h3> + +<h3>ã¹ã«ã©å¤æ°</h3> + +<p>A scalar variable can contain:</p> +<p class=original>ã¹ã«ã©å¤æ°ã¯ä»¥ä¸ã®ãã®ãæ ¼ç´ã§ãã¾ã:</p> +<ul class=original> + <li><tt>undef</tt> (corresponds to <tt>None</tt> in Python, <tt>null</tt> in PHP)</li> + <li>a number (Perl does not distinguish between an integer and a float)</li> + <li>a string</li> + <li>a reference to any other variable.</li> +</ul> +<ul class=original> + <li><tt>undef</tt> (Pythonã«ããã<tt>None</tt>ãPHPã®<tt>null</tt>ã«å¯¾å¿)</li> + <li>æ°å (Perlã¯integerã¨floatãåºå¥ãã¾ãã)</li> + <li>æå</li> + <li>ä»ã®å¤æ°ã¸ã®ãªãã¡ã¬ã³ã¹</li> +</ul> + +<pre class="perl"> +my $undef = undef; +print $undef; # warning + +# implicit undef: +my $undef2; +print $undef2; # exactly the same warning +</pre> + +<pre class="perl"> +my $num = 4040.5; +print $num; # "4040.5" +</pre> + +<pre class="perl"> +my $string = "world"; +print $string; # "world" +</pre> + +<p class=original>(References are coming up shortly.)</p> +<p>(ãªãã¡ã¬ã³ã¹ã¯å¾ã§èª¬æããã®ã§ç°¡åã«)</p> + +<p class=original>String concatenation using the <tt>.</tt> operator (same as PHP):</p> +<p>æååã®é£çµã«ã¯<tt>.</tt>æ¼ç®åã使ãã¾ã(PHPã¨åã):</p> +<pre class="perl"> +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> + +<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"> +my $str1 = "4G"; +my $str2 = "4H"; + +print $str1 . $str2; # "4G4H" +print $str1 + $str2; # "8" with two warnings +print $str1 eq $str2; # "" (empty string, i.e. false) +print $str1 == $str2; # "1" with NO WARNING! +</pre> + +<p class=original>The lesson is to always using the correct operator in the correct situation. There are separate operators for comparing scalars as numbers and comparing scalars as strings:</p> +<p>ãã®ã¬ãã¹ã³ã§ã¯ãæ£ããç¶æ³ã§æ£ããæ¼ç®åã使ãããã«ãã¦ãã¾ããæ°åã¨ãã¦ã¹ã«ã©ãæ¯è¼ããã®ã¨ãæååã¨ãã¦ã¹ã«ã©ãæ¯è¼ããã®ã¨ã§ã¯ãæ¼ç®åãåºå¥ãã¾ãã:</p> + +<pre class="perl original"> +# Numerical operators: <, >, <=, >=, ==, !=, <=> +# String operators: lt, gt, le, ge, eq, ne, cmp +</pre> + +<pre class="perl"> +# æ°åç¨ã®æ¼ç®å: <, >, <=, >=, ==, !=, <=> +# æåç¨ã®æ¼ç®å: lt, gt, le, ge, eq, ne, cmp +</pre> + +<p class=original><strong>Perl has no boolean data type.</strong> A scalar in an <tt>if</tt> statement evaluates to boolean "false" if and only if it is one of the following:</p> + +<p><strong>Perlã«ã¯ãã¼ãªã¢ã³ã®ãã¼ã¿åã¯ããã¾ããã</strong> <tt>if</tt>ã¹ãã¼ãã¡ã³ãã§ã¯ã¹ã«ã©ã¯ã以ä¸ã®ããããã®å ´åã«ã®ã¿"å½"ã¨ãã¦è©ä¾¡ããã¾ã:</p> + +<ul> + <li><tt>undef</tt></li> + <li>number <tt>0</tt></li> + <li>string <tt>""</tt></li> + <li>string <tt>"0"</tt>.</li> +</ul> + +<p>The Perl documentation <em>repeatedly</em> claims that functions return "true" or "false" values in certain situations. In practice, when a function is claimed to return "true" it usually returns <tt>1</tt>, and when it is claimed to return false it usually returns the empty string, <tt>""</tt>.</p> + +<p class=original>Perlã®ããã¥ã¡ã³ãã§ã¯ã<em>ç¹°ãè¿ã</em>ãç¹å®ã®ç¶æ³ã§"ç"ã"å½"ã®å¤ãé¢æ°ãè¿ãããã«ä¸»å¼µãã¦ãã¾ããå®éã«ã¯ã +é¢æ°ãçãè¿ãããã«ä¸»å¼µãã¦ããã¨ãã¯ãæ®é<tt>1</tt>ãè¿ãã¾ããã¾ããé¢æ°ãå½ãè¿ãããã«ä¸»å¼µãã¦ãããªããæ®éã¯ç©ºæå<tt>""</tt>ãè¿ãã¾ãã</p> + +<h3 class=original>Array variables</h3> +<h3>é åå¤æ°</h3> + +<p class=original>An array variable is a list of scalars indexed by integers beginning at 0. In Python this is known as a <i>list</i>, and in PHP this is known as an <i>array</i>.</p> +<p>é åå¤æ°ã¯æ´æ°ã®0ã§å§ã¾ãã¯ãã¾ãã¤ã³ããã¯ã¹ãããã¹ã«ã©ã§ããPythonã§ã¯ã<i>list</i>ã§ãPHPã§ã¯ã<i>array</i>ã§ãã</p> + +<pre class="perl original"> +my @array = ( + "print", + "these", + "strings", + "out", + "for", + "me", # trailing comma is okay +); +</pre> + +<pre class="perl"> +my @array = ( + "print", + "these", + "strings", + "out", + "for", + "me", # ã«ã³ããæå¾ã«ç¶ãã¦ãåé¡ããã¾ãã +); +</pre> + +<p class=original>You have to use a dollar sign to access a value from an array, because the value being <em>retrieved</em> is not an array but a scalar:</p> + +<p>é åããå¤ã«ã¢ã¯ã»ã¹ããã¨ãã«ã¯ãã«è¨å·ã使ããªããã°ããã¾ããã<em>åããã</em>å¤ã¯é åã§ã¯ãªããã¹ã«ã©ã ããã§ã:</p> + +<pre class="perl"> +print $array[0]; # "print" +print $array[1]; # "these" +print $array[2]; # "strings" +print $array[3]; # "out" +print $array[4]; # "for" +print $array[5]; # "me" +print $array[6]; # warning +</pre> + +<p class=original>You can use negative indices to retrieve entries starting from the end and working backwards:</p> +<p>è² ã®ã¤ã³ããã¯ã¹ããå¾ãããå¤ãåãã®ã«ä½¿ãã¾ããéåãã«ãªãã¾ã:</p> + +<pre class="perl"> +print $array[-1]; # "me" +print $array[-2]; # "for" +print $array[-3]; # "out" +print $array[-4]; # "strings" +print $array[-5]; # "these" +print $array[-6]; # "print" +print $array[-7]; # warning +</pre> + +<p class=original>There is no collision between a scalar <tt>$array</tt> and an array <tt>@array</tt> containing a scalar entry <tt>$array[0]</tt>. There may, however, be reader confusion, so avoid this.</p> + +<p>ã¹ã«ã©<tt>$array</tt>ã¨ã¹ã«ã©ã®ã¨ã³ããª<tt>$array[0]</tt>ãæã¤é å<tt>@array</tt>ã¯è¡çªãã¾ããã ã¨ã¯ãããã³ã¼ããèªã人ãæ··ä¹±ãã¾ãã®ã§ãåãååãä»ããã®ã¯é¿ãã¾ãããã</p> + +<p class=original>To get an array's length:</p> +<p>é åã®é·ããå¾ãã«ã¯:</p> + +<pre class="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> + +<p class=original>String concatenation using the <tt>.</tt> operator:</p> +<p>æååã®çµåã«ã¯<tt>.</tt>æ¼ç®åã使ãã¾ã:</p> + +<pre class="perl"> +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"> +print @array; # "printthesestringsoutforme" +</pre> + +<p class=original>The arguments with which the original Perl script was invoked are stored in the built-in array variable <tt>@ARGV</tt>.</p> +<p>ãªãªã¸ãã«ã®Perlã¹ã¯ãªããã®å®è¡æã®å¼æ°ã¯ãçµè¾¼ã®é åå¤æ°<tt><@ARGV/tt>ã«å ¥ãã¾ãã</p> + +<p class=original>Variables can be interpolated into strings:</p> +<p>å¤æ°ãæååã®éã«å ¥ãããã¨ãã§ãã¾ã:</p> +<pre class="perl"> +print "Hello $string"; # "Hello world" +print "@array"; # "print these strings out for me" +</pre> + +<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"> +print "Hello \$string"; # "Hello $string" +print 'Hello $string'; # "Hello $string" +print "\@array"; # "@array" +print '@array'; # "@array" +</pre> + +<h3 class=original>Hash variables</h3> +<h3>ããã·ã¥å¤æ°</h3> + +<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"> +my %scientists = ( + "Newton" => "Isaac", + "Einstein" => "Albert", + "Darwin" => "Charles", +); +</pre> + +<p class=original>Notice how similar this declaration is to an array declaration. In fact, the double arrow symbol <tt>=></tt> is called a "fat comma", because it is just a synonym for the comma separator. A hash is g declared using a list with an even number of elements, where the even-numbered elements (0, 2, ...) are all considered as strings.</p> +<p>ãã®å®£è¨ã¯é åå¤æ°ã®å®£è¨ã«ã¨ã¦ãä¼¼ã¦ãããã¨ã«æ³¨æãã¦ãã ãããå®éã«ãäºéç¢å°è¨å·<tt>=></tt>ã¯ã"ãã¡ããã«ã³ã"ã¨å¼ã°ãã¾ããã¨ããã®ããããã¯ãã«ã³ãã»ãã¬ã¼ã¿ã®ã¾ãã«ã·ããã ã ããã§ããããã·ã¥ã¯å¶æ°ã®æ°ã®ãªã¹ãã使ã£ã¦å®£è¨ããã¾ããå¶æ°ã®è¦ç´ (0, 2, ...)ã¯å ¨ã¦æååã¨èãããã¾ã</p> + +<p class=original>Once again, you have to use a dollar sign to access a value from a hash, because the value being <em>retrieved</em> is not a hash but a scalar:</p> +<p>ã¾ãã¾ããããã·ã¥ããå¤ã«ã¢ã¯ã»ã¹ããã«ã¯ããã«è¨å·ã使ããªããã°ããã¾ããã<em>åããã</em>å¤ã¯ããã·ã¥ã§ã¯ãªããã¹ã«ã©ã ããã§ã:</p> + +<pre class="perl original"> +print $scientists{"Newton"}; # "Isaac" +print $scientists{"Einstein"}; # "Albert" +print $scientists{"Darwin"}; # "Charles" +print $scientists{"Dyson"}; # runtime error - key not set +</pre> + +<pre class="perl"> +print $scientists{"Newton"}; # "Isaac" +print $scientists{"Einstein"}; # "Albert" +print $scientists{"Darwin"}; # "Charles" +print $scientists{"Dyson"}; # å®è¡æã¨ã©ã¼(訳註:è¦åã®ééã) - ãã¼ãã»ããããã¦ããªã +</pre> + +<p class=original>Note the braces used here. Again, there is no collision between a scalar <tt>$hash</tt> and a hash <tt>%hash</tt> containing a scalar entry <tt>$hash{"foo"}</tt>.</p> + +<p>ãã¬ã¼ã¹ã使ããã¦ãããã¨ã«æ³¨æãã¦ãã ãããé åã®ã¨ãã¨åãã§ãããç´ ããã®<tt>$hash</tt>ã¨ã¹ã«ã©ã®ã¨ã³ããª<tt>$hash{"foo"}</tt>ãå ¥ã£ã¦ããããã·ã¥ã®<tt>%hash</tt>ã¯è¡çªãã¾ããã</p> + +<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"> +my @scientists = %scientists; +</pre> + +<p class=original>However, unlike an array, the keys of a hash have <em>no underlying order</em>. They will be returned in whatever order is more efficient. So, notice the rearranged <em>order</em> but preserved <em>pairs</em> in the resulting array:</p> + +<p>ã§ãããé åã¨ã¯éããããã·ã¥ã®ãã¼ã¯<em>é çªãããã¾ãã</em>ãããå¹ççãªé çªã§è¿ã£ã¦ãã¾ãããã®ãããæ´åããç´ããã<em>é çª</em>ã«æ°ãã¤ãã¦ãã ãããããããçµæã®é åã®<em>ãã¢</em>ã¯ä¿æããã¾ã:</p> + +<pre class="perl"> +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"> +my $data = "orange"; +my @data = ("purple"); +my %data = ( "0" => "blue"); + +print $data; # "orange" +print $data[0]; # "purple" +print $data["0"]; # "purple" +print $data{0}; # "blue" +print $data{"0"}; # "blue" +</pre> + +<h3 class=original>Lists</h3> +<h3>ãªã¹ã</h3> + +<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"> +( + "print", + "these", + "strings", + "out", + "for", + "me", +) + +( + "Newton" => "Isaac", + "Einstein" => "Albert", + "Darwin" => "Charles", +) +</pre> + +<p class=original><strong>A list is not a variable.</strong> A list is an ephemeral <em>value</em> which can be <em>assigned</em> to an array or a hash variable. This is why the syntax for declaring array and hash variables is identical. There are many situations where the terms "list" and "array" can be used interchangeably, but there are equally many where lists and arrays display subtly different and extremely confusing behaviour.</p> +<p><strong>ãªã¹ãã¯å¤æ°ã§ã¯ããã¾ããã</strong>ãªã¹ãã¯ãã®å ´éãã®<em>å¤</em>ã§ãé åãããã·ã¥å¤æ°ã«<em>å²ãå½ã¦</em>ããã¾ãããã®ãã¨ã¯ãé åã¨ããã·ã¥ã®å¤æ°ã®å®£è¨ã®ã·ã³ã¿ãã¯ã¹ããä¸è´ãã¦ãããã¨ã®çç±ã§ãã"ãªã¹ã"ã¨"é å"ã®ç¨èªã交æå¯è½ãªç¶æ³ã¯å¤ãããã¾ãããåæ§ã«ããªã¹ãã¨é åãããã¶ãéã£ã¦è¦ããã¨ãããããããã®æ¯ãèãã«é常ã«æ··ä¹±ãã¾ãã</p> +<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"> +(0, 1, 2, 3, 4, 5) +(0 => 1, 2 => 3, 4 => 5) +</pre> + +<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> + +<p class=original>There aren't even hints here. This list could be used to declare an empty array or an empty hash and the <tt>perl</tt> interpreter clearly has no way of telling either way. Once you understand this odd aspect of Perl, you will also understand why the following fact must be true: <strong>List values cannot be nested.</strong> Try it:</p> + +<p>ãªãã®ãã³ããåå¨ãã¾ããããã®ãªã¹ãã¯ç©ºã®é åã¨ãã¦å®£è¨ããã¦ããã®ã§ãããããããã¨ããã«ã©ã®ããã·ã¥ã¨ãã¦ã§ããããã<tt>perl</tt>ã¤ã³ã¿ã¼ããªã¿ã«ã¯ãæããã«ã©ã¡ãã¨ãå¤æãããã¨ãã§ãã¾ãããPerlã®å¤ãã£ãä¸é¢ã§ããã¨ç解ãããªãã次ã®äºå®ãçã§ãããã¨ãã¾ãç解ããã§ããã: <strong>ãªã¹ãã®å¤ã¯ãã¹ãã§ãã¾ããã</strong> 試ãã¦ã¿ã¦ãã ãã:</p> + +<pre class="perl"> +my @array = ( + "apples", + "bananas", + ( + "inner", + "list", + "several", + "entries", + ), + "cherries", +); +</pre> + +<p class=original>Perl has no way of knowing whether <tt>("inner", "list", "several", "entries")</tt> is supposed to be an inner array or an inner hash. Therefore, Perl assumes that it is neither and <strong>flattens the list out into a single long list</strong>:</p> + +<p>Perlã«ã¯ã<tt>("inner"(訳註:å å´ã®), "list"(訳註:ãªã¹ãã), "several"(訳註:è¤æ°ã®), "entries"(訳註:å¤))</tt>ãå å ãããé åãªã®ããããã·ã¥ãªã®ãã©ããããããã¾ããããã®ãããPerlã¯ãããã§ããªãã¨æ³å®ãã<strong>ã²ã¨ã¤ã®é·ããªã¹ãã«å¹³æ¿åãã¾ã</strong>:</p> + +<pre> +print $array[0]; # "apples" +print $array[1]; # "bananas" +print $array[2]; # "inner" +print $array[3]; # "list" +print $array[4]; # "several" +print $array[5]; # "entries" +print $array[6]; # "cherries" + +print $array[2][0]; # error +print $array[2][1]; # error +print $array[2][2]; # error +print $array[2][3]; # error +</pre> + +<p class=original>The same is true whether the fat comma is used or not:</p> +<p>ãã¡ããã«ã³ãã使ã£ã¦ãã¦ãããªãã¦ãåããçã§ã:</p> + +<pre class="perl"> +my %hash = ( + "beer" => "good", + "bananas" => ( + "green" => "wait", + "yellow" => "eat", + ), +); + +# The above raises a warning because the hash was declared using a 7-element list + +print $hash{"beer"}; # "good" +print $hash{"bananas"}; # "green" +print $hash{"wait"}; # "yellow"; +print $hash{"eat"}; # undef, so raises a warning + +print $hash{"bananas"}{"green"}; # error +print $hash{"bananas"}{"yellow"}; # error +</pre> + +<p class=original>More on this shortly.</p> +<p>More on this shortly.</p> + +<h2>ã³ã³ããã¹ã</h2> + +<p class=original>Perl's most distinctive feature is that its code is <i>context-sensitive</i>. <strong>Every expression in Perl is evaluated either in scalar context or list context</strong>, depending on whether it is expected to produce a scalar or a list. Many Perl expressions and built-in functions display radically different behaviour depending on the context in which they are evaluated.</p> + +<p>Perlã®æãç¹å¾´ã®ããã¨ããã¯ãã³ã¼ãã<i>ã³ã³ãã¯ã¹ãã»ã³ã·ãã£ã</i>ãªã¨ããã§ãã<strong>Perlã®å ¨ã¦ã®å¼ã¯ã¹ã«ã©ã³ã³ããã¹ãããªã¹ãã³ã³ããã¹ãã§è©ä¾¡ããã¾ã</strong>ãã¹ã«ã©ããªã¹ããã®ãããããä½ãããæå¾ ããã¦ãããã«ä¾åãã¾ããå¤ãã®Perlã®å¼ã¨çµè¾¼ã®é¢æ°ã¯ããããè©ä¾¡ãããã³ã³ããã¹ãã«ãã£ã¦ãæ¯ãèãã«æ ¹æ¬çã«éããè¦ãã¾ãã</p> + +<p class=original>A scalar declaration such as <tt>my $scalar =</tt> evaluates its expression in scalar context. A scalar value such as <tt>"Mendeleev"</tt> evaluated in scalar context returns the scalar:</p> + +<p><tt>my $scalar =</tt>ã®ãããªã¹ã«ã©ã®å®£è¨ã¯ã¹ã«ã©ã³ã³ããã¹ãã¨ãã¦è©ä¾¡ããã¾ãã<tt>"Mendeleev"</tt>ã®ãããªã¹ã«ã©ã®å¤ãã¹ã«ã©ä»æµãã¨ã§è©ä¾¡ãããã¹ã«ã©ãè¿ãã¾ã:</p> + +<pre class="perl"> +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"> +my @array = ("Alpha", "Beta", "Gamma", "Pie"); +my %hash = ("Alpha" => "Beta", "Gamma" => "Pie"); +</pre> + +<p class=original>No surprises so far.</p> +<p>é©ããããªãã¨ã¯ããã¾ããã</p> + +<p class=original>A scalar expression evaluated in list context turns into a single-element list:</p> +<p>ã¹ã«ã©ã®å¼ã¯ãªã¹ãã³ã³ããã¹ãã§è©ä¾¡ãããã¨ãã²ã¨ã¤ã®å¤ã®ãªã¹ãã¨ãªãã¾ã:</p> + +<pre class="perl"> +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"> +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"> +my @array = ("Alpha", "Beta", "Gamma", "Pie"); +my $scalar = @array; +print $scalar; # "4" +</pre> +<p class=original>You can force any expression to be evaluated in scalar context using the <tt>scalar</tt> built-in function. In fact, this is why we use <tt>scalar</tt> to retrieve the length of an array.</p> +<p>ã©ã®ãããªå¼ã<tt>scalar</tt>çµè¾¼é¢æ°ã使ãã°ã¹ã«ã©ã³ã³ããã¹ãã§å¼·å¶çã«è©ä¾¡ãããã¨ãåºæ¥ã¾ãããã®ãã¨ãããªã<tt>scalar</tt>ãé åã®é·ããåãã®ã«ä½¿ãã®ãã®çç±ã§ãã</p> + +<p class=original>You are not bound by law or syntax to return a scalar value when a subroutine is evaluated in scalar context, nor to return a list value in list context. As seen above, Perl is perfectly capable of fudging the result for you.</p> +<p>ãµãã«ã¼ãã³ãã¹ã«ã©ã³ã³ããã¹ãã§è©ä¾¡ãããã¨ãã«ã¹ã«ã©ãè¿ããããªæ³å¾ãã·ã³ã¿ãã¯ã¹ã«ã¯ããªãã¯ç¸ããã¦ãã¾ããããã¾ãããªã¹ãã³ã³ããã¹ãã§ãªã¹ããè¿ããã¨ãã¾ãåãã§ããä¸ã§è¦ãããã«ãPerlã¯å®å ¨ã«ããªãã®ããã«çµæãä½ããã¨ãã§ãã¾ãã</p> + +<!-- +<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"> +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"> +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" +</pre> +--> + +<h2 class=original>References and nested data structures</h2> +<h2>ãªãã¡ã¬ã³ã¹ã¨ãã¹ãããããã¼ã¿æ§é </h2> + +<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"> +my @outer = (); +my @inner = ("Mercury", "Venus", "Earth"); + +$outer[0] = @inner; + +print $outer[0]; # "3", not "MercuryVenusEarth" as you would hope +print $outer[0][0]; # error, not "Mercury" as you would hope +</pre> + +<p class=original><tt>$outer[0]</tt> is a scalar, so it demands a scalar value. When you try to assign an array value like <tt>@inner</tt> to it, <tt>@inner</tt> is evaluated in scalar context. This is the same as assigning <tt>scalar @inner</tt>, which is the length of array <tt>@inner</tt>, which is 3.</p> +<p><tt>$outer[0]</tt>ã¯ã¹ã«ã©ã§ãããã®ãããã¹ã«ã©ã®å¤ãè¦æ±ãã¾ãã<tt>@inner</tt>ã®ãããªé åãå²ãå½ã¦ããã¨ããã¨ã<tt>@inner</tt>ã¯ã¹ã«ã©ã³ã³ããã¹ãã§è©ä¾¡ããã¾ããã¤ã¾ãã<tt>scalar @inner</tt>ã¨åãã§ããé å<tt>@inner</tt>ã®é·ãã3ã«ãªãã¾ãã</p> + +<p class=original>However, a scalar variable may contain a <i>reference</i> to any variable, including an array variable or a hash variable. This is how more complicated data structures are created in Perl.</p> +<p>ã§ãã, ã¹ã«ã©å¤æ°ã¯ãé åå¤æ°ãããã·ã¥å¤æ°ãå«ããä»ã®å¤æ°ã¸ã®<i>ãªãã¡ã¬ã³ã¹</i>ãæã¦ã¾ããããããPerlã§ããè¤éãªãã¼ã¿æ§é ãä½ãæ¹æ³ã§ãã</p> + +<p class=original>A reference is created using a backslash.</p> +<p>ãªãã¡ã¬ã³ã¹ã¯ããã¯ã¹ã©ãã·ã¥ã使ã£ã¦ä½ããã¾ãã/p> +<pre class="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"> +print $colour; # "Indigo" +print $scalarRef; # e.g. "SCALAR(0x182c180)" +print ${ $scalarRef }; # "Indigo" +</pre> + +<p class=original>As long as the result is not ambiguous, you can omit the braces too:</p> +<p>çµæãææ§ã§ãªãéãããã¬ã¼ã¹ãçç¥ãããã¨ãã§ãã¾ã:</p> + +<pre class="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, <tt>-></tt>:</p> +<p>ãªãã¡ã¬ã³ã¹ãé åãããã·ã¥å¤æ°ã®ãªãã¡ã¬ã³ã¹ã®å ´åããã¬ã¼ã¹ãããä¸è¬çãªã¢ãã¼æ¼ç®åã<tt>-></tt>ã使ã£ã¦ãã¼ã¿ãåãåºãã¾ãã:</p> + +<pre class="perl"> +my @colours = ("Red", "Orange", "Yellow", "Green", "Blue"); +my $arrayRef = \@colours; + +print $colours[0]; # direct array access +print ${ $arrayRef }[0]; # use the reference to get to the array +print $arrayRef->[0]; # exactly the same thing + +my %atomicWeights = ("Hydrogen" => 1.008, "Helium" => 4.003, "Manganese" => 54.94); +my $hashRef = \%atomicWeights; + +print $atomicWeights{"Helium"}; # direct hash access +print ${ $hashRef }{"Helium"}; # use a reference to get to the hash +print $hashRef->{"Helium"}; # exactly the same thing - this is very common +</pre> + +<h3 class=original>Declaring a data structure</h3> +<h3>ãã¼ã¿æ§é ã宣è¨ãã</h3> + +<pre class="perl"> +my %owner1 = ( + "name" => "Santa Claus", + "DOB" => "1882-12-25", +); + +my $owner1Ref = \%owner1; + +my %owner2 = ( + "name" => "Mickey Mouse", + "DOB" => "1928-11-18", +); + +my $owner2Ref = \%owner2; + +my @owners = ( $owner1Ref, $owner2Ref ); + +my $ownersRef = \@owners; + +my %account = ( + "number" => "12345678", + "opened" => "2000-01-01", + "owners" => $ownersRef, +); +</pre> + +<p class=original>That's obviously unnecessarily laborious, because you can shorten it to:</p> +<p>ããã¯ãæããã«ä¸å¿ è¦ã§éª¨ã®æãã¾ãããªããªãã次ã®ããã«çç¥ã§ãã¾ã:</p> + +<pre class="perl"> +my %owner1 = ( + "name" => "Santa Claus", + "DOB" => "1882-12-25", +); + +my %owner2 = ( + "name" => "Mickey Mouse", + "DOB" => "1928-11-18", +); + +my @owners = ( \%owner1, \%owner2 ); + +my %account = ( + "number" => "12345678", + "opened" => "2000-01-01", + "owners" => \@owners, +); +</pre> + +<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 <tt>%account</tt> as above:</p> +<p> +å¥ã®è¨å·ã使ã£ã¦<i>ç¡å</i>é åãããã·ã¥ã宣è¨ãããã¨ãåºæ¥ã¾ããåè§ããã©ã±ãããç¡åé åã«ããã¬ã¼ã¹ãç¡åããã·ã¥ã«ä½¿ãã¾ãããããããè¿ãããå¤ã¯ãç¡åã®ãã¼ã¿æ§é ã®<em>ãªãã¡ã¬ã³ã¹</em>ã«ãªãã¾ãã注æãã¦è¦ã¦ãã ããã次ã®çµæã¯ãä¸ã®<tt>%account</tt>ã¨å ¨ãåãã§ã:</p> + +<pre class="perl"> +# Braces denote an anonymous hash +my $owner1Ref = { + "name" => "Santa Claus", + "DOB" => "1882-12-25", +}; + +my $owner2Ref = { + "name" => "Mickey Mouse", + "DOB" => "1928-11-18", +}; + +# Square brackets denote an anonymous array +my $ownersRef = [ $owner1Ref, $owner2Ref ]; + +my %account = ( + "number" => "12345678", + "opened" => "2000-01-01", + "owners" => $ownersRef, +); +</pre> + +<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"> +my %account = ( + "number" => "31415926", + "opened" => "3000-01-01", + "owners" => [ + { + "name" => "Philip Fry", + "DOB" => "1974-08-06", + }, + { + "name" => "Hubert Farnsworth", + "DOB" => "2841-04-09", + }, + ], +); +</pre> + +<h3 class="original">Getting information out of a data structure</h3> +<h3>ãã¼ã¿æ§é ããæ å ±ãåãåºã</h3> + +<p class="original">Now, let's assume that you still have <tt>%account</tt> kicking around but everything else (if there was anything else) has fallen out of scope. You can print the information out by reversing the same procedure in each case:</p> + +<p>ãã¦ãããããã¾ãã<tt>%account</tt>ãã¾ã ããã¨ãã¾ããããã§ãããå ¨ã¦ã®ä»ã®ãã®(ä»ã®ãã®ããã£ããªã)ã¯ãã¹ã³ã¼ãã®å¤ã«è½ã¡ã¾ããããããã®ã±ã¼ã¹ã§åãæé ãéåãã«ãããã¨ã§ãæ å ±ã表示ã§ãã¾ãã</p> + +<pre class="perl"> +my $ownersRef = $account{"owners"}; +my @owners = @{ $ownersRef }; +my $owner1Ref = $owners[0]; +my %owner1 = %{ $owner1Ref }; +my $owner2Ref = $owners[1]; +my %owner2 = %{ $owner2Ref }; +print "Account #", $account{"number"}, "\n"; +print "Opened on ", $account{"opened"}, "\n"; +print "Joint owners:\n"; +print "\t", $owner1{"name"}, " (born ", $owner1{"DOB"}, ")\n"; +print "\t", $owner2{"name"}, " (born ", $owner2{"DOB"}, ")\n"; +</pre> + +<p class=original>Or, for short:</p> +<p>ã¾ãã¯, çç¥ãã¦:</p> + +<pre class="perl"> +my @owners = @{ $account{"owners"} }; +my %owner1 = %{ $owners[0] }; +my %owner2 = %{ $owners[1] }; +print "Account #", $account{"number"}, "\n"; +print "Opened on ", $account{"opened"}, "\n"; +print "Joint owners:\n"; +print "\t", $owner1{"name"}, " (born ", $owner1{"DOB"}, ")\n"; +print "\t", $owner2{"name"}, " (born ", $owner2{"DOB"}, ")\n"; +</pre> + +<p class=original>Or using references:</p> +<p>ã¾ãã¯ããªãã¡ã¬ã³ã¹ã使ã£ã¦:</p> + +<pre class="perl"> +my $ownersRef = $account{"owners"}; +my $owner1Ref = $ownersRef->[0]; +my $owner2Ref = $ownersRef->[1]; +print "Account #", $account{"number"}, "\n"; +print "Opened on ", $account{"opened"}, "\n"; +print "Joint owners:\n"; +print "\t", $owner1Ref->{"name"}, " (born ", $owner1Ref->{"DOB"}, ")\n"; +print "\t", $owner2Ref->{"name"}, " (born ", $owner2Ref->{"DOB"}, ")\n"; +</pre> + +<p class=original>And if we completely skip all the intermediate values:</p> +<p>ããã¦ãå ¨ã¦ã®ä¸éã®å¤ãã¹ããããããªã:</p> + +<pre class="perl"> +print "Account #", $account{"number"}, "\n"; +print "Opened on ", $account{"opened"}, "\n"; +print "Joint owners:\n"; +print "\t", $account{"owners"}->[0]->{"name"}, " (born ", $account{"owners"}->[0]->{"DOB"}, ")\n"; +print "\t", $account{"owners"}->[1]->{"name"}, " (born ", $account{"owners"}->[1]->{"DOB"}, ")\n"; +</pre> + +<h3 class=original>How to shoot yourself in the foot with references to arrays and hashes</h3> +<h3>é åã¨ããã·ã¥ã®ãªãã¡ã¬ã³ã¹ã§èªåã§èªåã®è¶³ãæã¤æ¹æ³</h3> + +<p class=original>This array has five elements:</p> +<p>ãã®é åã«ã¯5ã¤ã®è¦ç´ ãããã¾ã:</p> +<pre class="perl"> +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"> +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"> +my $array3Ref = [1, 2, 3, 4, 5]; +print $array3Ref; # e.g. "ARRAY(0x22710c0)" +print @{ $array3Ref }; # "12345" +print @$array3Ref; # "12345" +</pre> + +<h2 class=original>Flow control</h2> +<h2>ããã¼å¶å¾¡</h2> + +<h3><tt>if</tt> ... <tt>elsif</tt> ... <tt>else</tt> ...</h3> + +<p class=original>No surprises here, other than the spelling of <tt>elsif</tt>:</p> +<p><tt>elsif</tt>ã®ã¹ãã«ä»¥å¤ã«ã¯é©ããã®ã¯ããã¾ãã:</p> + +<pre class="perl"> +my $word = "antidisestablishmentarianism"; +my $strlen = length $word; + +if($strlen >= 15) { + print "'", $word, "' is a very long word"; +} elsif(10 <= $strlen && $strlen < 15) { + print "'", $word, "' is a medium-length word"; +} else { + print "'", $word, "' is a a short word"; +} +</pre> + +<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"> +print "'", $word, "' is actually enormous" if $strlen >= 20; +</pre> + +<h3><tt>unless</tt> ... <tt>else</tt> ...</h3> + +<pre class="perl"> +my $temperature = 20; + +unless($temperature > 30) { + print $temperature, " degrees Celsius is not very hot"; +} else { + print $temperature, " degrees Celsius is actually pretty hot"; +} +</pre> + +<p class=original><tt>unless</tt> blocks are generally best avoided like the plague because they are very confusing. An "<tt>unless</tt> [... <tt>else</tt>]" block can be trivially refactored into an "<tt>if</tt> [... <tt>else</tt>]" block by negating the condition [or by keeping the condition and swapping the blocks]. Mercifully, there is no <tt>elsunless</tt> keyword.</p> + +<p><tt>unless</tt>ãããã¯ã¯ç½ãã®ããã«é¿ããã®ãä¸çªã§ãããããã¯æ··ä¹±ã®ãã¨ã¨ãªãã¾ãã"<tt>unless</tt> [... <tt>else</tt>]"ãããã¯ã¯ç°¡åã«ã"<tt>if</tt> [... <tt>else</tt>]" ãããã¯ã«ãæ¡ä»¶ãå¦å®ãã[ã¾ãã¯ãæ¡ä»¶ããã®ã¾ã¾ã«ãããã¯ã交æãã]ãã¨ã§ãªãã¡ã¯ã¿ã§ãã¾ãã幸ãã <tt>elsunless</tt>ã®ãã¼ã¯ã¼ãã¯ããã¾ããã</p> + +<p class=original>This, by comparison, is highly recommended because it is so easy to read:</p> +<p>ä¸æ¹ã§ã以ä¸ã¯èªã¿ãããã®ããã«ãå¼·ãæ¨å¥¨ããã¾ã</p> +<pre class="perl"> +print "Oh no it's too cold" unless $temperature > 15; +</pre> + +<h3 class=original>Ternary operator</h3> +<h3>ä¸é æ¼ç®å</h3> + +<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"> +my $gain = 48; +print "You gained ", $gain, " ", ($gain == 1 ? "experience point" : "experience points"), "!"; +</pre> + +<p class=original>Aside: singulars and plurals are best spelled out in full in both cases. Don't do something clever like the following, because anybody searching the codebase to replace the words "tooth" or "teeth" will never find this line:</p> +<p>ä½è«: 両æ¹ã®ã±ã¼ã¹ã®åæ°ã¨è¤æ°ãå®å ¨ã«æ¸ãåºããã¦ãã¾ãã決ãã¦ä»¥ä¸ã®ãããªå·§å¦ãªãã¨ãããªãã§ãã ããã +ã³ã¼ããæ¤ç´¢ãã¦ã"tooth"ã"teeth"ã®åèªãç½®ãæãããã¨ãã¦ãããã®è¡ããè¦ã¤ãããã¨ãã§ãã¾ããã</p> + +<pre class="perl"> +my $lost = 1; +print "You lost ", $lost, " t", ($lost == 1 ? "oo" : "ee"), "th!"; +</pre> + +<p class=original>Ternary operators may be nested:</p> +<p>ä¸é æ¼ç®åã¯ãã¹ãã§ãã¾ã:</p> + +<pre class="perl"> +my $eggs = 5; +print "You have ", $eggs == 0 ? "no eggs" : + $eggs == 1 ? "an egg" : + "some eggs"; +</pre> + +<p class=original><tt>if</tt>, <tt>unless</tt> and <tt>?:</tt> statements evaluate their conditions in scalar context. For example, <tt>if(@array)</tt> returns true if and only if <tt>@array</tt> has 1 or more elements. It doesn't matter what those elements are - they may contain <tt>undef</tt> or other false values for all we care.</p> + +<p><tt>if</tt>ã <tt>unless</tt> 㨠<tt>?:</tt> ã¹ãã¼ãã¡ã³ãã¯æ¡ä»¶ãã¹ã«ã©ã³ã³ããã¹ãã§è©ä¾¡ãã¾ãã <tt>if(@array)</tt>ã¯ã<tt>@array</tt>ã«ã²ã¨ã¤ä»¥ä¸ã®ã¨ã¬ã¡ã³ããããå ´åã®ã¿ãçã¨ãªãã¾ããé åã®ä¸ã®å¤ãä½ãã¯åé¡ã«ãã¾ãã - å ¨ã¦ã<tt>undef</tt>ãä»ã®å½ã®å¤ã§ã</p> + +<h3 class=original>Array iteration</h3> +<h3>é åã®ã¤ãã¬ã¼ã·ã§ã³</h3> + +<p class=original>There's More Than One Way To Do It.</p> +<p>ããããã¯ã²ã¨ã¤ã§ã¯ããã¾ããã</p> + +<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"> +for(my $i = 0; $i < scalar @array; $i++) { + print $i, ": ", $array[$i]; +} +</pre> + +<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"> +foreach my $string ( @array ) { + print $string; +} +</pre> + +<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"> +foreach my $i ( 0 .. $#array ) { + print $i, ": ", $array[$i]; +} +</pre> + +<p class=original>If you don't provide an explicit iterator, Perl uses a default iterator, <tt>$_</tt>. <tt>$_</tt> is the first and friendliest of the built-in variables:</p> +<p>æ示çãªã¤ãã¬ã¼ã¿ã使ããªããã°ãPerlã¯ããã©ã«ãã®ã¤ãã¬ã¼ã¿ã¨ãã¦<tt>$_</tt>ã使ãã¾ãã<tt>$_</tt>㯠+ +If you don't provide an explicit iterator, Perl uses a default iterator, . <tt>$_</tt>ã¯æåã®ãã¬ã³ããªã¼ãªçµè¾¼ã®å¤æ°ã§ã:</p> + +<pre class="perl"> +foreach ( @array ) { + print $_; +} +</pre> + +<p class=original>If using the default iterator, and you only wish to put a single statement inside your loop, you can use the super-short loop syntax:</p> +<p>ããã©ã«ãã®ã¤ãã¬ã¼ã¿ã使ããã¾ããã«ã¼ãã«åä¸ã®ã¹ãã¼ãã¡ã³ãããç½®ããªããªããã¨ã¦ãçãã«ã¼ãã®ã·ã³ã¿ãã¯ã¹ã使ãã¾ã:</p> +<pre class="perl"> +print $_ foreach @array; +</pre> + +<p class=original>Perl also provides <tt>while</tt> loops but those are coming up in a second.</p> +<p>Perlã«ã¯ãã¾ãã<tt>while</tt> ã«ã¼ããããã¾ãã ã§ããããããã¯ã¾ããå¾ã§ã</p> + +<h3 class=original>Hash iteration</h3> +<h3>ããã·ã¥ã¤ãã¬ã¼ã¿</h3> + +<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"> +foreach my $key (keys %scientists) { + print $key, ": ", $scientists{$key}; +} +</pre> + +<p>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> +<pre class="perl"> +foreach my $key (sort keys %scientists) { + print $key, ": ", $scientists{$key}; +} +</pre> + +<p>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 class=original>ç¹å¥ãªçµè¾¼é¢æ°<tt>each</tt>ããããããã¯ããã¼/å¤ã®ãã¢ãä¸åº¦ã«ã²ã¨ã¤åãåºãã¾ãã<tt>each</tt>ãå¼ã°ãããã³ã«ãé åã®æå¾ã«è³ãã¾ã§ãå½ã®å¤ãæ»ãã¾ã§ã2ã¤ã®å¤ããã¤é åãè¿ãã¾ããé åã®å¤ã2ã¤ã®ã¹ã«ã©ã«ãåæã«å²ãå½ã¦ã¾ã:</p> + +<pre class="perl"> +while( my ($key, $value) = each %scientists ) { + print $key, ": ", $value; +} +</pre> + +<h3 class=original>Loop control</h3> +<h3>ã«ã¼ãå¶å¾¡</h3> + +<p class=original><tt>next</tt> and <tt>last</tt> can be used to control the progress of a loop. In most programming languages these are known as <tt>continue</tt> and <tt>break</tt> respectively. We can also optionally provide a label for any loop. By convention, labels are written in <tt>ALLCAPITALS</tt>. Having labelled the loop, <tt>next</tt> and <tt>last</tt> may target that label. This example lists all the non-fictional animals from an array:</p> + +<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"> +my @input = ( + "dragon", "camel", "cow", "pangolin", "unicorn", + "pig", "sheep", "donkey", "pig", "basilisk", + "monkey", "jellyfish", "squid", "crab", "dragon", +); +my @fictional = ("basilisk", "dragon", "unicorn"); + +INPUT: foreach my $input ( @input ) { + + # See if this input animal is fictional + foreach my $fictional ( @fictional ) { + + # It is? + if($input eq $fictional) { + + # Then jump to the next input animal + next INPUT; + } + } + + # Animal is not fictional, print it + print $input; +} +</pre> + +<h2 class=original>Array functions</h2> +<h2>é åé¢æ°</h2> + +<h3 class=original>In-place array modification</h3> +<h3>ãã®å ´ã§é åãå¤æ´ãã</h3> + +<p>We'll use <tt>@stack</tt> to demonstrate these:</p> + +<pre class="perl"> +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"> +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"> +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"> +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"> +unshift @stack, "Hank", "Grace"; +print @stack; # "HankGraceEileenDeniseBobAlice" +</pre> + +<p><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 class=original><tt>pop</tt>ã<tt>push</tt>ã <tt>shift</tt>ã<tt>unshift</tt> ã¯ãå ¨ã¦ã<tt>splice</tt>ã®ç¹å¥ãªã±ã¼ã¹ã§ãã<tt>splice</tt> ã¯ãé åã®ã¹ã©ã¤ã¹ãåé¤ãã¦ãè¿ãã¾ããå¥ã®é åã¹ã©ã¤ã¹ã§ãããç½®ãæãã¾ã:</p> +<pre class="perl"> +print splice(@stack, 1, 4, "<<<", ">>>"); # "GraceEileenDeniseBob" +print @stack; # "Hank<<<>>>Alice" +</pre> + +<h3 class=original>Creating new arrays from old</h3> +<h3>å¤ãé åããæ°ããé åãä½ã</h3> + +<p class=original>Perl provides the following functions which act on arrays to create other arrays.</p> +<p>Perlã«ã¯ä»¥ä¸ã®é¢æ°ããããé åã«å¯¾ãã¦ãæ°ããé åãä½ãã¾ã</p> + +<h4><tt>join</tt></h4> + +<p class="original">The <tt>join</tt> function concatenates many strings into one:</p> +<p><tt>join</tt> é¢æ°ã¯å¤ãã®æååãä¸ã¤ã«çµåãã¾ã:</p> + +<pre class="perl"> +my @elements = ("Antimony", "Arsenic", "Aluminum", "Selenium"); +print @elements; # "AntimonyArsenicAluminumSelenium" +print "@elements"; # "Antimony Arsenic Aluminum Selenium" +print join(", ", @elements); # "Antimony, Arsenic, Aluminum, Selenium" +</pre> + +<h4><tt>reverse</tt></h4> + +<p class=original>The <tt>reverse</tt> function returns an array in reverse order:</p> +<p><tt>reverse</tt> é¢æ°ã¯é çªãéã«ããé åãè¿ãã¾ã:</p> + +<pre class="perl"> +my @numbers = ("one", "two", "three"); +print reverse(@numbers); # "threetwoone" +</pre> + +<h4><tt>map</tt></h4> + +<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"> +my @capitals = ("Baton Rouge", "Indianapolis", "Columbus", "Montgomery", "Helena", "Denver", "Boise"); + +print join ", ", map { uc $_ } @capitals; +# "BATON ROUGE, INDIANAPOLIS, COLUMBUS, MONTGOMERY, HELENA, DENVER, BOISE" +</pre> + +<h4><tt>grep</tt></h4> + +<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"> +print join ", ", grep { length $_ == 6 } @capitals; +# "Helena, Denver" +</pre> + +<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"> +print join ", ", grep m/^[B-H]/, @capitals; +# "Baton Rouge, Columbus, Helena, Denver, Boise" +</pre> + +<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"> +print scalar grep { $_ eq "Columbus" } @capitals; # "1" +</pre> + +<p class=original><tt>grep</tt> and <tt>map</tt> may be combined to form <i>list comprehensions</i>, an exceptionally powerful feature conspicuously absent from many other programming languages.</p> +<p><tt>grep</tt> 㨠<tt>map</tt> ã¯ã<i>ãªã¹ãå å«</i>ã®å½¢ã«å«ã¾ãã¾ããä»ã®å¤ãã®ããã°ã©ãã³ã°è¨èªã«ç®ç«ã£ã¦æ¬ ãã¦ãããä¾å¤çã«å¼·åãªæ©è½ã§ãã</p> + +<h4><tt>sort</tt></h4> + +<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"> +my @elevations = (19, 1, 2, 100, 3, 98, 100, 1056); + +print join ", ", sort @elevations; +# "1, 100, 100, 1056, 19, 2, 3, 98" +</pre> + +<p class=original>However, similar to <tt>grep</tt> and <tt>map</tt>, you may supply some code of your own. Sorting is always performed using a series of comparisons between elements. Your block receives <tt>$a</tt> and <tt>$b</tt> as inputs and should return -1 if <tt>$a</tt> is "less than" <tt>$b</tt>, 0 if they are "equal" or 1 if <tt>$a</tt> is "greater than" <tt>$b</tt>.</p> +<p>ãããã<tt>grep</tt> 㨠<tt>map</tt>ã¨ä¼¼ã¦ãèªåã®ã³ã¼ãã渡ããã¨ãåºæ¥ã¾ããã½ã¼ãã¯å¸¸ã«è¦ç´ éã®ä¸é£ã®æ¯è¼ã使ã£ã¦ããã¾ããã³ã¼ããããã¯ã¯ã<tt>$a</tt>ã¨<tt>$b</tt>ãåãã<tt>$a</tt>ã<tt>$b</tt>"ããå°ã"ããã° -1ã"åã"ã§ããã° 0ã<tt>$a</tt>ã<tt>$b</tt>"ãã大ã"ããã° 1 ãè¿ãã¾ãã</p> + +<p class=original>The <tt>cmp</tt> operator does exactly this for strings:</p> +<p><tt>cmp</tt> æ¼ç®åã¯æååã«å¯¾ãã¦ãã¾ãã«ããããã¾ã:</p> + +<pre class="perl"> +print join ", ", sort { $a cmp $b } @elevations; +# "1, 100, 100, 1056, 19, 2, 3, 98" +</pre> + +<p class=original>The "spaceship operator", <tt><=></tt>, does the same for numbers:</p> +<p>"ã¹ãã¼ã¹ã·ããæ¼ç®å", <tt><=></tt>ã¯ãæ°åã«å¯¾ãã¦åããã¨ããã¾ã:</p> + +<pre class="perl"> +print join ", ", sort { $a <=> $b } @elevations; +# "1, 2, 3, 19, 98, 100, 100, 1056" +</pre> + +<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"> +sub comparator { + # lots of code... + # return -1, 0 or 1 +} + +print join ", ", sort comparator @elevations; +</pre> + +<p class=original>You can't do this for <tt>grep</tt> or <tt>map</tt> operations.</p> +<p>åããã¨ã¯ã<tt>grep</tt> ã <tt>map</tt> ã§ã¯ã§ãã¾ããã</p> + +<p class=original>Notice how the subroutine and block are never explicitly provided with <tt>$a</tt> and <tt>$b</tt>. Like <tt>$_</tt>, <tt>$a</tt> and <tt>$b</tt> are, in fact, global variables which are <em>populated</em> with a pair of values to be compared each time.</p> +<p>ãµãã«ã¼ãã³ã¨ãããã¯ã¯ <tt>$a</tt> 㨠<tt>$b</tt>ã¨ä¸ç·ã«ã¯æ¸¡ããã¾ããã<tt>$_</tt>ã®ããã«ã <tt>$a</tt>ã¨<tt>$b</tt>ã¯ãå®éãã°ãã¼ãã«å¤æ°ã§ããã®æã ã«ãæ¯è¼ãããå¤ã®ãã¢ã§<em>åããã</em>ã¾ã</p> + +<h2 class=original>Built-in functions</h2> +<h2>çµè¾¼é¢æ°</h2> +<p>By now you have seen at least a dozen built-in functions: <tt>print</tt>, <tt>sort</tt>, <tt>map</tt>, <tt>grep</tt>, <tt>each</tt>, <tt>keys</tt>, <tt>scalar</tt> and so on. Built-in functions are one of Perl's greatest strengths. They</p> +<ul> + <li>are numerous</li> + <li>are very useful</li> + <li>are extensively documented (in "perlfunc")</li> + <li>vary greatly in syntax, so check the documentation</li> + <li>sometimes accept regular expressions as arguments</li> + <li>sometimes accept entire blocks of code as arguments</li> + <li>sometimes don't require commas between arguments</li> + <li>sometimes will consume an arbitrary number of comma-separated arguments and sometimes will not</li> + <li>sometimes will fill in their own arguments if too few are supplied</li> + <li>generally don't require brackets around their arguments except in ambiguous circumstances</li> +</ul> +<p>The best advice regarding built-in functions is to know that they exist, so that you can <strong>use them</strong>. If you are carrying out a task which feels like it's low-level and common enough that it's been done many times before, the chances are that it has.</p> + +<h2>User-defined subroutines</h2> + +<p>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>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>Once you're inside a subroutine, the arguments are available using the built-in array variable <tt>@_</tt>. Examples follow.</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"> +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"> +sub leftPad { + my $newString = ($_[2] x ($_[1] - length $_[0])) . $_[0]; + return $newString; +} +</pre> + </li> + <li> + <p>Unpacking <tt>@_</tt> is only slightly less strongly discouraged:</p> +<pre class="perl"> +sub leftPad { + my $oldString = $_[0]; + my $width = $_[1]; + my $padChar = $_[2]; + my $newString = ($padChar x ($width - length $oldString)) . $oldString; + return $newString; +} +</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"> +sub leftPad { + my $oldString = shift @_; + my $width = shift @_; + my $padChar = shift @_; + my $newString = ($padChar x ($width - length $oldString)) . $oldString; + 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"> +sub leftPad { + my $oldString = shift; + my $width = shift; + my $padChar = shift; + my $newString = ($padChar x ($width - length $oldString)) . $oldString; + return $newString; +} +</pre> + <p>Beyond 4 arguments it becomes hard to keep track of what is being assigned where.</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"> +sub leftPad { + my ($oldString, $width, $padChar) = @_; + my $newString = ($padChar x ($width - length $oldString)) . $oldString; + return $newString; +} +</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"> +print leftPad("oldString" => "pod", "width" => 10, "padChar" => "+"); +</pre> + <p>And the subroutine itself looks like this:</p> +<pre class="perl"> +sub leftPad { + my %args = @_; + my $newString = ($args{"padChar"} x ($args{"width"} - length $args{"oldString"})) . $args{"oldString"}; + return $newString; +} +</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"> +sub contextualSubroutine { + # Caller wants a list. Return a list + return ("Everest", "K2", "Etna") if wantarray; + + # Caller wants a scalar. Return a scalar + return "Everest ::: K2 ::: Etna"; +} + +my @array = contextualSubroutine(); +print @array; # "EverestK2Etna" + +my $scalar = contextualSubroutine(); +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> +<!-- +<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> +<p>Perl has several dozen built-in variables. Some have useful names, such as <tt>@ARGV</tt> which contains the list of arguments with which the current Perl script was invoked, and <tt>%ENV</tt> which contains environment variables. Others are common enough that they can be referenced freely: the only two that I'd pick are the default iterator <tt>$_</tt> and the subroutine argument list <tt>@_</tt>, the latter of which you will encounter very shortly.</p> +<p>The remainder are mostly unmemorable punctuation names such as <tt>$"</tt>, <tt>$,</tt> and <tt>$!</tt>.</p> +<p>Using such variables without explanation is discouraged. You have some options:</p> +<ul> + <li>Comments.</li> + <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> + +<p>In Perl, packages and modules are different things.</p> + +<h3>Packages</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> + +<pre class="perl"> +sub subroutine { + print "universe"; +} + +our $variable = "empty"; + +package Food::Potatoes; + +# no collision: +sub subroutine { + print "kingedward"; +} + +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> + +<pre class="perl"> +subroutine(); # "kingedward" +print $variable; # "mashed" + +main::subroutine(); # "universe" +print $main::variable; # "empty" +Food::Potatoes::subroutine(); # "kingedward" +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> + +<pre class="perl"> +use strict; +use warnings; + +package Mathematics::Powers; + +our $e = 2.71828; + +sub exp { + return $e ** shift; +} + +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>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> +<p>or</p> +<pre class="bash"> +export PERL5LIB=/foo/bar/baz:$PERL5LIB +</pre> + +<p>Once the Perl module is created and <tt>perl</tt> knows where to look for it, you can use the <tt>require</tt> built-in function to search for and execute it during a Perl script. For example, calling <tt>require Mathematics::Powers</tt> causes the Perl interpreter to search each directory listed in <tt>PERL5LIB</tt> in turn, looking for a file called <tt>Mathematics/Powers.pm</tt>. After the module has been loaded, the subroutines and variables that were defined there suddenly become available in the main script. Our example script might be called <tt>powers.pl</tt> and read as follows:</p> + +<pre class="perl"> +use strict; +use warnings; + +require Mathematics::Powers; + +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>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> + <li><strong>A Perl script (<tt>.pl</tt> file) must always contain exactly zero <tt>package</tt> declarations.</strong></li> + <li><strong>A Perl module (<tt>.pm</tt> file) must always contain exactly one <tt>package</tt> declaration, corresponding exactly to its name and location.</strong> E.g. module <tt>Mathematics/Powers.pm</tt> must begin with <tt>package Mathematics::Powers</tt>.</li> +</ol> +<p>Because of this, in practice you will find that most "packages" and "modules" produced by reliable third parties <em>can</em> be regarded and referred to interchangeably. However, it is important that you do not take this for granted, because one day you <em>will</em> meet code produced by a madman.</p> + +<h2>Object-oriented Perl</h2> + +<p>Perl is not a great language for OO programming. Perl's OO capabilities were grafted on after the fact, and this shows.</p> + +<ul> + <li><p>An <i>object</i> is simply a reference (i.e. a scalar variable) which happens to know which class its referent belongs to. To tell a reference that its referent belongs to a class, use <tt>bless</tt>. To find out what class a reference's referent belongs to (if any), use <tt>ref</tt>.</p></li> + <li><p>A <i>method</i> is simply a subroutine that expects an object (or, in the case of class methods, a package name) as its first argument. Object methods are invoked using <tt>$obj->method()</tt>; class methods are invoked using <tt>Package::Name->method()</tt>.</p></li> + <li><p>A <i>class</i> is simply a package that happens to contain methods.</p></li> +</ul> + +<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"> +use strict; +use warnings; + +package Animals::Animal; + +sub eat { + # First argument is always the object to act upon. + my $self = shift; + + foreach my $food ( @_ ) { + if($self->canEat($food)) { + print "Eating ", $food; + } else { + print "Can't eat ", $food; + } + } +} + +sub canEat { + return 1; +} + +1; +</pre> + +<p>And a Perl script making use of this class might read:</p> + +<pre class="perl"> +use strict; +use warnings; + +require Animals::Animal; + +my $animal = {}; # $animal is an ordinary hash reference +print ref $animal; # "HASH" +bless $animal, "Animals::Animal"; # now it is an object of class "Animals::Animal" +print ref $animal; # "Animals::Animal" + +$animal->eat("insects", "curry", "salmon"); +</pre> + +<p>This final call is equivalent to <tt>Animals::Animal::eat($animal, "insects", "curry", "salmon")</tt>.</p> + +<p>Note: literally any reference can be blessed into any class. It's up to you to ensure that (1) the referent can actually be used as an instance of this class and (2) that the class in question exists and has been loaded.</p> + +<h3>Inheritance</h3> + +<p>To create a class inheriting from a base class, populate the <tt>@ISA</tt> package variable. Let's suppose we subclassed <tt>Animals::Animal</tt> with <tt>Animals::Bear</tt>, located at <tt>Animals/Bear.pm</tt>:</p> + +<pre class="perl"> +use strict; +use warnings; + +package Animals::Bear; + +require Animals::Animal; + +# Inherit from Animals::Animal +our @ISA = ("Animals::Animal"); + +# Override one method +sub canEat { + shift; + return 1 if shift eq "salmon"; + return 0; +} + +1; +</pre> + +<p>And some sample code:</p> + +<pre class="perl"> +use strict; +use warnings; + +require Animals::Bear; + +my $bear = bless {}, "Animals::Bear"; + +$bear->eat("insects", "curry", "salmon"); # eat only the salmon +</pre> + +<p>This final method call tries to invoke <tt>Animals::Bear::eat($bear, "insects", "curry", "salmon")</tt>, but a subroutine <tt>eat()</tt> isn't defined in the <tt>Animals::Bear</tt> package. However, because <tt>@ISA</tt> has been populated with a parent package <tt>Animals::Animal</tt>, the Perl interpreter tries calling <tt>Animals::Animal::eat($bear, "insects", "curry", "salmon")</tt> instead, which works. Note how the class <tt>Animals::Animal</tt> had to be loaded explicitly by <tt>Animals::Bear</tt>.</p> +<p>Since <tt>@ISA</tt> is an array, Perl supports multiple inheritance, with all the benefits and horrors this entails.</p> + +<h2><tt>BEGIN</tt> blocks</h2> + +<p>A <tt>BEGIN</tt> block is executed as soon as the compiler has finished parsing it, even before the compiler parses the rest of the file. It is ignored at execution time.</p> + +<pre class="perl"> +use strict; +use warnings; + +# a package declaration might go here + +BEGIN { + # do something extremely important +} + +# actual code +</pre> + +<p>A <tt>BEGIN</tt> block is always executed first. If you create multiple <tt>BEGIN</tt> blocks (don't), they are executed in order from top to bottom as the compiler encounters them. A <tt>BEGIN</tt> block always executes first even if it is placed halfway through a script (don't do this) or even at the end (or this).</p> +<p>Because they are executed at compilation time, a <tt>BEGIN</tt> block placed inside a conditional block will <em>still</em> be executed first, even if the conditional evaluates to false and despite the fact that the conditional <em>has not been evaluated at all yet</em> and in fact <em>may never be evaluated</em>. <strong>Do not put <tt>BEGIN</tt> blocks in conditionals!</strong> If you want to do something conditionally at compile time, you need to put the conditional <em>inside</em> the <tt>BEGIN</tt> block:</p> +<pre class="perl"> +BEGIN { + if($condition) { + # etc. + } +} +</pre> + +<h2><tt>use</tt></h2> +<p>Okay. Now that you understand the obtuse behaviour and semantics of packages, modules, class methods and <tt>BEGIN</tt> blocks, I can explain the exceedingly commonly-seen <tt>use</tt> function.</p> +<p>The following three statements:</p> +<pre class="perl"> +use Bugs::Caterpillar ("crawl", "pupate"); +use Bugs::Caterpillar (); +use Bugs::Caterpillar; +</pre> +<p>are respectively equivalent to:</p> +<pre class="perl"> +BEGIN { + require Bugs::Caterpillar; + Bugs::Caterpillar->import("crawl", "pupate"); +} +BEGIN { + require Bugs::Caterpillar; +} +BEGIN { + require Bugs::Caterpillar; + Bugs::Caterpillar->import(); +} +</pre> +<ul> + <li>No, the three examples are not in the wrong order. It is just that Perl is dumb.</li> + <li>A <tt>use</tt> call is a disguised <tt>BEGIN</tt> block. The same caveats apply. <tt>use</tt> statements must always be placed at the top of the file, and <strong>never inside conditionals</strong>.</li> + <li><tt>import()</tt> is not a built-in Perl function. It is a <strong>user-defined class method</strong>. The burden is on the programmer of the <tt>Bugs::Caterpillar</tt> package to define or inherit <tt>import()</tt>, and the method could theoretically accept anything as arguments and do anything with those arguments.</li> + <li>Notice how <tt>require Bugs::Caterpillar</tt> loads a <strong>module</strong> named <tt>Bugs/Caterpillar.pm</tt>, whereas <tt>Bugs::Caterpillar->import()</tt> calls the <tt>import()</tt> subroutine that was defined inside the <tt>Bugs::Caterpillar</tt> <strong>package</strong>. Let's hope the module and the package coincide!</li> +</ul> + +<h2>Exporter</h2> + +<p>The most common way to define an <tt>import()</tt> method is to inherit it from Exporter module. Exporter is a <i>de facto</i> core feature of the Perl programming language. In Exporter's implementation of <tt>import()</tt>, the list of arguments that you pass in is interpreted as a list of subroutine names. When a subroutine is <tt>import()</tt>ed, it becomes available in the current namespace as well as in its own original namespace.</p> + +<p>This concept is easiest to grasp using an example. Here's what <tt>Bugs/Caterpillar.pm</tt> looks like:</p> + +<pre class="perl"> +use strict; +use warnings; + +package Bugs::Caterpillar; + +require Exporter; + +our @ISA = ("Exporter"); + +our @EXPORT_OK = ("crawl", "eat"); +our @EXPORT = ("crawl"); + +sub crawl { print "inch inch"; } +sub eat { print "chomp chomp"; } +sub pupate { print "bloop bloop"; } + +1; +</pre> + +<p>And here's a script which makes use of the <tt>Bugs/Caterpillar.pm</tt> module:</p> + +<pre class="perl"> +use strict; +use warnings; + +use Bugs::Caterpillar; + +Bugs::Caterpillar::crawl(); # "inch inch" +Bugs::Caterpillar::eat(); # "chomp chomp" +Bugs::Caterpillar::pupate(); # "bloop bloop" + +crawl(); # "inch inch" +</pre> + +<p>The package variable <tt>@EXPORT_OK</tt> can be populated with a list of all subroutines which the user can import explicitly by passing subroutine names to <tt>import()</tt>. If <tt>import()</tt> is called with the name of a subroutine not in this list, a runtime error will occur. For example, try <tt>use Bugs::Caterpillar ("pupate")</tt>.</p> + +<p>The package variable <tt>@EXPORT</tt> can be populated with a list of subroutines to be exported by default. These are exported if <tt>import()</tt> is called with no arguments at all, which is what happens in this example.</p> + +<p>As a result of being <tt>import()</tt>ed, a method such as <tt>Bugs::Caterpillar::crawl()</tt> become available without qualification as <tt>crawl()</tt>. This saves typing. (Note: regardless of the content of <tt>@EXPORT_OK</tt>, every method can always be called "longhand", as shown above. There are no private methods in Perl. Customarily, a method intended for private use is named with a leading underscore or two.)</p> + +<p><strong>A caution.</strong> Notice how <tt>crawl()</tt> was neither defined in the script, nor explicitly <tt>import()</tt>ed from another file with e.g. <tt>use Bugs::Caterpillar ("crawl")</tt>. Suppose the middle three lines weren't there to provide clues, and suppose there were a dozen <tt>use</tt> calls alongside <tt>use Bugs::Caterpillar</tt>. And remember that any module is free to have more <tt>use</tt> calls of its own. In such a situation, it is extremely difficult to locate the place where <tt>crawl()</tt> was originally defined. The moral of this story is twofold:</p> +<ol> +<li><p>When creating a module which makes use of Exporter, never use <tt>@EXPORT</tt> to export subroutines by default. Always make the user call subroutines "longhand" or <tt>import()</tt> them explicitly (using e.g. <tt>use Bugs::Caterpillar ("crawl")</tt>, which is a strong clue to look in <tt>Bugs/Caterpillar.pm</tt> for the definition of <tt>crawl()</tt>).</p></li> +<li><p>When <tt>use</tt>ing a module, always explicitly name the subroutines you want to <tt>import()</tt>. If you don't want to <tt>import()</tt> any subroutines and wish to refer to them longhand, you must supply an explicit empty list: <tt>use Bugs::Caterpillar ()</tt>.</p></li> +</ol> + +<h2>Files</h2> + +<p>A <i>file handle</i> is a completely different object from a scalar, array or hash variable. File handles are customarily represented in <tt>ALLCAPS</tt>; three familiar built-in filehandles are <tt>STDIN</tt>, <tt>STDOUT</tt> and <tt>STDERR</tt>.</p> + +<p>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"> +my $f = "text.txt"; +my $result = open INPUT, "<", $f; + +if(!defined $result || !$result) { + die "Couldn't open ", $f, " for reading"; +} +</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"> +open(INPUT, "<", $f) || die "Couldn't open ", $f, " for reading"; +</pre> +<p>Notice how without the brackets, this would be:</p> +<pre class="perl"> +open INPUT, "<", $f || die "Couldn't open ", $f, " for reading"; +</pre> +<p>Which is the same as the nonsensical:</p> +<pre class="perl"> +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"> +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"> +while(1) { + my $line = readline INPUT; + last unless defined $line; + # process the line... +} +</pre> +<p>To truncate that possible trailing line break, use <tt>chomp</tt>:</p> +<pre class="perl"> +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"> +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"> +while(my $line = <INPUT>) { + # process $line... +} +</pre> +<p>And even:</p> +<pre class="perl"> +while(<INPUT>) { + # process $_... +} +</pre> +<p>To read a single line of user input:</p> +<pre class="perl"> +my $line = <STDIN>; +</pre> +<p>To just wait for the user to hit Enter:</p> +<pre class="perl"> +<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"> +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"> +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"> +my $fh; +open $fh, "<", "text.txt" or die; +while(<$fh>) { + # etc... +} +close $fh; +</pre> +<h2>System calls</h2> +<p>Apologies if you already know the following non-Perl-related facts. Every time a process finishes on a Windows or Linux system (and, I assume, on most other systems), it concludes with a 16-bit <i>status word</i>. The highest 8 bits constitute a <i>return code</i> between 0 and 255 inclusive, with 0 conventionally representing unqualified success, and other values representing various degrees of failure. The other 8 bits are less frequently examined - they "reflect mode of failure, like signal death and core dump information".</p> +<p>You can exit from a Perl script with the return code of your choice (from 0 to 255) using <tt>exit</tt>.</p> +<p>Perl provides More Than One Way To - in a single call - spawn a child process, pause the current script until the child process has finished, and then resume interpretation of the current script. Whichever method is used, you will find that immediately afterwards, the built-in variable <tt>$?</tt> (<tt>$CHILD_ERROR</tt>) has been populated with the status word that was returned from that child process's termination. You can get the return code by taking just the highest 8 of those 16 bits: <tt>$? >> 8</tt>.</p> +<p>The <tt>system</tt> function can be used to invoke another program with the arguments listed. The value returned by <tt>system</tt> is the same value with which <tt>$?</tt> is populated:</p> +<pre class="perl"> +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"> +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"> +use strict; +use warnings; + +print @ARGV; +exit(37); +</pre> + +<h2>Miscellaneous notes</h2> +<p>Perl provides a wide selection of quote-like operators in addition to what you've seen in these documents:</p> +<ul> + <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"> +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"> +my @capitals = ("Baton Rouge", "Indianapolis", "Columbus", "Montgomery", "Helena", "Denver", "Boise"); +my $regex = qr/^[B-H]/; +print join ", ", grep /$regex/, @capitals; +</pre> + </li> + <li> + <p><tt>qx{ }</tt> can be used instead of `backticks` to invoke a program and capture its output:</p> +<pre class="perl"> +my $text = qx{perl anotherscript.pl foo bar baz}; +</pre> + </li> + <li> + <p>And many more!</p> + </li> +</ul> +<p>Instead of braces, you can use any character you like as the delimiter in these alternate quote-like operators, as well as in <tt>m//</tt> regex matches and <tt>s///</tt> regex replacements. This is actually quite useful if your regex contains a lot of slashes or backslashes. For example, <tt>m!///!</tt> matches three literal forward slashes.</p> +<p>Perl does have <tt>CONSTANTS</tt>. These are discouraged now, but weren't always. Constants are actually just subroutine calls with omitted brackets.</p> +<p>Sometimes people omit quotes around hash keys. They can get away with it because in this situation a bareword (a string with no sigil) occurs as a string, as opposed to a subroutine call or a filehandle or a package name.</p> +<p>If you see a block of unformatted code wrapped in a delimiter with double chevrons, like <tt><<EOF</tt>, the magic word to Google for is "heredoc".</p> +<p>The Data::Dumper module can be used to output an arbitrary scalar variable to the screen. This is an essential debug tool.</p> + +<p>Warning! Many built-in functions can be called with no arguments, <strong>causing them to operate on <tt>$_</tt> instead</strong>. Hopefully this will help you understand formations like:</p> +<pre class="perl"> +print foreach @array; + +foreach ( @array ) { + next unless defined; +} +</pre> +<p>I dislike this formation because it can lead to problems when refactoring.</p> + + +<h3>File tests</h3> + +<p>The function <tt>-e</tt> is a built-in function which tests whether the named file exists.</p> +<pre class="perl"> +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> + + + +<script type="text/javascript"><!-- + var sc_project=667681; + var sc_invisible=1; + var sc_partition=5; + var sc_security="f56850e2"; + var sc_remove_link=1; +// --></script> +<script type="text/javascript" src="http://www.statcounter.com/counter/counter_xhtml.js"></script> + +<noscript> + <p><img + class="statcounter" + src="http://c6.statcounter.com/counter.php?sc_project=667681&java=0&security=f56850e2&invisible=1" + alt="website statistics" + /></p> +</noscript> +</body> +</html> \ No newline at end of file