This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add created_as_... builtins to perldelta
[perl5.git] / pod / perlhack.pod
1 =encoding utf8
2
3 =for comment
4 Consistent formatting of this file is achieved with:
5   perl ./Porting/podtidy pod/perlhack.pod
6
7 =head1 NAME
8
9 perlhack - How to hack on Perl
10
11 =head1 DESCRIPTION
12
13 This document explains how Perl development works.  It includes details
14 about the Perl 5 Porters email list, the Perl repository, the Perl
15 bug tracker, patch guidelines, and commentary on Perl development
16 philosophy.
17
18 =head1 SUPER QUICK PATCH GUIDE
19
20 If you just want to submit a single small patch like a pod fix, a test
21 for a bug, comment fixes, etc., it's easy! Here's how:
22
23 =over 4
24
25 =item * Check out the source repository
26
27 The perl source is in a git repository.  You can clone the repository
28 with the following command:
29
30   % git clone https://github.com/Perl/perl5.git perl
31
32 =item * Ensure you're following the latest advice
33
34 In case the advice in this guide has been updated recently, read the
35 latest version directly from the perl source:
36
37   % perldoc pod/perlhack.pod
38
39 =item * Create a branch for your change
40
41 Create a branch based on blead to commit your change to, which will
42 later be used to send it to the Perl issue tracker.
43
44   % git checkout -b mychange
45
46 =item * Make your change
47
48 Hack, hack, hack.  Keep in mind that Perl runs on many different
49 platforms, with different operating systems that have different
50 capabilities, different filesystem organizations, and even different
51 character sets.  L<perlhacktips> gives advice on this.
52
53 =item * Test your change
54
55 You can run all the tests with the following commands:
56
57   % ./Configure -des -Dusedevel
58   % make test
59
60 Keep hacking until the tests pass.
61
62 =item * Commit your change
63
64 Committing your work will save the change I<on your local system>:
65
66   % git commit -a -m 'Commit message goes here'
67
68 Make sure the commit message describes your change in a single
69 sentence.  For example, "Fixed spelling errors in perlhack.pod".
70
71 =item * Send your change to the Perl issue tracker
72
73 The next step is to submit your patch to the Perl core ticket system.
74
75 Create a GitHub fork of the perl5 repository and add it as a remote,
76 if you haven't already, as described in the GitHub documentation at
77 L<https://help.github.com/en/articles/working-with-forks>.
78
79   % git remote add fork git@github.com:MyUser/perl5.git
80
81 For more information, see L<"Connecting to GitHub with SSH"|https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/connecting-to-github-with-ssh>.
82
83 If you'd rather use an HTTPS URL for your C<git push> see L<"Cloning with
84 HTTPS URLs"|https://docs.github.com/en/free-pro-team@latest/github/using-git/which-remote-url-should-i-use#cloning-with-https-urls>.
85
86   % git remote add fork https://github.com/MyUser/perl5.git
87
88 Then, push your new branch to your fork.
89
90   % git push -u fork mychange
91
92 Finally, create a Pull Request on GitHub from your branch to blead as
93 described in the GitHub documentation at
94 L<https://help.github.com/en/articles/creating-a-pull-request-from-a-fork>.
95
96 =item * Thank you
97
98 The porters appreciate the time you spent helping to make Perl better.
99 Thank you!
100
101 =item * Acknowledgement
102
103 All contributors are credited (by name and email address) in the
104 AUTHORS file, which is part of the perl distribution, as well as the
105 Git commit history.
106
107 If you don’t want to be included in the AUTHORS file, just let us
108 know. Otherwise we will take your submission of a patch as permission
109 to credit you in the AUTHORS file.
110
111 =item * Next time
112
113 The next time you wish to make a patch, you need to start from the
114 latest perl in a pristine state.  Check you don't have any local changes
115 or added files in your perl check-out which you wish to keep, then run
116 these commands:
117
118   % git checkout blead
119   % git pull
120   % git reset --hard origin/blead
121   % git clean -dxf
122
123 =back
124
125 =head1 BUG REPORTING
126
127 If you want to report a bug in Perl, or browse existing Perl bugs and
128 patches, use the GitHub issue tracker at
129 L<https://github.com/perl/perl5/issues>.
130
131 Please check the archive of the perl5-porters list (see below) and/or
132 the bug tracking system before submitting a bug report.  Often, you'll
133 find that the bug has been reported already.
134
135 You can log in to the bug tracking system and comment on existing bug
136 reports.  If you have additional information regarding an existing bug,
137 please add it.  This will help the porters fix the bug.
138
139 =head1 PERL 5 PORTERS
140
141 The perl5-porters (p5p) mailing list is where the Perl standard
142 distribution is maintained and developed.  The people who maintain Perl
143 are also referred to as the "Perl 5 Porters", "p5p" or just the
144 "porters".
145
146 A searchable archive of the list is available at
147 L<https://markmail.org/search/?q=perl5-porters>.  There is also an archive at
148 L<https://archive.develooper.com/perl5-porters@perl.org/>.
149
150 =head2 perl-changes mailing list
151
152 The perl5-changes mailing list receives a copy of each patch that gets
153 submitted to the maintenance and development branches of the perl
154 repository.  See L<https://lists.perl.org/list/perl5-changes.html> for
155 subscription and archive information.
156
157 =head2 #p5p on IRC
158
159 Many porters are also active on the L<irc://irc.perl.org/#p5p> channel.
160 Feel free to join the channel and ask questions about hacking on the
161 Perl core.
162
163 =head1 GETTING THE PERL SOURCE
164
165 All of Perl's source code is kept centrally in a Git repository at
166 I<github.com>.  The repository contains many Perl revisions
167 from Perl 1 onwards and all the revisions from Perforce, the previous
168 version control system.
169
170 For much more detail on using git with the Perl repository, please see
171 L<perlgit>.
172
173 =head2 Read access via Git
174
175 You will need a copy of Git for your computer.  You can fetch a copy of
176 the repository using the git protocol:
177
178   % git clone git@github.com:Perl/perl5.git perl
179
180 This clones the repository and makes a local copy in the F<perl>
181 directory.
182
183 If you cannot use the git protocol for firewall reasons, you can also
184 clone via http:
185
186   % git clone https://github.com/Perl/perl5.git perl
187
188 =head2 Read access via the web
189
190 You may access the repository over the web.  This allows you to browse
191 the tree, see recent commits, subscribe to repository notifications,
192 search for particular commits and more.  You may access it at
193 L<https://github.com/Perl/perl5>.
194
195 =head2 Write access via git
196
197 If you have a commit bit, please see L<perlgit> for more details on
198 using git.
199
200 =head1 PATCHING PERL
201
202 If you're planning to do more extensive work than a single small fix,
203 we encourage you to read the documentation below.  This will help you
204 focus your work and make your patches easier to incorporate into the
205 Perl source.
206
207 =head2 Submitting patches
208
209 If you have a small patch to submit, please submit it via the GitHub
210 Pull Request workflow.  You may also send patches to the p5p list.
211
212 Patches are reviewed and discussed on GitHub or the p5p list.  Simple,
213 uncontroversial patches will usually be applied without any discussion.
214 When the patch is applied, the ticket will be updated and you will
215 receive email.
216
217 In other cases, the patch will need more work or discussion.
218 You are encouraged to participate in the discussion and advocate for
219 your patch.  Sometimes your patch may get lost in the shuffle.  It's
220 appropriate to send a reminder email to p5p if no action has been taken
221 in a month.  Please remember that the Perl 5 developers are all
222 volunteers, and be polite.
223
224 Changes are always applied directly to the main development branch,
225 called "blead".  Some patches may be backported to a maintenance
226 branch.  If you think your patch is appropriate for the maintenance
227 branch (see L<perlpolicy/MAINTENANCE BRANCHES>), please explain why
228 when you submit it.
229
230 =head2 Getting your patch accepted
231
232 If you are submitting a code patch there are several things that you
233 can do to help the Perl 5 Porters accept your patch.
234
235 =head3 Patch style
236
237 Using the GitHub Pull Request workflow, your patch will automatically
238 be available in a suitable format.  If you wish to submit a patch to
239 the p5p list for review, make sure to create it appropriately.
240
241 If you used git to check out the Perl source, then using C<git
242 format-patch> will produce a patch in a style suitable for Perl.  The
243 C<format-patch> command produces one patch file for each commit you
244 made.  If you prefer to send a single patch for all commits, you can
245 use C<git diff>.
246
247   % git checkout blead
248   % git pull
249   % git diff blead my-branch-name
250
251 This produces a patch based on the difference between blead and your
252 current branch.  It's important to make sure that blead is up to date
253 before producing the diff, that's why we call C<git pull> first.
254
255 We strongly recommend that you use git if possible.  It will make your
256 life easier, and ours as well.
257
258 However, if you're not using git, you can still produce a suitable
259 patch.  You'll need a pristine copy of the Perl source to diff against.
260 The porters prefer unified diffs.  Using GNU C<diff>, you can produce a
261 diff like this:
262
263   % diff -Npurd perl.pristine perl.mine
264
265 Make sure that you C<make realclean> in your copy of Perl to remove any
266 build artifacts, or you may get a confusing result.
267
268 =head3 Commit message
269
270 As you craft each patch you intend to submit to the Perl core, it's
271 important to write a good commit message.  This is especially important
272 if your submission will consist of a series of commits.
273
274 The first line of the commit message should be a short description
275 without a period.  It should be no longer than the subject line of an
276 email, 50 characters being a good rule of thumb.
277
278 A lot of Git tools (Gitweb, GitHub, git log --pretty=oneline, ...) will
279 only display the first line (cut off at 50 characters) when presenting
280 commit summaries.
281
282 The commit message should include a description of the problem that the
283 patch corrects or new functionality that the patch adds.
284
285 As a general rule of thumb, your commit message should help a
286 programmer who knows the Perl core quickly understand what you were
287 trying to do, how you were trying to do it, and why the change matters
288 to Perl.
289
290 =over 4
291
292 =item * Why
293
294 Your commit message should describe why the change you are making is
295 important.  When someone looks at your change in six months or six
296 years, your intent should be clear.
297
298 If you're deprecating a feature with the intent of later simplifying
299 another bit of code, say so.  If you're fixing a performance problem or
300 adding a new feature to support some other bit of the core, mention
301 that.
302
303 =item * What
304
305 Your commit message should describe what part of the Perl core you're
306 changing and what you expect your patch to do.
307
308 =item * How
309
310 While it's not necessary for documentation changes, new tests or
311 trivial patches, it's often worth explaining how your change works.
312 Even if it's clear to you today, it may not be clear to a porter next
313 month or next year.
314
315 =back
316
317 A commit message isn't intended to take the place of comments in your
318 code.  Commit messages should describe the change you made, while code
319 comments should describe the current state of the code.
320
321 If you've just implemented a new feature, complete with doc, tests and
322 well-commented code, a brief commit message will often suffice.  If,
323 however, you've just changed a single character deep in the parser or
324 lexer, you might need to write a small novel to ensure that future
325 readers understand what you did and why you did it.
326
327 =head3 Comments, Comments, Comments
328
329 Be sure to adequately comment your code.  While commenting every line
330 is unnecessary, anything that takes advantage of side effects of
331 operators, that creates changes that will be felt outside of the
332 function being patched, or that others may find confusing should be
333 documented.  If you are going to err, it is better to err on the side
334 of adding too many comments than too few.
335
336 The best comments explain I<why> the code does what it does, not I<what
337 it does>.
338
339 =head3 Style
340
341 In general, please follow the particular style of the code you are
342 patching.
343
344 In particular, follow these general guidelines for patching Perl
345 sources:
346
347 =over 4
348
349 =item *
350
351 4-wide indents for code, 2-wide indents for nested CPP C<#define>s,
352 with 8-wide tabstops.
353
354 =item *
355
356 Use spaces for indentation, not tab characters.
357
358 The codebase is a mixture of tabs and spaces for indentation, and we
359 are moving to spaces only.  Converting lines you're patching from 8-wide
360 tabs to spaces will help this migration.
361
362 =item *
363
364 Try not to exceed 79 columns
365
366 In general, we target 80 column lines.  When sticking to 80 columns would lead
367 to torturous code or rework, it's fine to go longer.  Try to keep your excess
368 past 80 to a minimum.
369
370 =item *
371
372 ANSI C prototypes
373
374 =item *
375
376 Uncuddled elses and "K&R" style for indenting control constructs
377
378 =item *
379
380 No C++ style (//) comments
381
382 =item *
383
384 Mark places that need to be revisited with XXX (and revisit often!)
385
386 =item *
387
388 Opening brace lines up with "if" when conditional spans multiple lines;
389 should be at end-of-line otherwise
390
391 =item *
392
393 In function definitions, name starts in column 0 (return value-type is on
394 previous line)
395
396 =item *
397
398 Single space after keywords that are followed by parens, no space
399 between function name and following paren
400
401 =item *
402
403 Avoid assignments in conditionals, but if they're unavoidable, use
404 extra paren, e.g. "if (a && (b = c)) ..."
405
406 =item *
407
408 "return foo;" rather than "return(foo);"
409
410 =item *
411
412 "if (!foo) ..." rather than "if (foo == FALSE) ..." etc.
413
414 =item *
415
416 Do not declare variables using "register".  It may be counterproductive
417 with modern compilers, and is deprecated in C++, under which the Perl
418 source is regularly compiled.
419
420 =item *
421
422 In-line functions that are in headers that are accessible to XS code
423 need to be able to compile without warnings with commonly used extra
424 compilation flags, such as gcc's C<-Wswitch-default> which warns
425 whenever a switch statement does not have a "default" case.  The use of
426 these extra flags is to catch potential problems in legal C code, and
427 is often used by Perl aggregators, such as Linux distributors.
428
429 =back
430
431 =head3 Test suite
432
433 If your patch changes code (rather than just changing documentation),
434 you should also include one or more test cases which illustrate the bug
435 you're fixing or validate the new functionality you're adding.  In
436 general, you should update an existing test file rather than create a
437 new one.
438
439 Your test suite additions should generally follow these guidelines
440 (courtesy of Gurusamy Sarathy <gsar@activestate.com>):
441
442 =over 4
443
444 =item *
445
446 Know what you're testing.  Read the docs, and the source.
447
448 =item *
449
450 Tend to fail, not succeed.
451
452 =item *
453
454 Interpret results strictly.
455
456 =item *
457
458 Use unrelated features (this will flush out bizarre interactions).
459
460 =item *
461
462 Use non-standard idioms (otherwise you are not testing TIMTOWTDI).
463
464 =item *
465
466 Avoid using hardcoded test numbers whenever possible (the EXPECTED/GOT
467 found in t/op/tie.t is much more maintainable, and gives better failure
468 reports).
469
470 =item *
471
472 Give meaningful error messages when a test fails.
473
474 =item *
475
476 Avoid using qx// and system() unless you are testing for them.  If you
477 do use them, make sure that you cover _all_ perl platforms.
478
479 =item *
480
481 Unlink any temporary files you create.
482
483 =item *
484
485 Promote unforeseen warnings to errors with $SIG{__WARN__}.
486
487 =item *
488
489 Be sure to use the libraries and modules shipped with the version being
490 tested, not those that were already installed.
491
492 =item *
493
494 Add comments to the code explaining what you are testing for.
495
496 =item *
497
498 Make updating the '1..42' string unnecessary.  Or make sure that you
499 update it.
500
501 =item *
502
503 Test _all_ behaviors of a given operator, library, or function.
504
505 Test all optional arguments.
506
507 Test return values in various contexts (boolean, scalar, list, lvalue).
508
509 Use both global and lexical variables.
510
511 Don't forget the exceptional, pathological cases.
512
513 =back
514
515 =head2 Patching a core module
516
517 This works just like patching anything else, with one extra
518 consideration.
519
520 Modules in the F<cpan/> directory of the source tree are maintained
521 outside of the Perl core.  When the author updates the module, the
522 updates are simply copied into the core.  See that module's
523 documentation or its listing on L<https://metacpan.org/> for more
524 information on reporting bugs and submitting patches.
525
526 In most cases, patches to modules in F<cpan/> should be sent upstream
527 and should not be applied to the Perl core individually.  If a patch to
528 a file in F<cpan/> absolutely cannot wait for the fix to be made
529 upstream, released to CPAN and copied to blead, you must add (or
530 update) a C<CUSTOMIZED> entry in the F<"Porting/Maintainers.pl"> file
531 to flag that a local modification has been made.  See
532 F<"Porting/Maintainers.pl"> for more details.
533
534 In contrast, modules in the F<dist/> directory are maintained in the
535 core.
536
537 =head2 Updating perldelta
538
539 For changes significant enough to warrant a F<pod/perldelta.pod> entry,
540 the porters will greatly appreciate it if you submit a delta entry
541 along with your actual change.  Significant changes include, but are
542 not limited to:
543
544 =over 4
545
546 =item *
547
548 Adding, deprecating, or removing core features
549
550 =item *
551
552 Adding, deprecating, removing, or upgrading core or dual-life modules
553
554 =item *
555
556 Adding new core tests
557
558 =item *
559
560 Fixing security issues and user-visible bugs in the core
561
562 =item *
563
564 Changes that might break existing code, either on the perl or C level
565
566 =item *
567
568 Significant performance improvements
569
570 =item *
571
572 Adding, removing, or significantly changing documentation in the
573 F<pod/> directory
574
575 =item *
576
577 Important platform-specific changes
578
579 =back
580
581 Please make sure you add the perldelta entry to the right section
582 within F<pod/perldelta.pod>.  More information on how to write good
583 perldelta entries is available in the C<Style> section of
584 F<Porting/how_to_write_a_perldelta.pod>.
585
586 =head2 What makes for a good patch?
587
588 New features and extensions to the language can be contentious.  There
589 is no specific set of criteria which determine what features get added,
590 but here are some questions to consider when developing a patch:
591
592 =head3 Does the concept match the general goals of Perl?
593
594 Our goals include, but are not limited to:
595
596 =over 4
597
598 =item 1.
599
600 Keep it fast, simple, and useful.
601
602 =item 2.
603
604 Keep features/concepts as orthogonal as possible.
605
606 =item 3.
607
608 No arbitrary limits (platforms, data sizes, cultures).
609
610 =item 4.
611
612 Keep it open and exciting to use/patch/advocate Perl everywhere.
613
614 =item 5.
615
616 Either assimilate new technologies, or build bridges to them.
617
618 =back
619
620 =head3 Where is the implementation?
621
622 All the talk in the world is useless without an implementation.  In
623 almost every case, the person or people who argue for a new feature
624 will be expected to be the ones who implement it.  Porters capable of
625 coding new features have their own agendas, and are not available to
626 implement your (possibly good) idea.
627
628 =head3 Backwards compatibility
629
630 It's a cardinal sin to break existing Perl programs.  New warnings can
631 be contentious--some say that a program that emits warnings is not
632 broken, while others say it is.  Adding keywords has the potential to
633 break programs, changing the meaning of existing token sequences or
634 functions might break programs.
635
636 The Perl 5 core includes mechanisms to help porters make backwards
637 incompatible changes more compatible such as the L<feature> and
638 L<deprecate> modules.  Please use them when appropriate.
639
640 =head3 Could it be a module instead?
641
642 Perl 5 has extension mechanisms, modules and XS, specifically to avoid
643 the need to keep changing the Perl interpreter.  You can write modules
644 that export functions, you can give those functions prototypes so they
645 can be called like built-in functions, you can even write XS code to
646 mess with the runtime data structures of the Perl interpreter if you
647 want to implement really complicated things.
648
649 Whenever possible, new features should be prototyped in a CPAN module
650 before they will be considered for the core.
651
652 =head3 Is the feature generic enough?
653
654 Is this something that only the submitter wants added to the language,
655 or is it broadly useful?  Sometimes, instead of adding a feature with a
656 tight focus, the porters might decide to wait until someone implements
657 the more generalized feature.
658
659 =head3 Does it potentially introduce new bugs?
660
661 Radical rewrites of large chunks of the Perl interpreter have the
662 potential to introduce new bugs.
663
664 =head3 How big is it?
665
666 The smaller and more localized the change, the better.  Similarly, a
667 series of small patches is greatly preferred over a single large patch.
668
669 =head3 Does it preclude other desirable features?
670
671 A patch is likely to be rejected if it closes off future avenues of
672 development.  For instance, a patch that placed a true and final
673 interpretation on prototypes is likely to be rejected because there are
674 still options for the future of prototypes that haven't been addressed.
675
676 =head3 Is the implementation robust?
677
678 Good patches (tight code, complete, correct) stand more chance of going
679 in.  Sloppy or incorrect patches might be placed on the back burner
680 until fixes can be made, or they might be discarded altogether
681 without further notice.
682
683 =head3 Is the implementation generic enough to be portable?
684
685 The worst patches make use of system-specific features.  It's highly
686 unlikely that non-portable additions to the Perl language will be
687 accepted.
688
689 =head3 Is the implementation tested?
690
691 Patches which change behaviour (fixing bugs or introducing new
692 features) must include regression tests to verify that everything works
693 as expected.
694
695 Without tests provided by the original author, how can anyone else
696 changing perl in the future be sure that they haven't unwittingly
697 broken the behaviour the patch implements? And without tests, how can
698 the patch's author be confident that his/her hard work put into the
699 patch won't be accidentally thrown away by someone in the future?
700
701 =head3 Is there enough documentation?
702
703 Patches without documentation are probably ill-thought out or
704 incomplete.  No features can be added or changed without documentation,
705 so submitting a patch for the appropriate pod docs as well as the
706 source code is important.
707
708 =head3 Is there another way to do it?
709
710 Larry said "Although the Perl Slogan is I<There's More Than One Way to
711 Do It>, I hesitate to make 10 ways to do something".  This is a tricky
712 heuristic to navigate, though--one man's essential addition is another
713 man's pointless cruft.
714
715 =head3 Does it create too much work?
716
717 Work for the committers, work for Perl programmers, work for module
718 authors, ... Perl is supposed to be easy.
719
720 =head3 Patches speak louder than words
721
722 Working code is always preferred to pie-in-the-sky ideas.  A patch to
723 add a feature stands a much higher chance of making it to the language
724 than does a random feature request, no matter how fervently argued the
725 request might be.  This ties into "Will it be useful?", as the fact
726 that someone took the time to make the patch demonstrates a strong
727 desire for the feature.
728
729 =head1 TESTING
730
731 The core uses the same testing style as the rest of Perl, a simple
732 "ok/not ok" run through Test::Harness, but there are a few special
733 considerations.
734
735 There are three ways to write a test in the core: L<Test::More>,
736 F<t/test.pl> and ad hoc C<print $test ? "ok 42\n" : "not ok 42\n">.
737 The decision of which to use depends on what part of the test suite
738 you're working on.  This is a measure to prevent a high-level failure
739 (such as Config.pm breaking) from causing basic functionality tests to
740 fail.
741
742 The F<t/test.pl> library provides some of the features of
743 L<Test::More>, but avoids loading most modules and uses as few core
744 features as possible.
745
746 If you write your own test, use the L<Test Anything
747 Protocol|https://testanything.org>.
748
749 =over 4
750
751 =item * F<t/base>, F<t/comp> and F<t/opbasic>
752
753 Since we don't know if C<require> works, or even subroutines, use ad hoc
754 tests for these three.  Step carefully to avoid using the feature being
755 tested.  Tests in F<t/opbasic>, for instance, have been placed there
756 rather than in F<t/op> because they test functionality which
757 F<t/test.pl> presumes has already been demonstrated to work.
758
759 =item * All other subdirectories of F<t/>
760
761 Now that basic require() and subroutines are tested, you can use the
762 F<t/test.pl> library.
763
764 You can also use certain libraries like L<Config> conditionally, but be
765 sure to skip the test gracefully if it's not there.
766
767 =item * Test files not found under F<t/>
768
769 This category includes F<.t> files underneath directories such as F<dist>,
770 F<ext> and F<lib>.  Since the core of Perl has now been tested, L<Test::More>
771 can and now should be used.  You can also use the full suite of core modules
772 in the tests.  (As noted in L<"Patching a core module"> above, changes to
773 F<.t> files found under F<cpan/> should be submitted to the upstream
774 maintainers of those modules.)
775
776 =back
777
778 When you say "make test", Perl uses the F<t/TEST> program to run the
779 test suite (except under Win32 where it uses F<t/harness> instead).
780 All tests are run from the F<t/> directory, B<not> the directory which
781 contains the test.  This causes some problems with the tests in
782 F<lib/>, so here's some opportunity for some patching.
783
784 You must be triply conscious of cross-platform concerns.  This usually
785 boils down to using L<File::Spec>, avoiding things like C<fork()>
786 and C<system()> unless absolutely necessary, and not assuming that a
787 given character has a particular ordinal value (code point) or that its
788 UTF-8 representation is composed of particular bytes.
789
790 There are several functions available to specify characters and code
791 points portably in tests.  The always-preloaded functions
792 C<utf8::unicode_to_native()> and its inverse
793 C<utf8::native_to_unicode()> take code points and translate
794 appropriately.  The file F<t/charset_tools.pl> has several functions
795 that can be useful.  It has versions of the previous two functions
796 that take strings as inputs -- not single numeric code points:
797 C<uni_to_native()> and C<native_to_uni()>.  If you must look at the
798 individual bytes comprising a UTF-8 encoded string,
799 C<byte_utf8a_to_utf8n()> takes as input a string of those bytes encoded
800 for an ASCII platform, and returns the equivalent string in the native
801 platform.  For example, C<byte_utf8a_to_utf8n("\xC2\xA0")> returns the
802 byte sequence on the current platform that form the UTF-8 for C<U+00A0>,
803 since C<"\xC2\xA0"> are the UTF-8 bytes on an ASCII platform for that
804 code point.  This function returns C<"\xC2\xA0"> on an ASCII platform, and
805 C<"\x80\x41"> on an EBCDIC 1047 one.
806
807 But easiest is, if the character is specifiable as a literal, like
808 C<"A"> or C<"%">, to use that; if not so specificable, you can use
809 C<\N{}> , if the side effects aren't troublesome.  Simply specify all
810 your characters in hex, using C<\N{U+ZZ}> instead of C<\xZZ>.  C<\N{}>
811 is the Unicode name, and so it
812 always gives you the Unicode character.  C<\N{U+41}> is the character
813 whose Unicode code point is C<0x41>, hence is C<'A'> on all platforms.
814 The side effects are:
815
816 =over 4
817
818 =item *
819
820 These select Unicode rules.  That means that in double-quotish strings,
821 the string is always converted to UTF-8 to force a Unicode
822 interpretation (you can C<utf8::downgrade()> afterwards to convert back
823 to non-UTF8, if possible).  In regular expression patterns, the
824 conversion isn't done, but if the character set modifier would
825 otherwise be C</d>, it is changed to C</u>.
826
827 =item *
828
829 If you use the form C<\N{I<character name>}>, the L<charnames> module
830 gets automatically loaded.  This may not be suitable for the test level
831 you are doing.
832
833 =back
834
835 If you are testing locales (see L<perllocale>), there are helper
836 functions in F<t/loc_tools.pl> to enable you to see what locales there
837 are on the current platform.
838
839 =head2 Special C<make test> targets
840
841 There are various special make targets that can be used to test Perl
842 slightly differently than the standard "test" target.  Not all them are
843 expected to give a 100% success rate.  Many of them have several
844 aliases, and many of them are not available on certain operating
845 systems.
846
847 =over 4
848
849 =item * test_porting
850
851 This runs some basic sanity tests on the source tree and helps catch
852 basic errors before you submit a patch.
853
854 =item * minitest
855
856 Run F<miniperl> on F<t/base>, F<t/comp>, F<t/cmd>, F<t/run>, F<t/io>,
857 F<t/op>, F<t/uni> and F<t/mro> tests.
858
859 F<miniperl> is a minimalistic perl built to bootstrap building
860 extensions, utilties, documentation etc.  It doesn't support dynamic
861 loading and depending on the point in the build process will only have
862 access to a limited set of core modules.  F<miniperl> is not intended
863 for day to day use.
864
865 =item * test.valgrind check.valgrind
866
867 (Only in Linux) Run all the tests using the memory leak + naughty
868 memory access tool "valgrind".  The log files will be named
869 F<testname.valgrind>.
870
871 =item * test_harness
872
873 Run the test suite with the F<t/harness> controlling program, instead
874 of F<t/TEST>.  F<t/harness> is more sophisticated, and uses the
875 L<Test::Harness> module, thus using this test target supposes that perl
876 mostly works.  The main advantage for our purposes is that it prints a
877 detailed summary of failed tests at the end.  Also, unlike F<t/TEST>,
878 it doesn't redirect stderr to stdout.
879
880 Note that under Win32 F<t/harness> is always used instead of F<t/TEST>,
881 so there is no special "test_harness" target.
882
883 Under Win32's "test" target you may use the TEST_SWITCHES and
884 TEST_FILES environment variables to control the behaviour of
885 F<t/harness>.  This means you can say
886
887     nmake test TEST_FILES="op/*.t"
888     nmake test TEST_SWITCHES="-torture" TEST_FILES="op/*.t"
889
890 =item * test-notty test_notty
891
892 Sets PERL_SKIP_TTY_TEST to true before running normal test.
893
894 =back
895
896 =head2 Parallel tests
897
898 The core distribution can now run its regression tests in parallel on
899 Unix-like and Windows platforms.  On Unix, instead of running C<make
900 test>, set C<TEST_JOBS> in your environment to the number of tests to
901 run in parallel, and run C<make test_harness>.  On a Bourne-like shell,
902 this can be done as
903
904     TEST_JOBS=3 make test_harness  # Run 3 tests in parallel
905
906 An environment variable is used, rather than parallel make itself,
907 because L<TAP::Harness> needs to be able to schedule individual
908 non-conflicting test scripts itself, and there is no standard interface
909 to C<make> utilities to interact with their job schedulers.
910
911 Tests are normally run in a logical order, with the sanity tests first,
912 then the main tests of the Perl core functionality, then the tests for
913 the non-core modules.  On many-core systems, this may not use the
914 hardware as effectively as possible.  By also specifying
915
916  TEST_JOBS=19 PERL_TEST_HARNESS_ASAP=1 make -j19 test_harness
917
918 you signal that you want the tests to finish in wall-clock time as short
919 as possible.  After the sanity tests are completed, this causes the
920 remaining ones to be packed into the available cores as tightly as
921 we know how.  This has its greatest effect on slower, many-core systems.
922 Throughput was sped up by 20% on an outmoded 24-core system; less on
923 more recent faster ones with fewer cores.
924
925 Note that the command line above added a C<-j> parameter to make, so as
926 to cause parallel compilation.  This may or may not work on your
927 platform.
928
929 =head2 Running tests by hand
930
931 You can run part of the test suite by hand by using one of the
932 following commands from the F<t/> directory:
933
934     ./perl -I../lib TEST list-of-.t-files
935
936 or
937
938     ./perl -I../lib harness list-of-.t-files
939
940 (If you don't specify test scripts, the whole test suite will be run.)
941
942 =head2 Using F<t/harness> for testing
943
944 If you use C<harness> for testing, you have several command line
945 options available to you.  The arguments are as follows, and are in the
946 order that they must appear if used together.
947
948     harness -v -torture -re=pattern LIST OF FILES TO TEST
949     harness -v -torture -re LIST OF PATTERNS TO MATCH
950
951 If C<LIST OF FILES TO TEST> is omitted, the file list is obtained from
952 the manifest.  The file list may include shell wildcards which will be
953 expanded out.
954
955 =over 4
956
957 =item * -v
958
959 Run the tests under verbose mode so you can see what tests were run,
960 and debug output.
961
962 =item * -torture
963
964 Run the torture tests as well as the normal set.
965
966 =item * -re=PATTERN
967
968 Filter the file list so that all the test files run match PATTERN.
969 Note that this form is distinct from the B<-re LIST OF PATTERNS> form
970 below in that it allows the file list to be provided as well.
971
972 =item * -re LIST OF PATTERNS
973
974 Filter the file list so that all the test files run match
975 /(LIST|OF|PATTERNS)/.  Note that with this form the patterns are joined
976 by '|' and you cannot supply a list of files, instead the test files
977 are obtained from the MANIFEST.
978
979 =back
980
981 You can run an individual test by a command similar to
982
983     ./perl -I../lib path/to/foo.t
984
985 except that the harnesses set up some environment variables that may
986 affect the execution of the test:
987
988 =over 4
989
990 =item * PERL_CORE=1
991
992 indicates that we're running this test as part of the perl core test
993 suite.  This is useful for modules that have a dual life on CPAN.
994
995 =item * PERL_DESTRUCT_LEVEL=2
996
997 is set to 2 if it isn't set already (see
998 L<perlhacktips/PERL_DESTRUCT_LEVEL>).
999
1000 =item * PERL
1001
1002 (used only by F<t/TEST>) if set, overrides the path to the perl
1003 executable that should be used to run the tests (the default being
1004 F<./perl>).
1005
1006 =item * PERL_SKIP_TTY_TEST
1007
1008 if set, tells to skip the tests that need a terminal.  It's actually
1009 set automatically by the Makefile, but can also be forced artificially
1010 by running 'make test_notty'.
1011
1012 =back
1013
1014 =head3 Other environment variables that may influence tests
1015
1016 =over 4
1017
1018 =item * PERL_TEST_Net_Ping
1019
1020 Setting this variable runs all the Net::Ping modules tests, otherwise
1021 some tests that interact with the outside world are skipped.  See
1022 L<perl58delta>.
1023
1024 =item * PERL_TEST_NOVREXX
1025
1026 Setting this variable skips the vrexx.t tests for OS2::REXX.
1027
1028 =item * PERL_TEST_NUMCONVERTS
1029
1030 This sets a variable in op/numconvert.t.
1031
1032 =item * PERL_TEST_MEMORY
1033
1034 Setting this variable includes the tests in F<t/bigmem/>.  This should
1035 be set to the number of gigabytes of memory available for testing, eg.
1036 C<PERL_TEST_MEMORY=4> indicates that tests that require 4GiB of
1037 available memory can be run safely.
1038
1039 =back
1040
1041 See also the documentation for the Test and Test::Harness modules, for
1042 more environment variables that affect testing.
1043
1044 =head2 Performance testing
1045
1046 The file F<t/perf/benchmarks> contains snippets of perl code which are
1047 intended to be benchmarked across a range of perls by the
1048 F<Porting/bench.pl> tool. If you fix or enhance a performance issue, you
1049 may want to add a representative code sample to the file, then run
1050 F<bench.pl> against the previous and current perls to see what difference
1051 it has made, and whether anything else has slowed down as a consequence.
1052
1053 The file F<t/perf/opcount.t> is designed to test whether a particular
1054 code snippet has been compiled into an optree containing specified
1055 numbers of particular op types. This is good for testing whether
1056 optimisations which alter ops, such as converting an C<aelem> op into an
1057 C<aelemfast> op, are really doing that.
1058
1059 The files F<t/perf/speed.t> and F<t/re/speed.t> are designed to test
1060 things that run thousands of times slower if a particular optimisation
1061 is broken (for example, the utf8 length cache on long utf8 strings).
1062 Add a test that will take a fraction of a second normally, and minutes
1063 otherwise, causing the test file to time out on failure.
1064
1065 =head2 Building perl at older commits
1066
1067 In the course of hacking on the Perl core distribution, you may have occasion
1068 to configure, build and test perl at an old commit.  Sometimes C<make> will
1069 fail during this process.  If that happens, you may be able to salvage the
1070 situation by using the Devel::PatchPerl library from CPAN (not included in the
1071 core) to bring the source code at that commit to a buildable state.
1072
1073 Here's a real world example, taken from work done to resolve
1074 L<perl #10118|https://github.com/Perl/perl5/issues/10118>.
1075 Use of F<Porting/bisect.pl> had identified commit
1076 C<ba77e4cc9d1ceebf472c9c5c18b2377ee47062e6> as the commit in which a bug was
1077 corrected.  To confirm, a P5P developer wanted to configure and build perl at
1078 commit C<ba77e4c^> (presumably "bad") and then at C<ba77e4c> (presumably
1079 "good").  Normal configuration and build was attempted:
1080
1081     $ sh ./Configure -des -Dusedevel
1082     $ make test_prep
1083
1084 C<make>, however, failed with output (excerpted) like this:
1085
1086     cc -fstack-protector -L/usr/local/lib -o miniperl \
1087       gv.o toke.o perly.o pad.o regcomp.o dump.o util.o \
1088       mg.o reentr.o mro.o hv.o av.o run.o pp_hot.o sv.o \
1089       pp.o scope.o pp_ctl.o pp_sys.o doop.o doio.o regexec.o \
1090       utf8.o taint.o deb.o universal.o globals.o perlio.o \
1091       numeric.o mathoms.o locale.o pp_pack.o pp_sort.o  \
1092       miniperlmain.o opmini.o perlmini.o
1093     pp.o: In function `Perl_pp_pow':
1094     pp.c:(.text+0x2db9): undefined reference to `pow'
1095     ...
1096     collect2: error: ld returned 1 exit status
1097     makefile:348: recipe for target 'miniperl' failed
1098     make: *** [miniperl] Error 1
1099
1100 Another P5P contributor recommended installation and use of Devel::PatchPerl
1101 for this situation, first to determine the version of perl at the commit in
1102 question, then to patch the source code at that point to facilitate a build.
1103
1104     $ perl -MDevel::PatchPerl -e \
1105         'print Devel::PatchPerl->determine_version("/path/to/sourcecode"), "\n";'
1106     5.11.1
1107     $ perl -MDevel::PatchPerl -e \
1108         'Devel::PatchPerl->patch_source("5.11.1", "/path/to/sourcecode");'
1109
1110 Once the source was patched, C<./Configure> and C<make test_prep> were called
1111 and completed successfully, enabling confirmation of the findings in RT
1112 #72414.
1113
1114 =head1 MORE READING FOR GUTS HACKERS
1115
1116 To hack on the Perl guts, you'll need to read the following things:
1117
1118 =over 4
1119
1120 =item * L<perlsource>
1121
1122 An overview of the Perl source tree.  This will help you find the files
1123 you're looking for.
1124
1125 =item * L<perlinterp>
1126
1127 An overview of the Perl interpreter source code and some details on how
1128 Perl does what it does.
1129
1130 =item * L<perlhacktut>
1131
1132 This document walks through the creation of a small patch to Perl's C
1133 code.  If you're just getting started with Perl core hacking, this will
1134 help you understand how it works.
1135
1136 =item * L<perlhacktips>
1137
1138 More details on hacking the Perl core.  This document focuses on lower
1139 level details such as how to write tests, compilation issues,
1140 portability, debugging, etc.
1141
1142 If you plan on doing serious C hacking, make sure to read this.
1143
1144 =item * L<perlguts>
1145
1146 This is of paramount importance, since it's the documentation of what
1147 goes where in the Perl source.  Read it over a couple of times and it
1148 might start to make sense - don't worry if it doesn't yet, because the
1149 best way to study it is to read it in conjunction with poking at Perl
1150 source, and we'll do that later on.
1151
1152 Gisle Aas's "illustrated perlguts", also known as I<illguts>, has very
1153 helpful pictures:
1154
1155 L<https://metacpan.org/release/RURBAN/illguts-0.49>
1156
1157 =item * L<perlxstut> and L<perlxs>
1158
1159 A working knowledge of XSUB programming is incredibly useful for core
1160 hacking; XSUBs use techniques drawn from the PP code, the portion of
1161 the guts that actually executes a Perl program.  It's a lot gentler to
1162 learn those techniques from simple examples and explanation than from
1163 the core itself.
1164
1165 =item * L<perlapi>
1166
1167 The documentation for the Perl API explains what some of the internal
1168 functions do, as well as the many macros used in the source.
1169
1170 =item * F<Porting/pumpkin.pod>
1171
1172 This is a collection of words of wisdom for a Perl porter; some of it
1173 is only useful to the pumpkin holders, but most of it applies to anyone
1174 wanting to go about Perl development.
1175
1176 =back
1177
1178 =head1 CPAN TESTERS AND PERL SMOKERS
1179
1180 The CPAN testers ( L<http://cpantesters.org/> ) are a group of volunteers
1181 who test CPAN modules on a variety of platforms.
1182
1183 Perl Smokers ( L<https://www.nntp.perl.org/group/perl.daily-build/> and
1184 L<https://www.nntp.perl.org/group/perl.daily-build.reports/> )
1185 automatically test Perl source releases on platforms with various
1186 configurations.
1187
1188 Both efforts welcome volunteers.  In order to get involved in smoke
1189 testing of the perl itself visit
1190 L<https://metacpan.org/release/Test-Smoke>.  In order to start smoke
1191 testing CPAN modules visit
1192 L<https://metacpan.org/release/CPANPLUS-YACSmoke> or
1193 L<https://metacpan.org/release/minismokebox> or
1194 L<https://metacpan.org/release/CPAN-Reporter>.
1195
1196 =head1 WHAT NEXT?
1197
1198 If you've read all the documentation in the document and the ones
1199 listed above, you're more than ready to hack on Perl.
1200
1201 Here's some more recommendations
1202
1203 =over 4
1204
1205 =item *
1206
1207 Subscribe to perl5-porters, follow the patches and try and understand
1208 them; don't be afraid to ask if there's a portion you're not clear on -
1209 who knows, you may unearth a bug in the patch...
1210
1211 =item *
1212
1213 Do read the README associated with your operating system, e.g.
1214 README.aix on the IBM AIX OS.  Don't hesitate to supply patches to that
1215 README if you find anything missing or changed over a new OS release.
1216
1217 =item *
1218
1219 Find an area of Perl that seems interesting to you, and see if you can
1220 work out how it works.  Scan through the source, and step over it in
1221 the debugger.  Play, poke, investigate, fiddle! You'll probably get to
1222 understand not just your chosen area but a much wider range of
1223 F<perl>'s activity as well, and probably sooner than you'd think.
1224
1225 =back
1226
1227 =head2 "The Road goes ever on and on, down from the door where it began."
1228
1229 If you can do these things, you've started on the long road to Perl
1230 porting.  Thanks for wanting to help make Perl better - and happy
1231 hacking!
1232
1233 =head2 Metaphoric Quotations
1234
1235 If you recognized the quote about the Road above, you're in luck.
1236
1237 Most software projects begin each file with a literal description of
1238 each file's purpose.  Perl instead begins each with a literary allusion
1239 to that file's purpose.
1240
1241 Like chapters in many books, all top-level Perl source files (along
1242 with a few others here and there) begin with an epigrammatic
1243 inscription that alludes, indirectly and metaphorically, to the
1244 material you're about to read.
1245
1246 Quotations are taken from writings of J.R.R. Tolkien pertaining to his
1247 Legendarium, almost always from I<The Lord of the Rings>.  Chapters and
1248 page numbers are given using the following editions:
1249
1250 =over 4
1251
1252 =item *
1253
1254 I<The Hobbit>, by J.R.R. Tolkien.  The hardcover, 70th-anniversary
1255 edition of 2007 was used, published in the UK by Harper Collins
1256 Publishers and in the US by the Houghton Mifflin Company.
1257
1258 =item *
1259
1260 I<The Lord of the Rings>, by J.R.R. Tolkien.  The hardcover,
1261 50th-anniversary edition of 2004 was used, published in the UK by
1262 Harper Collins Publishers and in the US by the Houghton Mifflin
1263 Company.
1264
1265 =item *
1266
1267 I<The Lays of Beleriand>, by J.R.R. Tolkien and published posthumously
1268 by his son and literary executor, C.J.R. Tolkien, being the 3rd of the
1269 12 volumes in Christopher's mammoth I<History of Middle Earth>.  Page
1270 numbers derive from the hardcover edition, first published in 1983 by
1271 George Allen & Unwin; no page numbers changed for the special 3-volume
1272 omnibus edition of 2002 or the various trade-paper editions, all again
1273 now by Harper Collins or Houghton Mifflin.
1274
1275 =back
1276
1277 Other JRRT books fair game for quotes would thus include I<The
1278 Adventures of Tom Bombadil>, I<The Silmarillion>, I<Unfinished Tales>,
1279 and I<The Tale of the Children of Hurin>, all but the first
1280 posthumously assembled by CJRT.  But I<The Lord of the Rings> itself is
1281 perfectly fine and probably best to quote from, provided you can find a
1282 suitable quote there.
1283
1284 So if you were to supply a new, complete, top-level source file to add
1285 to Perl, you should conform to this peculiar practice by yourself
1286 selecting an appropriate quotation from Tolkien, retaining the original
1287 spelling and punctuation and using the same format the rest of the
1288 quotes are in.  Indirect and oblique is just fine; remember, it's a
1289 metaphor, so being meta is, after all, what it's for.
1290
1291 =head1 AUTHOR
1292
1293 This document was originally written by Nathan Torkington, and is
1294 maintained by the perl5-porters mailing list.