home *** CD-ROM | disk | FTP | other *** search
/ Acorn User 10 / AU_CD10.iso / Updates / Perl / Non-RPC / Docs / perlsyn < prev    next >
Text File  |  1999-04-17  |  23KB  |  678 lines

  1. NAME
  2.     perlsyn - Perl syntax
  3.  
  4. DESCRIPTION
  5.     A Perl script consists of a sequence of declarations and
  6.     statements. The only things that need to be declared in Perl are
  7.     report formats and subroutines. See the sections below for more
  8.     information on those declarations. All uninitialized user-
  9.     created objects are assumed to start with a `null' or `0' value
  10.     until they are defined by some explicit operation such as
  11.     assignment. (Though you can get warnings about the use of
  12.     undefined values if you like.) The sequence of statements is
  13.     executed just once, unlike in sed and awk scripts, where the
  14.     sequence of statements is executed for each input line. While
  15.     this means that you must explicitly loop over the lines of your
  16.     input file (or files), it also means you have much more control
  17.     over which files and which lines you look at. (Actually, I'm
  18.     lying--it is possible to do an implicit loop with either the -n
  19.     or -p switch. It's just not the mandatory default like it is in
  20.     sed and awk.)
  21.  
  22.   Declarations
  23.  
  24.     Perl is, for the most part, a free-form language. (The only
  25.     exception to this is format declarations, for obvious reasons.)
  26.     Text from a `"#"' character until the end of the line is a
  27.     comment, and is ignored. If you attempt to use `/* */' C-style
  28.     comments, it will be interpreted either as division or pattern
  29.     matching, depending on the context, and C++ `//' comments just
  30.     look like a null regular expression, so don't do that.
  31.  
  32.     A declaration can be put anywhere a statement can, but has no
  33.     effect on the execution of the primary sequence of statements--
  34.     declarations all take effect at compile time. Typically all the
  35.     declarations are put at the beginning or the end of the script.
  36.     However, if you're using lexically-scoped private variables
  37.     created with `my()', you'll have to make sure your format or
  38.     subroutine definition is within the same block scope as the my
  39.     if you expect to be able to access those private variables.
  40.  
  41.     Declaring a subroutine allows a subroutine name to be used as if
  42.     it were a list operator from that point forward in the program.
  43.     You can declare a subroutine without defining it by saying `sub
  44.     name', thus:
  45.  
  46.         sub myname;
  47.         $me = myname $0         or die "can't get myname";
  48.  
  49.  
  50.     Note that it functions as a list operator, not as a unary
  51.     operator; so be careful to use `or' instead of `||' in this
  52.     case. However, if you were to declare the subroutine as `sub
  53.     myname ($)', then `myname' would function as a unary operator,
  54.     so either `or' or `||' would work.
  55.  
  56.     Subroutines declarations can also be loaded up with the
  57.     `require' statement or both loaded and imported into your
  58.     namespace with a `use' statement. See the perlmod manpage for
  59.     details on this.
  60.  
  61.     A statement sequence may contain declarations of lexically-
  62.     scoped variables, but apart from declaring a variable name, the
  63.     declaration acts like an ordinary statement, and is elaborated
  64.     within the sequence of statements as if it were an ordinary
  65.     statement. That means it actually has both compile-time and run-
  66.     time effects.
  67.  
  68.   Simple statements
  69.  
  70.     The only kind of simple statement is an expression evaluated for
  71.     its side effects. Every simple statement must be terminated with
  72.     a semicolon, unless it is the final statement in a block, in
  73.     which case the semicolon is optional. (A semicolon is still
  74.     encouraged there if the block takes up more than one line,
  75.     because you may eventually add another line.) Note that there
  76.     are some operators like `eval {}' and `do {}' that look like
  77.     compound statements, but aren't (they're just TERMs in an
  78.     expression), and thus need an explicit termination if used as
  79.     the last item in a statement.
  80.  
  81.     Any simple statement may optionally be followed by a *SINGLE*
  82.     modifier, just before the terminating semicolon (or block
  83.     ending). The possible modifiers are:
  84.  
  85.         if EXPR
  86.         unless EXPR
  87.         while EXPR
  88.         until EXPR
  89.         foreach EXPR
  90.  
  91.  
  92.     The `if' and `unless' modifiers have the expected semantics,
  93.     presuming you're a speaker of English. The `foreach' modifier is
  94.     an iterator: For each value in EXPR, it aliases `$_' to the
  95.     value and executes the statement. The `while' and `until'
  96.     modifiers have the usual "`while' loop" semantics (conditional
  97.     evaluated first), except when applied to a `do'-BLOCK (or to the
  98.     now-deprecated `do'-SUBROUTINE statement), in which case the
  99.     block executes once before the conditional is evaluated. This is
  100.     so that you can write loops like:
  101.  
  102.         do {
  103.         $line = <STDIN>;
  104.         ...
  105.         } until $line  eq ".\n";
  106.  
  107.  
  108.     See the "do" entry in the perlfunc manpage. Note also that the
  109.     loop control statements described later will *NOT* work in this
  110.     construct, because modifiers don't take loop labels. Sorry. You
  111.     can always put another block inside of it (for `next') or around
  112.     it (for `last') to do that sort of thing. For `next', just
  113.     double the braces:
  114.  
  115.         do {{
  116.         next if $x == $y;
  117.         # do something here
  118.         }} until $x++ > $z;
  119.  
  120.  
  121.     For `last', you have to be more elaborate:
  122.  
  123.         LOOP: { 
  124.             do {
  125.             last if $x = $y**2;
  126.             # do something here
  127.             } while $x++ <= $z;
  128.         }
  129.  
  130.  
  131.   Compound statements
  132.  
  133.     In Perl, a sequence of statements that defines a scope is called
  134.     a block. Sometimes a block is delimited by the file containing
  135.     it (in the case of a required file, or the program as a whole),
  136.     and sometimes a block is delimited by the extent of a string (in
  137.     the case of an eval).
  138.  
  139.     But generally, a block is delimited by curly brackets, also
  140.     known as braces. We will call this syntactic construct a BLOCK.
  141.  
  142.     The following compound statements may be used to control flow:
  143.  
  144.         if (EXPR) BLOCK
  145.         if (EXPR) BLOCK else BLOCK
  146.         if (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK
  147.         LABEL while (EXPR) BLOCK
  148.         LABEL while (EXPR) BLOCK continue BLOCK
  149.         LABEL for (EXPR; EXPR; EXPR) BLOCK
  150.         LABEL foreach VAR (LIST) BLOCK
  151.         LABEL BLOCK continue BLOCK
  152.  
  153.  
  154.     Note that, unlike C and Pascal, these are defined in terms of
  155.     BLOCKs, not statements. This means that the curly brackets are
  156.     *required*--no dangling statements allowed. If you want to write
  157.     conditionals without curly brackets there are several other ways
  158.     to do it. The following all do the same thing:
  159.  
  160.         if (!open(FOO)) { die "Can't open $FOO: $!"; }
  161.         die "Can't open $FOO: $!" unless open(FOO);
  162.         open(FOO) or die "Can't open $FOO: $!";    # FOO or bust!
  163.         open(FOO) ? 'hi mom' : die "Can't open $FOO: $!";
  164.                 # a bit exotic, that last one
  165.  
  166.  
  167.     The `if' statement is straightforward. Because BLOCKs are always
  168.     bounded by curly brackets, there is never any ambiguity about
  169.     which `if' an `else' goes with. If you use `unless' in place of
  170.     `if', the sense of the test is reversed.
  171.  
  172.     The `while' statement executes the block as long as the
  173.     expression is true (does not evaluate to the null string (`""')
  174.     or `0' or `"0")'. The LABEL is optional, and if present,
  175.     consists of an identifier followed by a colon. The LABEL
  176.     identifies the loop for the loop control statements `next',
  177.     `last', and `redo'. If the LABEL is omitted, the loop control
  178.     statement refers to the innermost enclosing loop. This may
  179.     include dynamically looking back your call-stack at run time to
  180.     find the LABEL. Such desperate behavior triggers a warning if
  181.     you use the -w flag.
  182.  
  183.     If there is a `continue' BLOCK, it is always executed just
  184.     before the conditional is about to be evaluated again, just like
  185.     the third part of a `for' loop in C. Thus it can be used to
  186.     increment a loop variable, even when the loop has been continued
  187.     via the `next' statement (which is similar to the C `continue'
  188.     statement).
  189.  
  190.   Loop Control
  191.  
  192.     The `next' command is like the `continue' statement in C; it
  193.     starts the next iteration of the loop:
  194.  
  195.         LINE: while (<STDIN>) {
  196.         next LINE if /^#/;    # discard comments
  197.         ...
  198.         }
  199.  
  200.  
  201.     The `last' command is like the `break' statement in C (as used
  202.     in loops); it immediately exits the loop in question. The
  203.     `continue' block, if any, is not executed:
  204.  
  205.         LINE: while (<STDIN>) {
  206.         last LINE if /^$/;    # exit when done with header
  207.         ...
  208.         }
  209.  
  210.  
  211.     The `redo' command restarts the loop block without evaluating
  212.     the conditional again. The `continue' block, if any, is *not*
  213.     executed. This command is normally used by programs that want to
  214.     lie to themselves about what was just input.
  215.  
  216.     For example, when processing a file like /etc/termcap. If your
  217.     input lines might end in backslashes to indicate continuation,
  218.     you want to skip ahead and get the next record.
  219.  
  220.         while (<>) {
  221.         chomp;
  222.         if (s/\\$//) {
  223.             $_ .= <>;
  224.             redo unless eof();
  225.         }
  226.         # now process $_
  227.         }
  228.  
  229.  
  230.     which is Perl short-hand for the more explicitly written
  231.     version:
  232.  
  233.         LINE: while (defined($line = <ARGV>)) {
  234.         chomp($line);
  235.         if ($line =~ s/\\$//) {
  236.             $line .= <ARGV>;
  237.             redo LINE unless eof(); # not eof(ARGV)!
  238.         }
  239.         # now process $line
  240.         }
  241.  
  242.  
  243.     Note that if there were a `continue' block on the above code, it
  244.     would get executed even on discarded lines. This is often used
  245.     to reset line counters or `?pat?' one-time matches.
  246.  
  247.         # inspired by :1,$g/fred/s//WILMA/
  248.         while (<>) {
  249.         ?(fred)?    && s//WILMA $1 WILMA/;
  250.         ?(barney)?  && s//BETTY $1 BETTY/;
  251.         ?(homer)?   && s//MARGE $1 MARGE/;
  252.         } continue {
  253.         print "$ARGV $.: $_";
  254.         close ARGV  if eof();        # reset $.
  255.         reset        if eof();        # reset ?pat?
  256.         }
  257.  
  258.  
  259.     If the word `while' is replaced by the word `until', the sense
  260.     of the test is reversed, but the conditional is still tested
  261.     before the first iteration.
  262.  
  263.     The loop control statements don't work in an `if' or `unless',
  264.     since they aren't loops. You can double the braces to make them
  265.     such, though.
  266.  
  267.         if (/pattern/) {{
  268.         next if /fred/;
  269.         next if /barney/;
  270.         # so something here
  271.         }}
  272.  
  273.  
  274.     The form `while/if BLOCK BLOCK', available in Perl 4, is no
  275.     longer available. Replace any occurrence of `if BLOCK' by `if
  276.     (do BLOCK)'.
  277.  
  278.   For Loops
  279.  
  280.     Perl's C-style `for' loop works exactly like the corresponding
  281.     `while' loop; that means that this:
  282.  
  283.         for ($i = 1; $i < 10; $i++) {
  284.         ...
  285.         }
  286.  
  287.  
  288.     is the same as this:
  289.  
  290.         $i = 1;
  291.         while ($i < 10) {
  292.         ...
  293.         } continue {
  294.         $i++;
  295.         }
  296.  
  297.  
  298.     (There is one minor difference: The first form implies a lexical
  299.     scope for variables declared with `my' in the initialization
  300.     expression.)
  301.  
  302.     Besides the normal array index looping, `for' can lend itself to
  303.     many other interesting applications. Here's one that avoids the
  304.     problem you get into if you explicitly test for end-of-file on
  305.     an interactive file descriptor causing your program to appear to
  306.     hang.
  307.  
  308.         $on_a_tty = -t STDIN && -t STDOUT;
  309.         sub prompt { print "yes? " if $on_a_tty }
  310.         for ( prompt(); <STDIN>; prompt() ) {
  311.         # do something
  312.         }
  313.  
  314.  
  315.   Foreach Loops
  316.  
  317.     The `foreach' loop iterates over a normal list value and sets
  318.     the variable VAR to be each element of the list in turn. If the
  319.     variable is preceded with the keyword `my', then it is lexically
  320.     scoped, and is therefore visible only within the loop.
  321.     Otherwise, the variable is implicitly local to the loop and
  322.     regains its former value upon exiting the loop. If the variable
  323.     was previously declared with `my', it uses that variable instead
  324.     of the global one, but it's still localized to the loop. (Note
  325.     that a lexically scoped variable can cause problems if you have
  326.     subroutine or format declarations within the loop which refer to
  327.     it.)
  328.  
  329.     The `foreach' keyword is actually a synonym for the `for'
  330.     keyword, so you can use `foreach' for readability or `for' for
  331.     brevity. (Or because the Bourne shell is more familiar to you
  332.     than *csh*, so writing `for' comes more naturally.) If VAR is
  333.     omitted, `$_' is set to each value. If any element of LIST is an
  334.     lvalue, you can modify it by modifying VAR inside the loop.
  335.     That's because the `foreach' loop index variable is an implicit
  336.     alias for each item in the list that you're looping over.
  337.  
  338.     If any part of LIST is an array, `foreach' will get very
  339.     confused if you add or remove elements within the loop body, for
  340.     example with `splice'. So don't do that.
  341.  
  342.     `foreach' probably won't do what you expect if VAR is a tied or
  343.     other special variable. Don't do that either.
  344.  
  345.     Examples:
  346.  
  347.         for (@ary) { s/foo/bar/ }
  348.  
  349.         foreach my $elem (@elements) {
  350.         $elem *= 2;
  351.         }
  352.  
  353.         for $count (10,9,8,7,6,5,4,3,2,1,'BOOM') {
  354.         print $count, "\n"; sleep(1);
  355.         }
  356.  
  357.         for (1..15) { print "Merry Christmas\n"; }
  358.  
  359.         foreach $item (split(/:[\\\n:]*/, $ENV{TERMCAP})) {
  360.         print "Item: $item\n";
  361.         }
  362.  
  363.  
  364.     Here's how a C programmer might code up a particular algorithm
  365.     in Perl:
  366.  
  367.         for (my $i = 0; $i < @ary1; $i++) {
  368.         for (my $j = 0; $j < @ary2; $j++) {
  369.             if ($ary1[$i] > $ary2[$j]) {
  370.             last; # can't go to outer :-(
  371.             }
  372.             $ary1[$i] += $ary2[$j];
  373.         }
  374.         # this is where that last takes me
  375.         }
  376.  
  377.  
  378.     Whereas here's how a Perl programmer more comfortable with the
  379.     idiom might do it:
  380.  
  381.         OUTER: foreach my $wid (@ary1) {
  382.         INNER:   foreach my $jet (@ary2) {
  383.             next OUTER if $wid > $jet;
  384.             $wid += $jet;
  385.              }
  386.           }
  387.  
  388.  
  389.     See how much easier this is? It's cleaner, safer, and faster.
  390.     It's cleaner because it's less noisy. It's safer because if code
  391.     gets added between the inner and outer loops later on, the new
  392.     code won't be accidentally executed. The `next' explicitly
  393.     iterates the other loop rather than merely terminating the inner
  394.     one. And it's faster because Perl executes a `foreach' statement
  395.     more rapidly than it would the equivalent `for' loop.
  396.  
  397.   Basic BLOCKs and Switch Statements
  398.  
  399.     A BLOCK by itself (labeled or not) is semantically equivalent to
  400.     a loop that executes once. Thus you can use any of the loop
  401.     control statements in it to leave or restart the block. (Note
  402.     that this is *NOT* true in `eval{}', `sub{}', or contrary to
  403.     popular belief `do{}' blocks, which do *NOT* count as loops.)
  404.     The `continue' block is optional.
  405.  
  406.     The BLOCK construct is particularly nice for doing case
  407.     structures.
  408.  
  409.         SWITCH: {
  410.         if (/^abc/) { $abc = 1; last SWITCH; }
  411.         if (/^def/) { $def = 1; last SWITCH; }
  412.         if (/^xyz/) { $xyz = 1; last SWITCH; }
  413.         $nothing = 1;
  414.         }
  415.  
  416.  
  417.     There is no official `switch' statement in Perl, because there
  418.     are already several ways to write the equivalent. In addition to
  419.     the above, you could write
  420.  
  421.         SWITCH: {
  422.         $abc = 1, last SWITCH  if /^abc/;
  423.         $def = 1, last SWITCH  if /^def/;
  424.         $xyz = 1, last SWITCH  if /^xyz/;
  425.         $nothing = 1;
  426.         }
  427.  
  428.  
  429.     (That's actually not as strange as it looks once you realize
  430.     that you can use loop control "operators" within an expression,
  431.     That's just the normal C comma operator.)
  432.  
  433.     or
  434.  
  435.         SWITCH: {
  436.         /^abc/ && do { $abc = 1; last SWITCH; };
  437.         /^def/ && do { $def = 1; last SWITCH; };
  438.         /^xyz/ && do { $xyz = 1; last SWITCH; };
  439.         $nothing = 1;
  440.         }
  441.  
  442.  
  443.     or formatted so it stands out more as a "proper" `switch'
  444.     statement:
  445.  
  446.         SWITCH: {
  447.         /^abc/         && do {
  448.                     $abc = 1;
  449.                     last SWITCH;
  450.                    };
  451.  
  452.         /^def/         && do {
  453.                     $def = 1;
  454.                     last SWITCH;
  455.                    };
  456.  
  457.         /^xyz/         && do {
  458.                     $xyz = 1;
  459.                     last SWITCH;
  460.                     };
  461.         $nothing = 1;
  462.         }
  463.  
  464.  
  465.     or
  466.  
  467.         SWITCH: {
  468.         /^abc/ and $abc = 1, last SWITCH;
  469.         /^def/ and $def = 1, last SWITCH;
  470.         /^xyz/ and $xyz = 1, last SWITCH;
  471.         $nothing = 1;
  472.         }
  473.  
  474.  
  475.     or even, horrors,
  476.  
  477.         if (/^abc/)
  478.         { $abc = 1 }
  479.         elsif (/^def/)
  480.         { $def = 1 }
  481.         elsif (/^xyz/)
  482.         { $xyz = 1 }
  483.         else
  484.         { $nothing = 1 }
  485.  
  486.  
  487.     A common idiom for a `switch' statement is to use `foreach''s
  488.     aliasing to make a temporary assignment to `$_' for convenient
  489.     matching:
  490.  
  491.         SWITCH: for ($where) {
  492.             /In Card Names/     && do { push @flags, '-e'; last; };
  493.             /Anywhere/          && do { push @flags, '-h'; last; };
  494.             /In Rulings/        && do {                    last; };
  495.             die "unknown value for form variable where: `$where'";
  496.             }
  497.  
  498.  
  499.     Another interesting approach to a switch statement is arrange
  500.     for a `do' block to return the proper value:
  501.  
  502.         $amode = do {
  503.         if     ($flag & O_RDONLY) { "r" }    # XXX: isn't this 0?
  504.         elsif  ($flag & O_WRONLY) { ($flag & O_APPEND) ? "a" : "w" }
  505.         elsif  ($flag & O_RDWR)   {
  506.             if ($flag & O_CREAT)  { "w+" }
  507.             else                  { ($flag & O_APPEND) ? "a+" : "r+" }
  508.         }
  509.         };
  510.  
  511.  
  512.     Or
  513.  
  514.             print do {
  515.                 ($flags & O_WRONLY) ? "write-only"          :
  516.                 ($flags & O_RDWR)   ? "read-write"          :
  517.                                       "read-only";
  518.             };
  519.  
  520.  
  521.     Or if you are certainly that all the `&&' clauses are true, you
  522.     can use something like this, which "switches" on the value of
  523.     the `HTTP_USER_AGENT' envariable.
  524.  
  525.         #!/usr/bin/perl 
  526.         # pick out jargon file page based on browser
  527.         $dir = 'http://www.wins.uva.nl/~mes/jargon';
  528.         for ($ENV{HTTP_USER_AGENT}) { 
  529.         $page  =    /Mac/            && 'm/Macintrash.html'
  530.              || /Win(dows )?NT/  && 'e/evilandrude.html'
  531.              || /Win|MSIE|WebTV/ && 'm/MicroslothWindows.html'
  532.              || /Linux/          && 'l/Linux.html'
  533.              || /HP-UX/          && 'h/HP-SUX.html'
  534.              || /SunOS/          && 's/ScumOS.html'
  535.              ||                     'a/AppendixB.html';
  536.         }
  537.         print "Location: $dir/$page\015\012\015\012";
  538.  
  539.  
  540.     That kind of switch statement only works when you know the `&&'
  541.     clauses will be true. If you don't, the previous `?:' example
  542.     should be used.
  543.  
  544.     You might also consider writing a hash instead of synthesizing a
  545.     `switch' statement.
  546.  
  547.   Goto
  548.  
  549.     Although not for the faint of heart, Perl does support a `goto'
  550.     statement. A loop's LABEL is not actually a valid target for a
  551.     `goto'; it's just the name of the loop. There are three forms:
  552.     `goto'-LABEL, `goto'-EXPR, and `goto'-&NAME.
  553.  
  554.     The `goto'-LABEL form finds the statement labeled with LABEL and
  555.     resumes execution there. It may not be used to go into any
  556.     construct that requires initialization, such as a subroutine or
  557.     a `foreach' loop. It also can't be used to go into a construct
  558.     that is optimized away. It can be used to go almost anywhere
  559.     else within the dynamic scope, including out of subroutines, but
  560.     it's usually better to use some other construct such as `last'
  561.     or `die'. The author of Perl has never felt the need to use this
  562.     form of `goto' (in Perl, that is--C is another matter).
  563.  
  564.     The `goto'-EXPR form expects a label name, whose scope will be
  565.     resolved dynamically. This allows for computed `goto's per
  566.     FORTRAN, but isn't necessarily recommended if you're optimizing
  567.     for maintainability:
  568.  
  569.         goto ("FOO", "BAR", "GLARCH")[$i];
  570.  
  571.  
  572.     The `goto'-&NAME form is highly magical, and substitutes a call
  573.     to the named subroutine for the currently running subroutine.
  574.     This is used by `AUTOLOAD()' subroutines that wish to load
  575.     another subroutine and then pretend that the other subroutine
  576.     had been called in the first place (except that any
  577.     modifications to `@_' in the current subroutine are propagated
  578.     to the other subroutine.) After the `goto', not even `caller()'
  579.     will be able to tell that this routine was called first.
  580.  
  581.     In almost all cases like this, it's usually a far, far better
  582.     idea to use the structured control flow mechanisms of `next',
  583.     `last', or `redo' instead of resorting to a `goto'. For certain
  584.     applications, the catch and throw pair of `eval{}' and die() for
  585.     exception processing can also be a prudent approach.
  586.  
  587.   PODs: Embedded Documentation
  588.  
  589.     Perl has a mechanism for intermixing documentation with source
  590.     code. While it's expecting the beginning of a new statement, if
  591.     the compiler encounters a line that begins with an equal sign
  592.     and a word, like this
  593.  
  594.         =head1 Here There Be Pods!
  595.  
  596.  
  597.     Then that text and all remaining text up through and including a
  598.     line beginning with `=cut' will be ignored. The format of the
  599.     intervening text is described in the perlpod manpage.
  600.  
  601.     This allows you to intermix your source code and your
  602.     documentation text freely, as in
  603.  
  604.         =item snazzle($)
  605.  
  606.         The snazzle() function will behave in the most spectacular
  607.         form that you can possibly imagine, not even excepting
  608.         cybernetic pyrotechnics.
  609.  
  610.         =cut back to the compiler, nuff of this pod stuff!
  611.  
  612.         sub snazzle($) {
  613.         my $thingie = shift;
  614.         .........
  615.         }
  616.  
  617.  
  618.     Note that pod translators should look at only paragraphs
  619.     beginning with a pod directive (it makes parsing easier),
  620.     whereas the compiler actually knows to look for pod escapes even
  621.     in the middle of a paragraph. This means that the following
  622.     secret stuff will be ignored by both the compiler and the
  623.     translators.
  624.  
  625.         $a=3;
  626.         =secret stuff
  627.          warn "Neither POD nor CODE!?"
  628.         =cut back
  629.         print "got $a\n";
  630.  
  631.  
  632.     You probably shouldn't rely upon the `warn()' being podded out
  633.     forever. Not all pod translators are well-behaved in this
  634.     regard, and perhaps the compiler will become pickier.
  635.  
  636.     One may also use pod directives to quickly comment out a section
  637.     of code.
  638.  
  639.   Plain Old Comments (Not!)
  640.  
  641.     Much like the C preprocessor, Perl can process line directives.
  642.     Using this, one can control Perl's idea of filenames and line
  643.     numbers in error or warning messages (especially for strings
  644.     that are processed with `eval()'). The syntax for this mechanism
  645.     is the same as for most C preprocessors: it matches the regular
  646.     expression `/^#\s*line\s+(\d+)\s*(?:\s"([^"]*)")?/' with `$1'
  647.     being the line number for the next line, and `$2' being the
  648.     optional filename (specified within quotes).
  649.  
  650.     Here are some examples that you should be able to type into your
  651.     command shell:
  652.  
  653.         % perl
  654.         # line 200 "bzzzt"
  655.         # the `#' on the previous line must be the first char on line
  656.         die 'foo';
  657.         __END__
  658.         foo at bzzzt line 201.
  659.  
  660.         % perl
  661.         # line 200 "bzzzt"
  662.         eval qq[\n#line 2001 ""\ndie 'foo']; print $@;
  663.         __END__
  664.         foo at - line 2001.
  665.  
  666.         % perl
  667.         eval qq[\n#line 200 "foo bar"\ndie 'foo']; print $@;
  668.         __END__
  669.         foo at foo bar line 200.
  670.  
  671.         % perl
  672.         # line 345 "goop"
  673.         eval "\n#line " . __LINE__ . ' "' . __FILE__ ."\"\ndie 'foo'";
  674.         print $@;
  675.         __END__
  676.         foo at goop line 345.
  677.  
  678.