home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 2 / RISC_DISC_2.iso / pd_share / utilities / cli / perl / !Perl / Manual / perlsyn < prev    next >
Encoding:
Text File  |  1995-04-18  |  11.0 KB  |  274 lines

  1. <!-- $RCSfile$$Revision$$Date$ -->
  2. <!-- $Log$ -->
  3. <HTML>
  4. <TITLE> PERLSYN </TITLE>
  5. <h2>NAME</h2>
  6. perlsyn - Perl syntax
  7. <p><h2>DESCRIPTION</h2>
  8. A Perl script consists of a sequence of declarations and statements.
  9. The only things that need to be declared in Perl are report formats
  10. and subroutines.  See the sections below for more information on those
  11. declarations.  All uninitialized user-created objects are assumed to
  12. start with a null or 0 value until they are defined by some explicit
  13. operation such as assignment.  (Though you can get warnings about the
  14. use of undefined values if you like.)  The sequence of statements is
  15. executed just once, unlike in <B>sed</B> and <B>awk</B> scripts, where the
  16. sequence of statements is executed for each input line.  While this means
  17. that you must explicitly loop over the lines of your input file (or
  18. files), it also means you have much more control over which files and
  19. which lines you look at.  (Actually, I'm lying--it is possible to do an
  20. implicit loop with either the 
  21. <A HREF="perlrun.html#perlrun_353">-n</A>
  22.  or 
  23. <A HREF="perlrun.html#perlrun_354">-p</A>
  24.  switch.  It's just not the
  25. mandatory default like it is in <B>sed</B> and <B>awk</B>.)
  26. <p>Perl is, for the most part, a free-form language.  (The only
  27. exception to this is format declarations, for obvious reasons.) Comments
  28. are indicated by the "#" character, and extend to the end of the line.  If
  29. you attempt to use <B>/* */</B> C-style comments, it will be interpreted
  30. either as division or pattern matching, depending on the context, and C++
  31. <B>//</B> comments just look like a null regular expression, So don't do
  32. that.
  33. <p>A declaration can be put anywhere a statement can, but has no effect on
  34. the execution of the primary sequence of statements--declarations all
  35. take effect at compile time.  Typically all the declarations are put at
  36. the beginning or the end of the script.
  37. <p>As of Perl 5, declaring a subroutine allows a subroutine name to be used
  38. as if it were a list operator from that point forward in the program.  You
  39. can declare a subroutine without defining it by saying just
  40. <p><pre>
  41.         sub myname;
  42.         $me = myname $0                 or die "can't get myname";
  43. </pre>
  44. Note that it functions as a list operator though, not a unary
  45. operator, so be careful to use <B>or</B> instead of <B>||</B> there.
  46. <p>Subroutines declarations can also be imported by a 
  47. <A HREF="perlfunc.html#perlfunc_263">use</A>
  48.  statement.
  49. <p>Also as of Perl 5, a statement sequence may contain declarations of
  50. lexically scoped variables, but apart from declaring a variable name,
  51. the declaration acts like an ordinary statement, and is elaborated within
  52. the sequence of statements as if it were an ordinary statement.
  53. <p><h3>Simple statements</h3>
  54. The only kind of simple statement is an expression evaluated for its
  55. side effects.  Every simple statement must be terminated with a
  56. semicolon, unless it is the final statement in a block, in which case
  57. the semicolon is optional.  (A semicolon is still encouraged there if the
  58. block takes up more than one line, since you may add another line.)
  59. Note that there are some operators like <B>eval {}</B> and <B>do {}</B> that look
  60. like compound statements, but aren't (they're just TERMs in an expression), 
  61. and thus need an explicit termination
  62. if used as the last item in a statement.
  63. <p>Any simple statement may optionally be followed by a <I>SINGLE</I> modifier,
  64. just before the terminating semicolon (or block ending).  The possible
  65. modifiers are:
  66. <p><pre>
  67.         if EXPR
  68.         unless EXPR
  69.         while EXPR
  70.         until EXPR
  71. </pre>
  72. The <B>if</B> and <B>unless</B> modifiers have the expected semantics,
  73. presuming you're a speaker of English.  The <B>while</B> and <B>until</B>
  74. modifiers also have the usual "while loop" semantics (conditional
  75. evaluated first), except when applied to a do-BLOCK (or to the
  76. now-deprecated do-SUBROUTINE statement), in which case the block
  77. executes once before the conditional is evaluated.  This is so that you
  78. can write loops like:
  79. <p><pre>
  80.         do {
  81.         $_ = <STDIN>;
  82.         ...
  83.         } until $_ eq ".\n";
  84. </pre>
  85. See perlfunc/do.  Note also that the loop control
  86. statements described later will <I>NOT</I> work in this construct, since
  87. modifiers don't take loop labels.  Sorry.  You can always wrap
  88. another block around it to do that sort of thing.)
  89. <p><h3>Compound statements</h3>
  90. In Perl, a sequence of statements that defines a scope is called a block.
  91. Sometimes a block is delimited by the file containing it (in the case
  92. of a required file, or the program as a whole), and sometimes a block
  93. is delimited by the extent of a string (in the case of an eval).
  94. <p>But generally, a block is delimited by curly brackets, also known as braces.
  95. We will call this syntactic construct a BLOCK.
  96. <p>The following compound statements may be used to control flow:
  97. <p><pre>
  98.         if (EXPR) BLOCK
  99.         if (EXPR) BLOCK else BLOCK
  100.         if (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK
  101.         LABEL while (EXPR) BLOCK
  102.         LABEL while (EXPR) BLOCK continue BLOCK
  103.         LABEL for (EXPR; EXPR; EXPR) BLOCK
  104.         LABEL foreach VAR (ARRAY) BLOCK
  105.         LABEL BLOCK continue BLOCK
  106. </pre>
  107. Note that, unlike C and Pascal, these are defined in terms of BLOCKs,
  108. not statements.  This means that the curly brackets are <I>required</I>--no
  109. dangling statements allowed.  If you want to write conditionals without
  110. curly brackets there are several other ways to do it.  The following
  111. all do the same thing:
  112. <p><pre>
  113.         if (!open(FOO)) { die "Can't open $FOO: $!"; }
  114.         die "Can't open $FOO: $!" unless open(FOO);
  115.         open(FOO) or die "Can't open $FOO: $!"; # FOO or bust!
  116.         open(FOO) ? 'hi mom' : die "Can't open $FOO: $!";
  117.                         # a bit exotic, that last one
  118. </pre>
  119. The <B>if</B> statement is straightforward.  Since BLOCKs are always
  120. bounded by curly brackets, there is never any ambiguity about which
  121. <B>if</B> an <B>else</B> goes with.  If you use <B>unless</B> in place of <B>if</B>,
  122. the sense of the test is reversed.
  123. <p>The <B>while</B> statement executes the block as long as the expression is
  124. true (does not evaluate to the null string or 0 or "0").  The LABEL is
  125. optional, and if present, consists of an identifier followed by a
  126. colon.  The LABEL identifies the loop for the loop control statements
  127.  
  128. <A HREF="perlfunc.html#perlfunc_179">next</A>
  129. <A HREF="perlfunc.html#perlfunc_161">last</A>
  130. , and 
  131. <A HREF="perlfunc.html#perlfunc_202">redo</A>
  132.  (see below).  If there is a <B>continue</B>
  133. BLOCK, it is always executed just before the conditional is about to be
  134. evaluated again, just like the third part of a <B>for</B> loop in C.
  135. Thus it can be used to increment a loop variable, even when the loop
  136. has been continued via the 
  137. <A HREF="perlfunc.html#perlfunc_179">next</A>
  138.  statement (which is similar to the C
  139. <B>continue</B> statement).
  140. <p>If the word <B>while</B> is replaced by the word <B>until</B>, the sense of the
  141. test is reversed, but the conditional is still tested before the first
  142. iteration.
  143. <p>In either the <B>if</B> or the <B>while</B> statement, you may replace "(EXPR)"
  144. with a BLOCK, and the conditional is true if the value of the last
  145. statement in that block is true.  (This feature continues to work in Perl
  146. 5 but is deprecated.  Please change any occurrences of "if BLOCK" to
  147. "if (do BLOCK)".)
  148. <p>The C-style <B>for</B> loop works exactly like the corresponding <B>while</B> loop:
  149. <p><pre>
  150.         for ($i = 1; $i < 10; $i++) {
  151.         ...
  152.         }
  153. </pre>
  154. is the same as
  155. <p><pre>
  156.         $i = 1;
  157.         while ($i < 10) {
  158.         ...
  159.         } continue {
  160.         $i++;
  161.         }
  162. </pre>
  163. The foreach loop iterates over a normal list value and sets the
  164. variable VAR to be each element of the list in turn.  The variable is
  165. implicitly local to the loop (unless declared previously with 
  166. <A HREF="perlfunc.html#perlfunc_178">my</A>
  167. ),
  168. and regains its former value upon exiting the loop.  The <B>foreach</B>
  169. keyword is actually a synonym for the <B>for</B> keyword, so you can use
  170. <B>foreach</B> for readability or <B>for</B> for brevity.  If VAR is omitted, $_
  171. is set to each value.  If ARRAY is an actual array (as opposed to an
  172. expression returning a list value), you can modify each element of the
  173. array by modifying VAR inside the loop.  Examples:
  174. <p><pre>
  175.         for (@ary) { s/foo/bar/; }
  176. </pre>
  177. <pre>
  178.         foreach $elem (@elements) {
  179.         $elem *= 2;
  180.         }
  181. </pre>
  182. <pre>
  183.         for ((10,9,8,7,6,5,4,3,2,1,'BOOM')) {
  184.         print $_, "\n"; sleep(1);
  185.         }
  186. </pre>
  187. <pre>
  188.         for (1..15) { print "Merry Christmas\n"; }
  189. </pre>
  190. <pre>
  191.         foreach $item (split(/:[\\\n:]*/, $ENV{'TERMCAP'})) {
  192.         print "Item: $item\n";
  193.         }
  194. </pre>
  195. A BLOCK by itself (labeled or not) is semantically equivalent to a loop
  196. that executes once.  Thus you can use any of the loop control
  197. statements in it to leave or restart the block.  The <B>continue</B> block
  198. is optional.  This construct is particularly nice for doing case
  199. structures.
  200. <p><pre>
  201.         SWITCH: {
  202.         if (/^abc/) { $abc = 1; last SWITCH; }
  203.         if (/^def/) { $def = 1; last SWITCH; }
  204.         if (/^xyz/) { $xyz = 1; last SWITCH; }
  205.         $nothing = 1;
  206.         }
  207. </pre>
  208. There is no official switch statement in Perl, because there are
  209. already several ways to write the equivalent.  In addition to the
  210. above, you could write
  211. <p><pre>
  212.         SWITCH: {
  213.         $abc = 1, last SWITCH  if /^abc/;
  214.         $def = 1, last SWITCH  if /^def/;
  215.         $xyz = 1, last SWITCH  if /^xyz/;
  216.         $nothing = 1;
  217.         }
  218. </pre>
  219. (That's actually not as strange as it looks one you realize that you can
  220. use loop control "operators" within an expression,  That's just the normal
  221. C comma operator.)
  222. <p>or
  223. <p><pre>
  224.         SWITCH: {
  225.         /^abc/ && do { $abc = 1; last SWITCH; };
  226.         /^def/ && do { $def = 1; last SWITCH; };
  227.         /^xyz/ && do { $xyz = 1; last SWITCH; };
  228.         $nothing = 1;
  229.         }
  230. </pre>
  231. or formatted so it stands out more as a "proper" switch statement:
  232. <p><pre>
  233.         SWITCH: {
  234.         /^abc/      && do { 
  235.                             $abc = 1; 
  236.                             last SWITCH; 
  237.                        };
  238. </pre>
  239. <pre>
  240.         /^def/      && do { 
  241.                             $def = 1; 
  242.                             last SWITCH; 
  243.                        };
  244. </pre>
  245. <pre>
  246.         /^xyz/      && do { 
  247.                             $xyz = 1; 
  248.                             last SWITCH; 
  249.                         };
  250.         $nothing = 1;
  251.         }
  252. </pre>
  253. or
  254. <p><pre>
  255.         SWITCH: {
  256.         /^abc/ and $abc = 1, last SWITCH;
  257.         /^def/ and $def = 1, last SWITCH;
  258.         /^xyz/ and $xyz = 1, last SWITCH;
  259.         $nothing = 1;
  260.         }
  261. </pre>
  262. or even, horrors,
  263. <p><pre>
  264.         if (/^abc/)
  265.         { $abc = 1 }
  266.         elsif (/^def/)
  267.         { $def = 1 }
  268.         elsif (/^xyz/)
  269.         { $xyz = 1 }
  270.         else
  271.         { $nothing = 1 }
  272. </pre>
  273.