home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Magazin: Amiga-CD 1996 July
/
AMIGA_1996_7.BIN
/
ausgabe_7_96
/
pd-programmierung
/
perl5_002bin.lha
/
man
/
catp
/
perlsyn.0
< prev
next >
Wrap
Text File
|
1996-03-02
|
43KB
|
727 lines
PERLSYN(1) User Contributed Perl Documentation PERLSYN(1)
NNNNAAAAMMMMEEEE
perlsyn - Perl syntax
DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
A Perl script consists of a sequence of declarations and
statements. The only things that need to be declared in
Perl are report formats and subroutines. See the sections
below for more information on those declarations. All
uninitialized user-created objects are assumed to start
with a null or 0 value until they are defined by some
explicit operation such as assignment. (Though you can
get warnings about the use of undefined values if you
like.) The sequence of statements is executed just once,
unlike in sssseeeedddd and aaaawwwwkkkk scripts, where the sequence of
statements is executed for each input line. While this
means that you must explicitly loop over the lines of your
input file (or files), it also means you have much more
control over which files and which lines you look at.
(Actually, I'm lying--it is possible to do an implicit
loop with either the ----nnnn or ----pppp switch. It's just not the
mandatory default like it is in sssseeeedddd and aaaawwwwkkkk.)
DDDDeeeeccccllllaaaarrrraaaattttiiiioooonnnnssss
Perl is, for the most part, a free-form language. (The
only exception to this is format declarations, for obvious
reasons.) Comments are indicated by the "#" character, and
extend to the end of the line. If you attempt to use ////****
****//// C-style comments, it will be interpreted either as
division or pattern matching, depending on the context,
and C++ //////// comments just look like a null regular
expression, so don't do that.
A declaration can be put anywhere a statement can, but has
no effect on the execution of the primary sequence of
statements--declarations all take effect at compile time.
Typically all the declarations are put at the beginning or
the end of the script. However, if you're using
lexically-scoped private variables created with _m_y_(_),
you'll have to make sure your format or subroutine
definition is within the same block scope as the my if you
expect to to be able to access those private variables.
Declaring a subroutine allows a subroutine name to be used
as if it were a list operator from that point forward in
the program. You can declare a subroutine (prototyped to
take one scalar parameter) without defining it by saying
just:
ssssuuuubbbb mmmmyyyynnnnaaaammmmeeee (((($$$$))));;;;
$$$$mmmmeeee ==== mmmmyyyynnnnaaaammmmeeee $$$$0000 oooorrrr ddddiiiieeee """"ccccaaaannnn''''tttt ggggeeeetttt mmmmyyyynnnnaaaammmmeeee"""";;;;
Note that it functions as a list operator though, not as a
unary operator, so be careful to use oooorrrr instead of ||||||||
30/Jan/96 perl 5.002 with 1
PERLSYN(1) User Contributed Perl Documentation PERLSYN(1)
there.
Subroutines declarations can also be loaded up with the
rrrreeeeqqqquuuuiiiirrrreeee statement or both loaded and imported into your
namespace with a uuuusssseeee statement. See the _p_e_r_l_m_o_d manpage
for details on this.
A statement sequence may contain declarations of
lexically-scoped variables, but apart from declaring a
variable name, the declaration acts like an ordinary
statement, and is elaborated within the sequence of
statements as if it were an ordinary statement. That
means it actually has both compile-time and run-time
effects.
SSSSiiiimmmmpppplllleeee ssssttttaaaatttteeeemmmmeeeennnnttttssss
The only kind of simple statement is an expression
evaluated for its side effects. Every simple statement
must be terminated with a semicolon, unless it is the
final statement in a block, in which case the semicolon is
optional. (A semicolon is still encouraged there if the
block takes up more than one line, since you may
eventually add another line.) Note that there are some
operators like eeeevvvvaaaallll {{{{}}}} and ddddoooo {{{{}}}} that look like compound
statements, but aren't (they're just TERMs in an
expression), and thus need an explicit termination if used
as the last item in a statement.
Any simple statement may optionally be followed by a
_S_I_N_G_L_E modifier, just before the terminating semicolon (or
block ending). The possible modifiers are:
iiiiffff EEEEXXXXPPPPRRRR
uuuunnnnlllleeeessssssss EEEEXXXXPPPPRRRR
wwwwhhhhiiiilllleeee EEEEXXXXPPPPRRRR
uuuunnnnttttiiiillll EEEEXXXXPPPPRRRR
The iiiiffff and uuuunnnnlllleeeessssssss modifiers have the expected semantics,
presuming you're a speaker of English. The wwwwhhhhiiiilllleeee and
uuuunnnnttttiiiillll modifiers also have the usual "while loop" semantics
(conditional evaluated first), except when applied to a
do-BLOCK (or to the now-deprecated do-SUBROUTINE
statement), in which case the block executes once before
the conditional is evaluated. This is so that you can
write loops like:
ddddoooo {{{{
$$$$lllliiiinnnneeee ==== <<<<SSSSTTTTDDDDIIIINNNN>>>>;;;;
............
}}}} uuuunnnnttttiiiillll $$$$lllliiiinnnneeee eeeeqqqq """"....\\\\nnnn"""";;;;
See the ddddoooo entry in the _p_e_r_l_f_u_n_c manpage. Note also that
the loop control statements described later will _N_O_T work
30/Jan/96 perl 5.002 with 2
PERLSYN(1) User Contributed Perl Documentation PERLSYN(1)
in this construct, since modifiers don't take loop labels.
Sorry. You can always wrap another block around it to do
that sort of thing.
CCCCoooommmmppppoooouuuunnnndddd ssssttttaaaatttteeeemmmmeeeennnnttttssss
In Perl, a sequence of statements that defines a scope is
called a block. Sometimes a block is delimited by the
file containing it (in the case of a required file, or the
program as a whole), and sometimes a block is delimited by
the extent of a string (in the case of an eval).
But generally, a block is delimited by curly brackets,
also known as braces. We will call this syntactic
construct a BLOCK.
The following compound statements may be used to control
flow:
iiiiffff ((((EEEEXXXXPPPPRRRR)))) BBBBLLLLOOOOCCCCKKKK
iiiiffff ((((EEEEXXXXPPPPRRRR)))) BBBBLLLLOOOOCCCCKKKK eeeellllsssseeee BBBBLLLLOOOOCCCCKKKK
iiiiffff ((((EEEEXXXXPPPPRRRR)))) BBBBLLLLOOOOCCCCKKKK eeeellllssssiiiiffff ((((EEEEXXXXPPPPRRRR)))) BBBBLLLLOOOOCCCCKKKK ............ eeeellllsssseeee BBBBLLLLOOOOCCCCKKKK
LLLLAAAABBBBEEEELLLL wwwwhhhhiiiilllleeee ((((EEEEXXXXPPPPRRRR)))) BBBBLLLLOOOOCCCCKKKK
LLLLAAAABBBBEEEELLLL wwwwhhhhiiiilllleeee ((((EEEEXXXXPPPPRRRR)))) BBBBLLLLOOOOCCCCKKKK ccccoooonnnnttttiiiinnnnuuuueeee BBBBLLLLOOOOCCCCKKKK
LLLLAAAABBBBEEEELLLL ffffoooorrrr ((((EEEEXXXXPPPPRRRR;;;; EEEEXXXXPPPPRRRR;;;; EEEEXXXXPPPPRRRR)))) BBBBLLLLOOOOCCCCKKKK
LLLLAAAABBBBEEEELLLL ffffoooorrrreeeeaaaacccchhhh VVVVAAAARRRR ((((LLLLIIIISSSSTTTT)))) BBBBLLLLOOOOCCCCKKKK
LLLLAAAABBBBEEEELLLL BBBBLLLLOOOOCCCCKKKK ccccoooonnnnttttiiiinnnnuuuueeee BBBBLLLLOOOOCCCCKKKK
Note that, unlike C and Pascal, these are defined in terms
of BLOCKs, not statements. This means that the curly
brackets are _r_e_q_u_i_r_e_d--no dangling statements allowed. If
you want to write conditionals without curly brackets
there are several other ways to do it. The following all
do the same thing:
iiiiffff ((((!!!!ooooppppeeeennnn((((FFFFOOOOOOOO)))))))) {{{{ ddddiiiieeee """"CCCCaaaannnn''''tttt ooooppppeeeennnn $$$$FFFFOOOOOOOO:::: $$$$!!!!"""";;;; }}}}
ddddiiiieeee """"CCCCaaaannnn''''tttt ooooppppeeeennnn $$$$FFFFOOOOOOOO:::: $$$$!!!!"""" uuuunnnnlllleeeessssssss ooooppppeeeennnn((((FFFFOOOOOOOO))));;;;
ooooppppeeeennnn((((FFFFOOOOOOOO)))) oooorrrr ddddiiiieeee """"CCCCaaaannnn''''tttt ooooppppeeeennnn $$$$FFFFOOOOOOOO:::: $$$$!!!!"""";;;; #### FFFFOOOOOOOO oooorrrr bbbbuuuusssstttt!!!!
ooooppppeeeennnn((((FFFFOOOOOOOO)))) ???? ''''hhhhiiii mmmmoooommmm'''' :::: ddddiiiieeee """"CCCCaaaannnn''''tttt ooooppppeeeennnn $$$$FFFFOOOOOOOO:::: $$$$!!!!"""";;;;
#### aaaa bbbbiiiitttt eeeexxxxoooottttiiiicccc,,,, tttthhhhaaaatttt llllaaaasssstttt oooonnnneeee
The iiiiffff statement is straightforward. Since BLOCKs are
always bounded by curly brackets, there is never any
ambiguity about which iiiiffff an eeeellllsssseeee goes with. If you use
uuuunnnnlllleeeessssssss in place of iiiiffff, the sense of the test is reversed.
The wwwwhhhhiiiilllleeee statement executes the block as long as the
expression is true (does not evaluate to the null string
or 0 or "0"). The LABEL is optional, and if present,
consists of an identifier followed by a colon. The LABEL
identifies the loop for the loop control statements nnnneeeexxxxtttt,
llllaaaasssstttt, and rrrreeeeddddoooo. If the LABEL is omitted, the loop control
statement refers to the innermost enclosing loop. This
may include dynamically looking back your call-stack at
30/Jan/96 perl 5.002 with 3
PERLSYN(1) User Contributed Perl Documentation PERLSYN(1)
run time to find the LABEL. Such desperate behavior
triggers a warning if you use the ----wwww flag.
If there is a ccccoooonnnnttttiiiinnnnuuuueeee BLOCK, it is always executed just
before the conditional is about to be evaluated again,
just like the third part of a ffffoooorrrr loop in C. Thus it can
be used to increment a loop variable, even when the loop
has been continued via the nnnneeeexxxxtttt statement (which is
similar to the C ccccoooonnnnttttiiiinnnnuuuueeee statement).
LLLLoooooooopppp CCCCoooonnnnttttrrrroooollll
The nnnneeeexxxxtttt command is like the ccccoooonnnnttttiiiinnnnuuuueeee statement in C; it
starts the next iteration of the loop:
LLLLIIIINNNNEEEE:::: wwwwhhhhiiiilllleeee ((((<<<<SSSSTTTTDDDDIIIINNNN>>>>)))) {{{{
nnnneeeexxxxtttt LLLLIIIINNNNEEEE iiiiffff ////^^^^####////;;;; #### ddddiiiissssccccaaaarrrrdddd ccccoooommmmmmmmeeeennnnttttssss
............
}}}}
The llllaaaasssstttt command is like the bbbbrrrreeeeaaaakkkk statement in C (as used
in loops); it immediately exits the loop in question. The
ccccoooonnnnttttiiiinnnnuuuueeee block, if any, is not executed:
LLLLIIIINNNNEEEE:::: wwwwhhhhiiiilllleeee ((((<<<<SSSSTTTTDDDDIIIINNNN>>>>)))) {{{{
llllaaaasssstttt LLLLIIIINNNNEEEE iiiiffff ////^^^^$$$$////;;;; #### eeeexxxxiiiitttt wwwwhhhheeeennnn ddddoooonnnneeee wwwwiiiitttthhhh hhhheeeeaaaaddddeeeerrrr
............
}}}}
The rrrreeeeddddoooo command restarts the loop block without
evaluating the conditional again. The ccccoooonnnnttttiiiinnnnuuuueeee block, if
any, is _n_o_t executed. This command is normally used by
programs that want to lie to themselves about what was
just input.
For example, when processing a file like _/_e_t_c_/_t_e_r_m_c_a_p. If
your input lines might end in backslashes to indicate
continuation, you want to skip ahead and get the next
record.
wwwwhhhhiiiilllleeee ((((<<<<>>>>)))) {{{{
cccchhhhoooommmmpppp;;;;
iiiiffff ((((ssss////\\\\\\\\$$$$////////)))) {{{{
$$$$____ ....==== <<<<>>>>;;;;
rrrreeeeddddoooo uuuunnnnlllleeeessssssss eeeeooooffff(((())));;;;
}}}}
#### nnnnoooowwww pppprrrroooocccceeeessssssss $$$$____
}}}}
which is Perl short-hand for the more explicitly written
version:
30/Jan/96 perl 5.002 with 4
PERLSYN(1) User Contributed Perl Documentation PERLSYN(1)
LLLLIIIINNNNEEEE:::: wwwwhhhhiiiilllleeee (((($$$$lllliiiinnnneeee ==== <<<<AAAARRRRGGGGVVVV>>>>)))) {{{{
cccchhhhoooommmmpppp(((($$$$lllliiiinnnneeee))));;;;
iiiiffff (((($$$$lllliiiinnnneeee ====~~~~ ssss////\\\\\\\\$$$$////////)))) {{{{
$$$$lllliiiinnnneeee ....==== <<<<AAAARRRRGGGGVVVV>>>>;;;;
rrrreeeeddddoooo LLLLIIIINNNNEEEE uuuunnnnlllleeeessssssss eeeeooooffff(((())));;;; #### nnnnooootttt eeeeooooffff((((AAAARRRRGGGGVVVV))))!!!!
}}}}
#### nnnnoooowwww pppprrrroooocccceeeessssssss $$$$lllliiiinnnneeee
}}}}
Or here's a a simpleminded Pascal comment stripper
(warning: assumes no { or } in strings)
LLLLIIIINNNNEEEE:::: wwwwhhhhiiiilllleeee ((((<<<<SSSSTTTTDDDDIIIINNNN>>>>)))) {{{{
wwwwhhhhiiiilllleeee ((((ssss||||(((({{{{....****}}}}....****)))){{{{....****}}}}||||$$$$1111 ||||)))) {{{{}}}}
ssss||||{{{{....****}}}}|||| ||||;;;;
iiiiffff ((((ssss||||{{{{....****|||| ||||)))) {{{{
$$$$ffffrrrroooonnnntttt ==== $$$$____;;;;
wwwwhhhhiiiilllleeee ((((<<<<SSSSTTTTDDDDIIIINNNN>>>>)))) {{{{
iiiiffff ((((////}}}}////)))) {{{{ #### eeeennnndddd ooooffff ccccoooommmmmmmmeeeennnntttt????
ssss||||^^^^||||$$$$ffffrrrroooonnnntttt{{{{||||;;;;
rrrreeeeddddoooo LLLLIIIINNNNEEEE;;;;
}}}}
}}}}
}}}}
pppprrrriiiinnnntttt;;;;
}}}}
Note that if there were a ccccoooonnnnttttiiiinnnnuuuueeee block on the above
code, it would get executed even on discarded lines.
If the word wwwwhhhhiiiilllleeee is replaced by the word uuuunnnnttttiiiillll, the sense
of the test is reversed, but the conditional is still
tested before the first iteration.
In either the iiiiffff or the wwwwhhhhiiiilllleeee statement, you may replace
"(EXPR)" with a BLOCK, and the conditional is true if the
value of the last statement in that block is true. While
this "feature" continues to work in version 5, it has been
deprecated, so please change any occurrences of "if BLOCK"
to "if (do BLOCK)".
FFFFoooorrrr LLLLooooooooppppssss
Perl's C-style ffffoooorrrr loop works exactly like the
corresponding wwwwhhhhiiiilllleeee loop; that means that this:
ffffoooorrrr (((($$$$iiii ==== 1111;;;; $$$$iiii <<<< 11110000;;;; $$$$iiii++++++++)))) {{{{
............
}}}}
is the same as this:
30/Jan/96 perl 5.002 with 5
PERLSYN(1) User Contributed Perl Documentation PERLSYN(1)
$$$$iiii ==== 1111;;;;
wwwwhhhhiiiilllleeee (((($$$$iiii <<<< 11110000)))) {{{{
............
}}}} ccccoooonnnnttttiiiinnnnuuuueeee {{{{
$$$$iiii++++++++;;;;
}}}}
Besides the normal array index looping, ffffoooorrrr can lend
itself to many other interesting applications. Here's one
that avoids the problem you get into if you explicitly
test for end-of-file on an interactive file descriptor
causing your program to appear to hang.
$$$$oooonnnn____aaaa____ttttttttyyyy ==== ----tttt SSSSTTTTDDDDIIIINNNN &&&&&&&& ----tttt SSSSTTTTDDDDOOOOUUUUTTTT;;;;
ssssuuuubbbb pppprrrroooommmmpppptttt {{{{ pppprrrriiiinnnntttt """"yyyyeeeessss???? """" iiiiffff $$$$oooonnnn____aaaa____ttttttttyyyy }}}}
ffffoooorrrr (((( pppprrrroooommmmpppptttt(((())));;;; <<<<SSSSTTTTDDDDIIIINNNN>>>>;;;; pppprrrroooommmmpppptttt(((()))) )))) {{{{
#### ddddoooo ssssoooommmmeeeetttthhhhiiiinnnngggg
}}}}
FFFFoooorrrreeeeaaaacccchhhh LLLLooooooooppppssss
The ffffoooorrrreeeeaaaacccchhhh loop iterates over a normal list value and
sets the variable VAR to be each element of the list in
turn. The variable is implicitly local to the loop and
regains its former value upon exiting the loop. If the
variable was previously declared with mmmmyyyy, it uses that
variable instead of the global one, but it's still
localized to the loop. This can cause problems if you
have subroutine or format declarations within that block's
scope.
The ffffoooorrrreeeeaaaacccchhhh keyword is actually a synonym for the ffffoooorrrr
keyword, so you can use ffffoooorrrreeeeaaaacccchhhh for readability or ffffoooorrrr for
brevity. If VAR is omitted, $$$$____ is set to each value. If
LIST is an actual array (as opposed to an expression
returning a list value), you can modify each element of
the array by modifying VAR inside the loop. That's
because the ffffoooorrrreeeeaaaacccchhhh loop index variable is an implicit
alias for each item in the list that you're looping over.
Examples:
ffffoooorrrr ((((@@@@aaaarrrryyyy)))) {{{{ ssss////ffffoooooooo////bbbbaaaarrrr//// }}}}
ffffoooorrrreeeeaaaacccchhhh $$$$eeeelllleeeemmmm ((((@@@@eeeelllleeeemmmmeeeennnnttttssss)))) {{{{
$$$$eeeelllleeeemmmm ****==== 2222;;;;
}}}}
ffffoooorrrr $$$$ccccoooouuuunnnntttt ((((11110000,,,,9999,,,,8888,,,,7777,,,,6666,,,,5555,,,,4444,,,,3333,,,,2222,,,,1111,,,,''''BBBBOOOOOOOOMMMM'''')))) {{{{
pppprrrriiiinnnntttt $$$$ccccoooouuuunnnntttt,,,, """"\\\\nnnn"""";;;; sssslllleeeeeeeepppp((((1111))));;;;
}}}}
ffffoooorrrr ((((1111........11115555)))) {{{{ pppprrrriiiinnnntttt """"MMMMeeeerrrrrrrryyyy CCCChhhhrrrriiiissssttttmmmmaaaassss\\\\nnnn"""";;;; }}}}
30/Jan/96 perl 5.002 with 6
PERLSYN(1) User Contributed Perl Documentation PERLSYN(1)
ffffoooorrrreeeeaaaacccchhhh $$$$iiiitttteeeemmmm ((((sssspppplllliiiitttt((((////::::[[[[\\\\\\\\\\\\nnnn::::]]]]****////,,,, $$$$EEEENNNNVVVV{{{{TTTTEEEERRRRMMMMCCCCAAAAPPPP}}}})))))))) {{{{
pppprrrriiiinnnntttt """"IIIItttteeeemmmm:::: $$$$iiiitttteeeemmmm\\\\nnnn"""";;;;
}}}}
Here's how a C programmer might code up a particular
algorithm in Perl:
ffffoooorrrr (((($$$$iiii ==== 0000;;;; $$$$iiii <<<< @@@@aaaarrrryyyy1111;;;; $$$$iiii++++++++)))) {{{{
ffffoooorrrr (((($$$$jjjj ==== 0000;;;; $$$$jjjj <<<< @@@@aaaarrrryyyy2222;;;; $$$$jjjj++++++++)))) {{{{
iiiiffff (((($$$$aaaarrrryyyy1111[[[[$$$$iiii]]]] >>>> $$$$aaaarrrryyyy2222[[[[$$$$jjjj]]]])))) {{{{
llllaaaasssstttt;;;; #### ccccaaaannnn''''tttt ggggoooo ttttoooo oooouuuutttteeeerrrr ::::----((((
}}}}
$$$$aaaarrrryyyy1111[[[[$$$$iiii]]]] ++++==== $$$$aaaarrrryyyy2222[[[[$$$$jjjj]]]];;;;
}}}}
#### tttthhhhiiiissss iiiissss wwwwhhhheeeerrrreeee tttthhhhaaaatttt llllaaaasssstttt ttttaaaakkkkeeeessss mmmmeeee
}}}}
Whereas here's how a Perl programmer more confortable with
the idiom might do it:
OOOOUUUUTTTTEEEERRRR:::: ffffoooorrrreeeeaaaacccchhhh $$$$wwwwiiiidddd ((((@@@@aaaarrrryyyy1111)))) {{{{
IIIINNNNNNNNEEEERRRR:::: ffffoooorrrreeeeaaaacccchhhh $$$$jjjjeeeetttt ((((@@@@aaaarrrryyyy2222)))) {{{{
nnnneeeexxxxtttt OOOOUUUUTTTTEEEERRRR iiiiffff $$$$wwwwiiiidddd >>>> $$$$jjjjeeeetttt;;;;
$$$$wwwwiiiidddd ++++==== $$$$jjjjeeeetttt;;;;
}}}}
}}}}
See how much easier this is? It's cleaner, safer, and
faster. It's cleaner because it's less noisy. It's safer
because if code gets added between the inner and outer
loops later on, the new code won't be accidentally
excecuted: the nnnneeeexxxxtttt explicitly iterates the other loop
rather than merely terminating the inner one. And it's
faster because Perl executes a ffffoooorrrreeeeaaaacccchhhh statement more
rapidly than it would the equivalent ffffoooorrrr loop.
BBBBaaaassssiiiicccc BBBBLLLLOOOOCCCCKKKKssss aaaannnndddd SSSSwwwwiiiittttcccchhhh SSSSttttaaaatttteeeemmmmeeeennnnttttssss
A BLOCK by itself (labeled or not) is semantically
equivalent to a loop that executes once. Thus you can use
any of the loop control statements in it to leave or
restart the block. (Note that this is _N_O_T true in eeeevvvvaaaallll{{{{}}}},
ssssuuuubbbb{{{{}}}}, or contrary to popular belief ddddoooo{{{{}}}} blocks, which do
_N_O_T count as loops.) The ccccoooonnnnttttiiiinnnnuuuueeee block is optional.
The BLOCK construct is particularly nice for doing case
structures.
SSSSWWWWIIIITTTTCCCCHHHH:::: {{{{
iiiiffff ((((////^^^^aaaabbbbcccc////)))) {{{{ $$$$aaaabbbbcccc ==== 1111;;;; llllaaaasssstttt SSSSWWWWIIIITTTTCCCCHHHH;;;; }}}}
iiiiffff ((((////^^^^ddddeeeeffff////)))) {{{{ $$$$ddddeeeeffff ==== 1111;;;; llllaaaasssstttt SSSSWWWWIIIITTTTCCCCHHHH;;;; }}}}
iiiiffff ((((////^^^^xxxxyyyyzzzz////)))) {{{{ $$$$xxxxyyyyzzzz ==== 1111;;;; llllaaaasssstttt SSSSWWWWIIIITTTTCCCCHHHH;;;; }}}}
$$$$nnnnooootttthhhhiiiinnnngggg ==== 1111;;;;
}}}}
30/Jan/96 perl 5.002 with 7
PERLSYN(1) User Contributed Perl Documentation PERLSYN(1)
There is no official switch statement in Perl, because
there are already several ways to write the equivalent.
In addition to the above, you could write
SSSSWWWWIIIITTTTCCCCHHHH:::: {{{{
$$$$aaaabbbbcccc ==== 1111,,,, llllaaaasssstttt SSSSWWWWIIIITTTTCCCCHHHH iiiiffff ////^^^^aaaabbbbcccc////;;;;
$$$$ddddeeeeffff ==== 1111,,,, llllaaaasssstttt SSSSWWWWIIIITTTTCCCCHHHH iiiiffff ////^^^^ddddeeeeffff////;;;;
$$$$xxxxyyyyzzzz ==== 1111,,,, llllaaaasssstttt SSSSWWWWIIIITTTTCCCCHHHH iiiiffff ////^^^^xxxxyyyyzzzz////;;;;
$$$$nnnnooootttthhhhiiiinnnngggg ==== 1111;;;;
}}}}
(That's actually not as strange as it looks once you
realize that you can use loop control "operators" within
an expression, That's just the normal C comma operator.)
or
SSSSWWWWIIIITTTTCCCCHHHH:::: {{{{
////^^^^aaaabbbbcccc//// &&&&&&&& ddddoooo {{{{ $$$$aaaabbbbcccc ==== 1111;;;; llllaaaasssstttt SSSSWWWWIIIITTTTCCCCHHHH;;;; }}}};;;;
////^^^^ddddeeeeffff//// &&&&&&&& ddddoooo {{{{ $$$$ddddeeeeffff ==== 1111;;;; llllaaaasssstttt SSSSWWWWIIIITTTTCCCCHHHH;;;; }}}};;;;
////^^^^xxxxyyyyzzzz//// &&&&&&&& ddddoooo {{{{ $$$$xxxxyyyyzzzz ==== 1111;;;; llllaaaasssstttt SSSSWWWWIIIITTTTCCCCHHHH;;;; }}}};;;;
$$$$nnnnooootttthhhhiiiinnnngggg ==== 1111;;;;
}}}}
or formatted so it stands out more as a "proper" switch
statement:
SSSSWWWWIIIITTTTCCCCHHHH:::: {{{{
////^^^^aaaabbbbcccc//// &&&&&&&& ddddoooo {{{{
$$$$aaaabbbbcccc ==== 1111;;;;
llllaaaasssstttt SSSSWWWWIIIITTTTCCCCHHHH;;;;
}}}};;;;
////^^^^ddddeeeeffff//// &&&&&&&& ddddoooo {{{{
$$$$ddddeeeeffff ==== 1111;;;;
llllaaaasssstttt SSSSWWWWIIIITTTTCCCCHHHH;;;;
}}}};;;;
////^^^^xxxxyyyyzzzz//// &&&&&&&& ddddoooo {{{{
$$$$xxxxyyyyzzzz ==== 1111;;;;
llllaaaasssstttt SSSSWWWWIIIITTTTCCCCHHHH;;;;
}}}};;;;
$$$$nnnnooootttthhhhiiiinnnngggg ==== 1111;;;;
}}}}
or
SSSSWWWWIIIITTTTCCCCHHHH:::: {{{{
////^^^^aaaabbbbcccc//// aaaannnndddd $$$$aaaabbbbcccc ==== 1111,,,, llllaaaasssstttt SSSSWWWWIIIITTTTCCCCHHHH;;;;
////^^^^ddddeeeeffff//// aaaannnndddd $$$$ddddeeeeffff ==== 1111,,,, llllaaaasssstttt SSSSWWWWIIIITTTTCCCCHHHH;;;;
////^^^^xxxxyyyyzzzz//// aaaannnndddd $$$$xxxxyyyyzzzz ==== 1111,,,, llllaaaasssstttt SSSSWWWWIIIITTTTCCCCHHHH;;;;
$$$$nnnnooootttthhhhiiiinnnngggg ==== 1111;;;;
}}}}
30/Jan/96 perl 5.002 with 8
PERLSYN(1) User Contributed Perl Documentation PERLSYN(1)
or even, horrors,
iiiiffff ((((////^^^^aaaabbbbcccc////))))
{{{{ $$$$aaaabbbbcccc ==== 1111 }}}}
eeeellllssssiiiiffff ((((////^^^^ddddeeeeffff////))))
{{{{ $$$$ddddeeeeffff ==== 1111 }}}}
eeeellllssssiiiiffff ((((////^^^^xxxxyyyyzzzz////))))
{{{{ $$$$xxxxyyyyzzzz ==== 1111 }}}}
eeeellllsssseeee
{{{{ $$$$nnnnooootttthhhhiiiinnnngggg ==== 1111 }}}}
A common idiom for a switch statement is to use ffffoooorrrreeeeaaaacccchhhh's
aliasing to make a temporary assignment to $$$$____ for
convenient matching:
SSSSWWWWIIIITTTTCCCCHHHH:::: ffffoooorrrr (((($$$$wwwwhhhheeeerrrreeee)))) {{{{
////IIIInnnn CCCCaaaarrrrdddd NNNNaaaammmmeeeessss//// &&&&&&&& ddddoooo {{{{ ppppuuuusssshhhh @@@@ffffllllaaaaggggssss,,,, ''''----eeee'''';;;; llllaaaasssstttt;;;; }}}};;;;
////AAAAnnnnyyyywwwwhhhheeeerrrreeee//// &&&&&&&& ddddoooo {{{{ ppppuuuusssshhhh @@@@ffffllllaaaaggggssss,,,, ''''----hhhh'''';;;; llllaaaasssstttt;;;; }}}};;;;
////IIIInnnn RRRRuuuulllliiiinnnnggggssss//// &&&&&&&& ddddoooo {{{{ llllaaaasssstttt;;;; }}}};;;;
ddddiiiieeee """"uuuunnnnkkkknnnnoooowwwwnnnn vvvvaaaalllluuuueeee ffffoooorrrr ffffoooorrrrmmmm vvvvaaaarrrriiiiaaaabbbblllleeee wwwwhhhheeeerrrreeee:::: ````$$$$wwwwhhhheeeerrrreeee''''"""";;;;
}}}}
Another interesting approach to a switch statement is
arrange for a ddddoooo block to return the proper value:
$$$$aaaammmmooooddddeeee ==== ddddoooo {{{{
iiiiffff (((($$$$ffffllllaaaagggg &&&& OOOO____RRRRDDDDOOOONNNNLLLLYYYY)))) {{{{ """"rrrr"""" }}}}
eeeellllssssiiiiffff (((($$$$ffffllllaaaagggg &&&& OOOO____WWWWRRRROOOONNNNLLLLYYYY)))) {{{{ (((($$$$ffffllllaaaagggg &&&& OOOO____AAAAPPPPPPPPEEEENNNNDDDD)))) ???? """"aaaa"""" :::: """"wwww"""" }}}}
eeeellllssssiiiiffff (((($$$$ffffllllaaaagggg &&&& OOOO____RRRRDDDDWWWWRRRR)))) {{{{
iiiiffff (((($$$$ffffllllaaaagggg &&&& OOOO____CCCCRRRREEEEAAAATTTT)))) {{{{ """"wwww++++"""" }}}}
eeeellllsssseeee {{{{ (((($$$$ffffllllaaaagggg &&&& OOOO____AAAAPPPPPPPPEEEENNNNDDDD)))) ???? """"aaaa++++"""" :::: """"rrrr++++"""" }}}}
}}}}
}}}};;;;
GGGGoooottttoooo
Although not for the faint of heart, Perl does support a
ggggoooottttoooo statement. A loop's LABEL is not actually a valid
target for a ggggoooottttoooo; it's just the name of the loop. There
are three forms: goto-LABEL, goto-EXPR, and goto-&NAME.
The goto-LABEL form finds the statement labeled with LABEL
and resumes execution there. It may not be used to go
into any construct that requires initialization, such as a
subroutine or a foreach loop. It also can't be used to go
into a construct that is optimized away. It can be used
to go almost anywhere else within the dynamic scope,
including out of subroutines, but it's usually better to
use some other construct such as last or die. The author
of Perl has never felt the need to use this form of goto
(in Perl, that is--C is another matter).
The goto-EXPR form expects a label name, whose scope will
30/Jan/96 perl 5.002 with 9
PERLSYN(1) User Contributed Perl Documentation PERLSYN(1)
be resolved dynamically. This allows for computed gotos
per FORTRAN, but isn't necessarily recommended if you're
optimizing for maintainability:
ggggoooottttoooo ((((""""FFFFOOOOOOOO"""",,,, """"BBBBAAAARRRR"""",,,, """"GGGGLLLLAAAARRRRCCCCHHHH""""))))[[[[$$$$iiii]]]];;;;
The goto-&NAME form is highly magical, and substitutes a
call to the named subroutine for the currently running
subroutine. This is used by _A_U_T_O_L_O_A_D_(_) subroutines that
wish to load another subroutine and then pretend that the
other subroutine had been called in the first place
(except that any modifications to @@@@____ in the current
subroutine are propagated to the other subroutine.) After
the ggggoooottttoooo, not even _c_a_l_l_e_r_(_) will be able to tell that this
routine was called first.
In almost all cases like this, it's usually a far, far
better idea to use the structured control flow mechanisms
of nnnneeeexxxxtttt, llllaaaasssstttt, or rrrreeeeddddoooo instead of resorting to a ggggoooottttoooo.
For certain applications, the catch and throw pair of
eeeevvvvaaaallll{{{{}}}} and _d_i_e_(_) for exception processing can also be a
prudent approach.
PPPPOOOODDDDssss:::: EEEEmmmmbbbbeeeeddddddddeeeedddd DDDDooooccccuuuummmmeeeennnnttttaaaattttiiiioooonnnn
Perl has a mechanism for intermixing documentation with
source code. While it's expecting the beginning of a new
statement, if the compiler encounters a line that begins
with an equal sign and a word, like this
====hhhheeeeaaaadddd1111 HHHHeeeerrrreeee TTTThhhheeeerrrreeee BBBBeeee PPPPooooddddssss!!!!
Then that text and all remaining text up through and
including a line beginning with ====ccccuuuutttt will be ignored. The
format of the intervening text is described in the _p_e_r_l_p_o_d
manpage.
This allows you to intermix your source code and your
documentation text freely, as in
====iiiitttteeeemmmm ssssnnnnaaaazzzzzzzzlllleeee(((($$$$))))
TTTThhhheeee ssssnnnnaaaazzzzzzzzlllleeee(((()))) ffffuuuunnnnccccttttiiiioooonnnn wwwwiiiillllllll bbbbeeeehhhhaaaavvvveeee iiiinnnn tttthhhheeee mmmmoooosssstttt ssssppppeeeeccccttttaaaaccccuuuullllaaaarrrr
ffffoooorrrrmmmm tttthhhhaaaatttt yyyyoooouuuu ccccaaaannnn ppppoooossssssssiiiibbbbllllyyyy iiiimmmmaaaaggggiiiinnnneeee,,,, nnnnooootttt eeeevvvveeeennnn eeeexxxxcccceeeeppppttttiiiinnnngggg
ccccyyyybbbbeeeerrrrnnnneeeettttiiiicccc ppppyyyyrrrrooootttteeeecccchhhhnnnniiiiccccssss....
====ccccuuuutttt bbbbaaaacccckkkk ttttoooo tttthhhheeee ccccoooommmmppppiiiilllleeeerrrr,,,, nnnnuuuuffffffff ooooffff tttthhhhiiiissss ppppoooodddd ssssttttuuuuffffffff!!!!
ssssuuuubbbb ssssnnnnaaaazzzzzzzzlllleeee(((($$$$)))) {{{{
mmmmyyyy $$$$tttthhhhiiiinnnnggggiiiieeee ==== sssshhhhiiiifffftttt;;;;
....................................
}}}}
Note that pod translators should only look at paragraphs
30/Jan/96 perl 5.002 with 10
PERLSYN(1) User Contributed Perl Documentation PERLSYN(1)
beginning with a pod diretive (it makes parsing easier),
whereas the compiler actually knows to look for pod
escapes even in the middle of a paragraph. This means
that the following secret stuff will be ignored by both
the compiler and the translators.
$$$$aaaa====3333;;;;
====sssseeeeccccrrrreeeetttt ssssttttuuuuffffffff
wwwwaaaarrrrnnnn """"NNNNeeeeiiiitttthhhheeeerrrr PPPPOOOODDDD nnnnoooorrrr CCCCOOOODDDDEEEE!!!!????""""
====ccccuuuutttt bbbbaaaacccckkkk
pppprrrriiiinnnntttt """"ggggooootttt $$$$aaaa\\\\nnnn"""";;;;
You probably shouldn't rely upon the _w_a_r_n_(_) being podded
out forever. Not all pod translators are well-behaved in
this regard, and perhaps the compiler will become pickier.
30/Jan/96 perl 5.002 with 11