Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: What is an ordinary statement?

by Eily (Monsignor)
on Jun 04, 2019 at 20:12 UTC ( [id://11100973]=note: print w/replies, xml ) Need Help??


in reply to What is an ordinary statement?

From the context I'd say that ordinary statement means executable statement. Unlike #define or even just plain variable declaration in C, which define some new words for the compiler to understand later statements, but are not translated into instructions.

Because variables in perl are quite more complicated than C variables (basically, there is some C structure that holds the variable information, that structure must be initialized), they act as ordinary statements by being translated into instruction that are executed at a given time (before some statements, and after others). As far as I can tell, the only place where this is relevant is in explaining why statements such as: my $variable = 'value' unless /condition/; lead to undefined behaviour, because the executable (run-time) part of the my statement could be understood as being skipped if /condition/ is met. The compile time effect of my $variable would be to declare (well...) the existence of $variable to the compiler.

I would guess that when a variable is defined, it is inserted into the symbol table
I'm a little less confident about what I'm about to say after seeing other replies, but here it is: That's almost never true(*). First, to be sure that we agree on the vocabulary, the thing that makes a variable start to exist is its declaration(*) (telling the compiler "I'm going to use that name at some point"). The definition is giving it a value. In perl, variable declaration can be explicit (eg: using my), or in some cases implicit (most of the time, when strict is not used), when something that looks like a variable is being used while never explicitly declared (eg you write $name = 'ntj' when there never was a my $name), in those cases, defining the variable will mean adding it to the symbols table. But the main reason why declaring a variable does not insert it in the symbols table, is that only package variables are in the symbols table. Perl accesses the package variables' values by looking for their name in the correct package's symbols table. If the variable has a full name (eg $My::Package::variable) the package is the one explicitly mentioned, otherwise if the variable as a simple name (eg $variable) and has not been declared with my, the correct package is the current one.

On the other hand, you can consider that perl forgets the names of lexical (my) variables after compiling, and it basically uses placeholders instead of the name ("the tool in my left hand" rather than "Eily's screwdriver"). This is why symbolic references (using a string with the name of a variable to get its value) do not work on lexical variables. That's also why a package variable is the same for different functions (several people using Eily's screwdriver would be using the same tool), while lexical variables only have a specific meaning in the scope in which they are declared ("The tool in my left hand" is not the same tool for different people). And that's why local only works on non lexical variables, because it temporarily changes a variable at a specific name.

(*)This depends on your exact interpretation of "declaration" is in perl. You might consider that all package variables are actually always declared, but are used or unused. In which case package Hello; our $world; does not declare the variable $Hello::world which always existed, but instead only declares $world, which is a lexical alias to $Hello::world;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-04-24 20:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found