Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: What is an ordinary statement?

by shmem (Chancellor)
on Jun 21, 2019 at 10:43 UTC ( [id://11101664]=note: print w/replies, xml ) Need Help??


in reply to What is an ordinary statement?

Questions

Answers:

  1. What is an ordinary statement?
    An ordinary statement is an expression calculated for the sake of its side effect. The result of the calculation is discarded.
  2. What are the run- and compile-time effects of this statement?
    At compile time, structures are allocated for the statement to be successful at runtime. At runtime, these structures come to life with content.
  3. Are there more compile-time steps in perl than in C/C++ when defining a variable?
    They are different. Perl is implemented in C, so all compile-time concepts of C apply for the perl binary. But perl has its own compile-time rules and optimizers. Linkage rules are different, and there is no compilation into assembly and machine language.

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))
  • 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.
Is that correct?

Not quite.

  • perl compiles the directives of a program into an internal representation, called the execution stack, consisting of stack frames. Only variables declared as such (package globals) are allocated in the symbol table, others pertain to its stack frame and are local to that frame. Only if the setup of this execution stack succeeds according to the perl grammar, perl switches to runtime. Structures of the variables contents are created at runtime.
  • there are some passes over the execution tree, optimisation and such, e.g. to weed out code which will never be executed. No binary is created except the internal binary representation of the program.
  • since there is no binary, there is no execution of a binary. Instead, at runtime, the internal execution stack is walked.
perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11101664]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-03-28 08:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found