|
198 | 198 | my $filters_scn = 'Source Filters';
|
199 | 199 | my $floating_scn = 'Floating point';
|
200 | 200 | my $genconfig_scn = 'General Configuration';
|
| 201 | +my $global_definitions_scn = 'Declaration and Initialization of Globals'; |
201 | 202 | my $globals_scn = 'Global Variables';
|
202 | 203 | my $GV_scn = 'GV Handling and Stashes';
|
203 | 204 | my $hook_scn = 'Hook manipulation';
|
|
373 | 374 | =back
|
374 | 375 | EOT
|
375 | 376 | },
|
| 377 | + $global_definitions_scn => { |
| 378 | + header => <<~'EOT', |
| 379 | + Global variables are defined and initialized in one place, but |
| 380 | + referred to from multiple files. They need to be defined and any |
| 381 | + initialization done in that one place, but extern declarations |
| 382 | + made for them in each file that may refer to them. Note that |
| 383 | + there is no harm in declaring a global and not using it. |
| 384 | +
|
| 385 | + Perl has a mechanism that allows both purposes to be served while |
| 386 | + minimizing code duplication. F<EXTERN.h> and F<INTERN.h> define |
| 387 | + the same relatively few macros, but their definitions are |
| 388 | + different. In F<EXTERN.h>, the macros expand to |
| 389 | + declarations of the globals as external to the file. In |
| 390 | + F<INTERN.h> they actually cause the space to be allocated and |
| 391 | + possibly initialized. |
| 392 | +
|
| 393 | + Most files will follow this paradigm: |
| 394 | +
|
| 395 | + #include "EXTERN.h" |
| 396 | + ... |
| 397 | + #include "perl.h"> |
| 398 | +
|
| 399 | + This causes every global symbol that is referred to in F<perl.h> |
| 400 | + and every file it includes (which is nearly every top level Perl |
| 401 | + header file) to be declared as external. |
| 402 | +
|
| 403 | + The very few files that define globals will instead do |
| 404 | +
|
| 405 | + #include "INTERN.h" |
| 406 | + ... |
| 407 | + #include "perl.h"> |
| 408 | + include the file |
| 409 | +
|
| 410 | + It doesn't work for a file to both define some globals and refer |
| 411 | + to others as externs. That is, you can only include one of |
| 412 | + F<INTERN.h> and F<EXTERN.h>. |
| 413 | +
|
| 414 | + This section documents the macros that are defined in these two |
| 415 | + header files. F<perl.h> has many uses of them that can serve as |
| 416 | + paradigms for you. |
| 417 | + EOT |
| 418 | + may_be_empty_in_perlapi => 1, |
| 419 | + }, |
376 | 420 | $globals_scn => {},
|
377 | 421 | $GV_scn => {},
|
378 | 422 | $hook_scn => {},
|
|
475 | 519 | 'gv.c' => $GV_scn,
|
476 | 520 | 'gv.h' => $GV_scn,
|
477 | 521 | 'hv.h' => $HV_scn,
|
| 522 | + 'INTERN.h' => $global_definitions_scn, |
478 | 523 | 'locale.c' => $locale_scn,
|
479 | 524 | 'malloc.c' => $memory_scn,
|
480 | 525 | 'numeric.c' => $numeric_scn,
|
|
0 commit comments