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