DEV Community

Yuki Kimoto
Yuki Kimoto

Posted on

The way to read Perl core source codes

I explain the way to read Perl core source codes. Perl 5.36.0 is used in this description.

This is originally The way to read Perl core source codes | Perl ABC

Where is main function?

main function is the entry point of perl command.

It is written in perlmain.c, but Perl has no C source code that name is perlmain.c.

perlmain.c is generated by Makefile.SH using ExtUtils::Miniperl.

Where is processing perl command line arguments?

Processing perl command line arguments are started from perl_parse function.

perl_parse is defined in perl.c.

Go forward to parse_body function.

parse_body is defined in perl.c

You can see the processing logic of command line arguments.

Where is tokenizer?

Next is the tokenizer. The parts of Perl syntax are converted to tokens.

Tokenizing is started from lex_start function.

lex_start is defined in toke.c.

Where is parser?

Next is the parser. The parser converts tokens to AST(abstruct syntax tree).

Parsing is started from yyparse function.

yyparse is defined in perly.c.

perly.c is generated from perly.y using bison.

Where is Perl data structure?

The type of the scalar value SV and its functions are defined in sv.h and sv.c.

The type of the array AV and its functions are defined in av.h and av.c.

The type of the hash HV and its functions are defined in hv.h and hv.c.

Where does Perl run?

After AST is optimized, Perl run from perl_run function.

perl_run is defined in perl.c.

Go forward to run_body function.

run_body is defined in perl.c.

Go forward to CALLRUNOPS.

CALLRUNOPS is defined as PL_runops in perl.h

Furthermore, macros are defined as follows.

perl.h:#define CALLRUNOPS  PL_runops
embedvar.h:#define PL_runops            (vTHX->Irunops)
intrpvar.h:PERLVARI(I, runops,  runops_proc_t, RUNOPS_DEFAULT)
perl.h:# define RUNOPS_DEFAULT Perl_runops_standard

Go forward to Perl_runops_standard function.

Perl_runops_standard is defined in run.c.

The member op_ppaddr of OP structure is called.

op_ppaddr is a function pointer to perlform an operation.

Where are operators defined?

Operators are defined in the following sources.

Discussion (0)