Front page | perl.perl5.porters |
Postings from May 2003
[PATCH] perlsyn.pod Revamp (take 3)
Thread Previous
|
Thread Next
From:
Shlomi Fish
Date:
May 13, 2003 00:53
Subject:
[PATCH] perlsyn.pod Revamp (take 3)
Message ID:
Pine.LNX.4.33L2.0305131051580.20214-200000@vipe.technion.ac.il
--- orig/perlsyn.pod Tue May 13 10:15:46 2003
+++ pod/perlsyn.pod Tue May 13 10:47:56 2003
@@ -5,28 +5,32 @@
=head1 DESCRIPTION
A Perl script consists of a sequence of declarations and statements.
-The sequence of statements is executed just once, unlike in B<sed>
+The statements are executed one by one and the entire sequence of
+statements is executed just once, unlike in B<sed>
and B<awk> 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 B<-n> or B<-p> switch. It's just not the mandatory
-default like it is in B<sed> and B<awk>.)
+It is possible to emulate the B<awk> and B<sed behaviour using the
+B<-n> or B<-p> switches. However, the default is to follow the
+standard of traditional imperative languages.
Perl is, for the most part, a free-form language. (The only exception
to this is format declarations, for obvious reasons.) Text from a
C<"#"> character until the end of the line is a comment, and is
-ignored. If you attempt to use C</* */> C-style comments, it will be
-interpreted either as division or pattern matching, depending on the
-context, and C++ C<//> comments just look like a null regular
-expression or defined-or operator, so don't do that.
+ignored. C-style C</* */> or C++ C<//> comments will do something
+else entirely, so don't use them. If you attempt to use C</* */> C-style
+comments, it will be interpreted either as division or pattern matching,
+depending on the context, and C++ C<//> comments just look like a null
+regular expression or defined-or operator, so don't do that.
=head2 Declarations
The only things you need to declare in Perl are report formats
-and subroutines--and even undefined subroutines can be handled
-through AUTOLOAD. A variable holds the undefined value (C<undef>)
+and subroutines. (Undefined subroutines can be handled
+through the AUTOLOAD feature as documented in L<perlsub>)
+
+A variable holds the undefined value (C<undef>)
until it has been assigned a defined value, which is anything
other than C<undef>. When used as a number, C<undef> is treated
as C<0>; when used as a string, it is treated the empty string,
@@ -100,12 +104,18 @@
until EXPR
foreach EXPR
-The C<if> and C<unless> modifiers have the expected semantics,
-presuming you're a speaker of English. The C<foreach> modifier is an
-iterator: For each value in EXPR, it aliases C<$_> to the value and
-executes the statement. The C<while> and C<until> modifiers have the
-usual "C<while> loop" semantics (conditional evaluated first), except
-when applied to a C<do>-BLOCK (or to the deprecated C<do>-SUBROUTINE
+C<if> executes a the statement once if C<EXPR> evaluated to a true value.
+C<unless> executes the statement once if the condition (C<EXPR>) did not.
+A false value is C<undef>, 0, the empty string, and the floating number
+0.0. A true value is everything else. Normally, you'll wish to use
+a mechanism that explicitly return a true or false value as a
+boolean expression there.
+
+C<while> and C<until> repeat the same block of statements while or
+until the condition is true. (I.e.: C<until> iterates while it is
+false) The C<while> and C<until> modifiers have the usual
+"C<while> loop" semantics (conditional evaluated first), except
+when applied to a C<do>-BLOCK (or to the deprecated C<do>-SUBROUTINE
statement), in which case the block executes once before the
conditional is evaluated. This is so that you can write loops like:
@@ -141,6 +151,10 @@
it. Future versions of perl might do something different from the
version of perl you try it out on. Here be dragons.
+Finally, the C<foreach> modifier is an iterator: For each value
+in EXPR, it aliases the special "default" variable C<$_> to the
+value and executes the statement.
+
=head2 Compound statements
In Perl, a sequence of statements that defines a scope is called a block.
@@ -558,7 +572,8 @@
The C<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 C<foreach> loop. It
-also can't be used to go into a construct that is optimized away. It
+also can't be used to go into a construct that is optimized away at
+the compilation stage, like an C<if (0) { ... }> block. 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 C<last> or C<die>. The author of Perl has never felt the
Thread Previous
|
Thread Next