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