Let's start at the end

What does the END statement do? Let's add some space and explanations:

END {                             # perltidy isn't perfect yet
    $00 =~ s/ (.*)(\n)            # Fetch the whole line (except \n)
            / ( ++$* %            # Increment the line counter and
                ( @ARGV ? 1 : 2 ) # check for parity if @ARGV empty
               ? reverse $1       # Reverse if $* odd (even line-number)
               : $1 )             # Otherwise, keep the line unchanged
              . $2                # Don't forget to keep the end-of-line
            /gex;                 # /x for demonstration purpose only
    eval$00                       # eval the whole thing
}
$00=<<'OPC 5'                     # the program starts here (line 2)

Explanation: If there are no command-line parameters, the even lines are reversed. In this case, we have a boustrophedon. If there are command-line parameters, the program doesn't change. Then the (possibly modified) program is evaled. We really have two programs, with slightly different behaviors.

Previous | Next


Copyright © 2000-2001, Philippe "BooK" Bruhat.