1. Perl
  2. Language implementation
  3. here

interpvar.h

interpretvar.h contains the definitions of interpreted variable. Interpreter variable are variable that are defined for each Perl interpreter.

What is an interpreter?

Perl can have multiple interpreters. Perl will generate an interpreter and begin interpreting the source code. Normally, one interpreter is enough, but if you want to keep multiple interpreters in a C language program, you can generate multiple interpreters.

# Multiple interpreters
Perl-Interpreter 1
     |
     - Interpreter 2
     |
     - Interpreter 3

However, Perl can have multiple interpreters only when the macro "MULTIPLICITY" is enabled. This can be set as a compile option. In my environment, the default Perl on CentOS was valid, and the one installed by perlbrew was disabled. Performance seems to be faster if "MULTIPLICITY" is not enabled.

If "MULTIPLICITY" is not enabled, Perl's structure looks like this:

Perl-(no interpreter)

=====

Interpreter variable

Interpreter variable are variable that each interpreter has. When reading Perl source code, you will open interpvar.h very often. This is because the variable defined here are used throughout the Perl source code.

For example, subroutine call stack information, currently executing syntax tree nodes, a lexical variable information, a local variable information, control information, variable related to SV memory allocation, program execution start position, and so on.

Interpreter variable are defined using special macros. It's a macro called PERLVAR.

PERLVAR (I, stack_sp, SV **)/* top of the stack */PERLVAR (I, op, OP *)/* currently executing op */PERLVAR (I, curpad, SV **)/* active pad (lexicals + tmps) */</pre>

Let's take a look at the PERLVAR macro. There is a definition in "perl.h".

<pre>
/*perl.h */
# define PERLVAR (prefix, var, type) type prefix # # var;

I think that few people who have used the C language define definition have seen the symbol "##". You can combine strings using "##".

That is, the PERLVAR macro expands as follows:

SV ** Istack_sp;
OP * Iop;
SV ** Icurpad;

However, this variable is rarely used in Perl source code. It is often accessed with the variable name "PL_stacksp". It is defined in "embedvar.h".

# define PL_stack_sp (vTHX->Istack_sp)

When actually reading the source code, if you see the variable name "PL_stack_sp", you first guess that this is an interpreter variable. Then open "interpvar.h" and check if there is a definition like "PERLVAR (I, stack_sp, SV **)".

Interpreter variable are defined as global variable when "MULTIPLICITY" is not enabled

Interpreter variable are defined as global variable when "MULTIPLICITY" is not enabled. The description is in "perl.h".

# if !defined(MULTIPLICITY)
START_EXTERN_C
# include "intrpvar.h"
END_EXTERN_C
# endif

vTHX is the current interpreter

vTHX is an interpreter. Under "MULTIPLICITY", "vTHX" expands to "aTHX" and "PERL_GET_INTERP", but in general Perl, read as "PERL_IMPLICIT_CONTEXT" is defined. In other words, let's think that it will be expanded to "aTHX".

# if defined(MULTIPLICITY)
/ * cases 2 and 3 above */
# if defined(PERL_IMPLICIT_CONTEXT)
# define vTHX aTHX
# else
# define vTHX PERL_GET_INTERP
# endif

Furthermore, in "perl.h", "aTXH" is expanded to "my_perl".

/*perl.h */
# define aTHX my_perl

"My_perl" is a variable that represents the current interpreter and is defined in "perl_main.c".

/*perl.h */
static PerlInterpreter * my_perl;

Perl Interpreter is defined in "perl.h" as an alias for the "interpreter structure".

/*perl.h */
typedef struct interpreter PerlInterpreter;

And finally, the "interpreter structure" is defined as follows: The definition of the interpreter variable in "interpvar.h" is written as a member of the structure.

/*perl.h */
struct interpreter {
# include "intrpvar.h"
};

You can see that "PL_stack_sp" is expanded to "my_perl->Istack_sp". This means accessing a member variable called "Istack_sp" in the current interpreter.

Summary

Keep in mind that "PL_stack_sp" will be expanded to "my_perl->Istack_sp". In addition, if "MULTIPLICITY" is not enabled, "Istack_sp" is directly available as a global variable. By using "PL_stack_sp", you can access with or without multiple interpreters without being aware of it.

Perl macro definitions are too deep in the hierarchy, but if you go deeper, you'll find a landing point.


Perl Language Research

Related Informatrion