The way to read Perl core source codes
Tags: perl
Published: 2022-06-09 05:25:53

I explain the way to read Perl core source codes. [Perl 5.36.0](https://github.com/Perl/perl5/tree/v5.36.0) is used in this description.

This is originally <a href="https://en.perlzemi.com/blog/20220609124939.html">The way to read Perl core source codes | Perl ABC</a>

#### 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](https://github.com/Perl/perl5/blob/v5.36.0/Makefile.SH#L802) using [ExtUtils::Miniperl](https://github.com/Perl/perl5/blob/v5.36.0/ext/ExtUtils-Miniperl/lib/ExtUtils/Miniperl.pm).

#### Where is processing perl command line arguments?

Processing perl command line arguments are started from [perl_parse](https://github.com/Perl/perl5/blob/v5.36.0/ext/ExtUtils-Miniperl/lib/ExtUtils/Miniperl.pm#L141) function.

`perl_parse` is defined in [perl.c](https://github.com/Perl/perl5/blob/v5.36.0/perl.c#L1678).

Go forward to [parse_body](https://github.com/Perl/perl5/blob/v5.36.0/perl.c#L1827) function.

`parse_body` is defined in [perl.c](https://github.com/Perl/perl5/blob/v5.36.0/perl.c#L2056)

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](https://github.com/Perl/perl5/blob/v5.36.0/perl.c#L2523) function.

`lex_start` is defined in [toke.c](https://github.com/Perl/perl5/blob/v5.36.0/toke.c#L809).

#### Where is parser?

Next is the parser. The parser converts tokens to [AST](https://en.wikipedia.org/wiki/Abstract_syntax_tree)(abstruct syntax tree).

Parsing is started from [yyparse](https://github.com/Perl/perl5/blob/v5.36.0/perl.c#L2536) function.

`yyparse` is defined in [perly.c](https://github.com/Perl/perl5/blob/v5.36.0/perly.c#L266).

`perly.c` is generated from [perly.y](https://github.com/Perl/perl5/blob/v5.36.0/perly.y) using [bison](https://en.wikipedia.org/wiki/GNU_Bison).

#### Where is Perl data structure?

The type of the scalar value `SV` and its functions are defined in [sv.h](https://github.com/Perl/perl5/blob/v5.36.0/sv.h) and [sv.c](https://github.com/Perl/perl5/blob/v5.36.0/sv.c).

The type of the array `AV` and its functions are defined in [av.h](https://github.com/Perl/perl5/blob/v5.36.0/av.h) and [av.c](https://github.com/Perl/perl5/blob/v5.36.0/av.c).

The type of the hash `HV` and its functions are defined in [hv.h](https://github.com/Perl/perl5/blob/v5.36.0/hv.h) and [hv.c](https://github.com/Perl/perl5/blob/v5.36.0/hv.c).

#### Where does Perl run?

After AST is optimized, Perl run from [perl_run](https://github.com/Perl/perl5/blob/v5.36.0/ext/ExtUtils-Miniperl/lib/ExtUtils/Miniperl.pm#L142) function.

`perl_run` is defined in [perl.c](https://github.com/Perl/perl5/blob/v5.36.0/perl.c#L2621).

Go forward to [run_body](https://github.com/Perl/perl5/blob/v5.36.0/perl.c#L2644) function.

`run_body` is defined in [perl.c](https://github.com/Perl/perl5/blob/v5.36.0/perl.c#L2678).

Go forward to [CALLRUNOPS](https://github.com/Perl/perl5/blob/v5.36.0/perl.c#L2721).

`CALLRUNOPS` is defined as `PL_runops` in [perl.h](https://github.com/Perl/perl5/blob/v5.36.0/perl.h#L228)

Furthermore, macros are defined as follows.

<pre>
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
</pre>

Go forward to `Perl_runops_standard` function.

`Perl_runops_standard` is defined in [run.c](https://github.com/Perl/perl5/blob/v5.36.0/run.c#L37).

The member [op_ppaddr](https://github.com/Perl/perl5/blob/v5.36.0/run.c#L41) 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.

* [pp.c](https://github.com/Perl/perl5/blob/v5.36.0/pp.c)
* [pp_ctl.c](https://github.com/Perl/perl5/blob/v5.36.0/pp_ctl.c)
* [pp_hot.c](https://github.com/Perl/perl5/blob/v5.36.0/pp_hot.c)
* [pp_pack.c](https://github.com/Perl/perl5/blob/v5.36.0/pp_pack.c)
* [pp_sort.c](https://github.com/Perl/perl5/blob/v5.36.0/pp_sort.c)
* [pp_sys.c](https://github.com/Perl/perl5/blob/v5.36.0/pp_sys.c)