MAJOR=11 MINOR=0 DEVNAME=sr0 DEVTYPE=disk DISKSEQ=4 7ˆl–ßi~”ên-jq@ãoܪbÑ¿�þ^h’¬ãd}·� Ž€€þÏ „e—ßÎÍÌ?÷=encoding utf8 =for comment Consistent formatting of this file is achieved with: perl ./Porting/podtidy pod/perlinterp.pod =head1 NAME perlinterp - An overview of the Perl interpreter =head1 DESCRIPTION This document provides an overview of how the Perl interpreter works at the level of C code, along with pointers to the relevant C source code files. =head1 ELEMENTS OF THE INTERPRETER The work of the interpreter has two main stages: compiling the code into the internal representation, or bytecode, and then executing it. L explains exactly how the compilation stage happens. Here is a short breakdown of perl's operation: =head2 Startup The action begins in F. (or F for miniperl) This is very high-level code, enough to fit on a single screen, and it resembles the code found in L; most of the real action takes place in F F is generated by C from F at make time, so you should make perl to follow this along. First, F allocates some memory and constructs a Perl interpreter, along these lines: 1 PERL_SYS_INIT3(&argc,&argv,&env); 2 3 if (!PL_do_undump) { 4 my_perl = perl_alloc(); 5 if (!my_perl) 6 exit(1); 7 perl_construct(my_perl); 8 PL_perl_destruct_level = 0; 9 } Line 1 is a macro, and its definition is dependent on your operating system. Line 3 references C, a global variable - all global variables in Perl start with C. This tells you whether the current running program was created with the C<-u> flag to perl and then F, which means it's going to be false in any sane context. Line 4 calls a function in F to allocate memory for a Perl interpreter. It's quite a simple function, and the guts of it looks like this: my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter)); Here you see an example of Perl's system abstraction, which we'll see later: C is either your system's C, or Perl's own C as defined in F if you selected that option at configure time. Next, in line 7, we construct the interpreter using perl_construct, also in F; this sets up all the special variables that Perl needs, the stacks, and so on. Now we pass Perl the command line options, and tell it to go: if (!perl_parse(my_perl, xs_init, argc, argv, (char **)NULL)) perl_run(my_perl); exitstatus = perl_destruct(my_perl); perl_free(my_perl); C is actually a wrapper around C, as defined in F, which processes the command line options, sets up any statically linked XS modules, opens the program and calls C to parse it. =head2 Parsing The aim of this stage is to take the Perl source, and turn it into an op tree. We'll see what one of those looks like later. Strictly speaking, there's three things going on here. C, the parser, lives in F, although you're better off reading the original YACC input in F. (Yes, Virginia, there B a YACC grammar for Perl!) The job of the parser is to take your code and "understand" it, splitting it into sentences, deciding which operands go with which operators and so on. The parser is nobly assisted by the lexer, which chunks up your input into tokens, and decides what type of thing each token is: a variable name, an operator, a bareword, a subroutine, a core function, and so on. The main point of entry to the lexer is C, and that and its associated routines can be found in F. Perl isn't much like other computer languages; it's highly context sensitive at times, it can be tricky to work out what sort of token something is, or where a token ends. As such, there's a lot of interplay between the tokeniser and the parser, which can get pretty frightening if you're not used to it. As the parser understands a Perl prog