in reply to What is an ordinary statement?
Questions
Answers:
The answers are a bit terse and might need further explanation.
Ad 1, a perl expression (EXPR) may be a simple expression or a compound expression. The moment for an expression to be evaluated is marked with a semicolon (;) at the end of the expression, which makes it into a statement. Regarding side effect - take this simple statement:
print "Hello, world!\n";
The side effect of this statement is the appearance of the string Hello, world! on the terminal (or at whatever output channel STDOUT is, or whatever has been selected). The calculated value is either 1 or nothing, depending on whether the print succeeds or not. It is discarded. If you capture the result of print into a variable
$success = print "Hello, world!\n";
then the print "Hello, world!\n" part is a subexpression of the statement; the two side effects are: output of string, storage of the result from print in a variable; but the main effect is the result of the variable assignment operation, which happens to be the value of the variable $success. This final result is also discarded.
Ad 2, rules for BEGIN blocks and use statements apply.
use strict; use warnings; my $x; $x = 'bar'; BEGIN { print "\$x in first BEGIN block: '$x'\n" } print "runtime: \$x = '$x'\n"; BEGIN { $x = 'foo'; print "\$x in second BEGIN block: '$x'\n"; } print "done.\n"; __END__ Use of uninitialized value $x in concatenation (.) or string at foo.pl + line 4. $x in first BEGIN block: '' $x in second BEGIN block: 'foo' runtime: $x = 'bar' done.
In the second line of the above snippet, $x is allocated - uninitialized - on the scope's PAD (not in the symbol table, since it is a my variable. Line 3: $x the assignment operation is compiled and stored in the execution path (the optree). After that, the BEGIN block is compiled and executed. The variable $x is still uninitailized, hence the warning. Compiling goes on, the print operation is compiled, but not executed. After that, the second BEGIN block is compiled and executed, so $x is assigned a value. But at runtime, line 3 is executed, (assignment), thus $x is 'bar' at line 5.
(Guess on (2) and (3))Is that correct?
- Based on what some of the languages do, I would guess that when a variable is defined, it is inserted into the symbol table, which is used by the semantic parser. When the grammar is correct, the code containing the variable is inserted into the output.
- There are N other passes that generate the binary.
- Then, when the binary is executed, the variable is used so has run-time effects.
Not quite.
|
|---|