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 -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

In reply to Re: What is an ordinary statement? by shmem
in thread What is an ordinary statement? by ntj

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.