This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Mention 7-zip as alternative to WinZip
[perl5.git] / pod / perlhack.pod
index 6403bb1..e62f7b0 100644 (file)
@@ -29,9 +29,19 @@ with the following command:
 
   % git clone git://perl5.git.perl.org/perl.git perl
 
+=item * Ensure you're following the latest advice
+
+In case the advice in this guide has been updated recently, read the
+latest version directly from the perl source:
+
+  % perldoc pod/perlhack.pod
+
 =item * Make your change
 
-Hack, hack, hack.
+Hack, hack, hack.  Keep in mind that Perl runs on many different
+platforms, with different operating systems that have different
+capabilities, different filesystem organizations, and even different
+character sets.  L<perlhacktips> gives advice on this.
 
 =item * Test your change
 
@@ -56,22 +66,51 @@ sentence.  For example, "Fixed spelling errors in perlhack.pod".
 The next step is to submit your patch to the Perl core ticket system
 via email.
 
-Assuming your patch consists of a single git commit, the following
-writes the file as a MIME attachment, and sends it with a meaningful
-subject:
+If your changes are in a single git commit, run the following commands
+to generate the patch file and attach it to your bug report:
 
- % git format-patch -1 --attach
% perlbug -s "[PATCH] $(git log -1 --oneline HEAD)" -f 0001-*.patch
+  % git format-patch -1
 % ./perl -Ilib utils/perlbug -p 0001-*.patch
 
 The perlbug program will ask you a few questions about your email
 address and the patch you're submitting.  Once you've answered them it
 will submit your patch via email.
 
+If your changes are in multiple commits, generate a patch file for each
+one and provide them to perlbug's C<-p> option separated by commas:
+
+  % git format-patch -3
+  % ./perl -Ilib utils/perlbug -p 0001-fix1.patch,0002-fix2.patch,\
+  > 0003-fix3.patch
+
+When prompted, pick a subject that summarizes your changes.
+
 =item * Thank you
 
 The porters appreciate the time you spent helping to make Perl better.
 Thank you!
 
+=item * Acknowledgement
+
+All contributors are credited (by name and email address) in the
+AUTHORS file, which is part of the perl distribution, as well as the
+Git commit history.
+
+If you don’t want to be included in the AUTHORS file, just let us
+know. Otherwise we will take your submission of a patch as permission
+to credit you in the AUTHORS file.
+
+=item * Next time
+
+The next time you wish to make a patch, you need to start from the
+latest perl in a pristine state.  Check you don't have any local changes
+or added files in your perl check-out which you wish to keep, then run
+these commands:
+
+  % git pull
+  % git reset --hard origin/blead
+  % git clean -dxf
+
 =back
 
 =head1 BUG REPORTING
@@ -81,7 +120,7 @@ command line tool.  This tool will ensure that your bug report includes
 all the relevant system and configuration information.
 
 To browse existing Perl bugs and patches, you can use the web interface
-at L<http://rt.perl.org/>.
+at L<https://rt.perl.org/>.
 
 Please check the archive of the perl5-porters list (see below) and/or
 the bug tracking system before submitting a bug report.  Often, you'll
@@ -99,15 +138,14 @@ are also referred to as the "Perl 5 Porters", "p5p" or just the
 "porters".
 
 A searchable archive of the list is available at
-L<http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/>.  There is
-also another archive at
-L<http://archive.develooper.com/perl5-porters@perl.org/>.
+L<https://markmail.org/search/?q=perl5-porters>.  There is also an archive at
+L<https://archive.develooper.com/perl5-porters@perl.org/>.
 
 =head2 perl-changes mailing list
 
 The perl5-changes mailing list receives a copy of each patch that gets
 submitted to the maintenance and development branches of the perl
-repository.  See L<http://lists.perl.org/list/perl5-changes.html> for
+repository.  See L<https://lists.perl.org/list/perl5-changes.html> for
 subscription and archive information.
 
 =head2 #p5p on IRC
@@ -119,8 +157,8 @@ Perl core.
 =head1 GETTING THE PERL SOURCE
 
 All of Perl's source code is kept centrally in a Git repository at
-I<perl5.git.perl.org>.  The repository contains many Perl revisions from
-Perl 1 onwards and all the revisions from Perforce, the previous
+I<perl5.git.perl.org>.  The repository contains many Perl revisions
+from Perl 1 onwards and all the revisions from Perforce, the previous
 version control system.
 
 For much more detail on using git with the Perl repository, please see
@@ -139,33 +177,33 @@ directory.
 If you cannot use the git protocol for firewall reasons, you can also
 clone via http, though this is much slower:
 
-  % git clone http://perl5.git.perl.org/perl.git perl
+  % git clone https://perl5.git.perl.org/perl.git perl
 
 =head2 Read access via the web
 
 You may access the repository over the web.  This allows you to browse
 the tree, see recent commits, subscribe to RSS feeds for the changes,
 search for particular commits and more.  You may access it at
-L<http://perl5.git.perl.org/perl.git>.  A mirror of the repository is
-found at L<http://github.com/mirrors/perl>.
+L<https://perl5.git.perl.org/perl.git>.  A mirror of the repository is
+found at L<https://github.com/Perl/perl5>.
 
 =head2 Read access via rsync
 
 You can also choose to use rsync to get a copy of the current source
 tree for the bleadperl branch and all maintenance branches:
 
-    % rsync -avz rsync://perl5.git.perl.org/perl-current .
-    % rsync -avz rsync://perl5.git.perl.org/perl-5.12.x .
-    % rsync -avz rsync://perl5.git.perl.org/perl-5.10.x .
-    % rsync -avz rsync://perl5.git.perl.org/perl-5.8.x .
-    % rsync -avz rsync://perl5.git.perl.org/perl-5.6.x .
-    % rsync -avz rsync://perl5.git.perl.org/perl-5.005xx .
+  % rsync -avz rsync://perl5.git.perl.org/perl-current .
+  % rsync -avz rsync://perl5.git.perl.org/perl-5.12.x .
+  % rsync -avz rsync://perl5.git.perl.org/perl-5.10.x .
+  % rsync -avz rsync://perl5.git.perl.org/perl-5.8.x .
+  % rsync -avz rsync://perl5.git.perl.org/perl-5.6.x .
+  % rsync -avz rsync://perl5.git.perl.org/perl-5.005xx .
 
 (Add the C<--delete> option to remove leftover files.)
 
 To get a full list of the available sync points:
 
-    % rsync perl5.git.perl.org::
+  % rsync perl5.git.perl.org::
 
 =head2 Write access via git
 
@@ -191,6 +229,9 @@ email from our ticket tracking system.  This email will give you a
 ticket number.  Once your patch has made it to the ticket tracking
 system, it will also be sent to the perl5-porters@perl.org list.
 
+If your patch is related to an already-opened ticket you can also
+attach your patch to that ticket, without having to use perlbug.
+
 Patches are reviewed and discussed on the p5p list.  Simple,
 uncontroversial patches will usually be applied without any discussion.
 When the patch is applied, the ticket will be updated and you will
@@ -206,9 +247,10 @@ in a month.  Please remember that the Perl 5 developers are all
 volunteers, and be polite.
 
 Changes are always applied directly to the main development branch,
-called "blead".  Some patches may be backported to a maintenance branch.
-If you think your patch is appropriate for the maintenance branch,
-please explain why when you submit it.
+called "blead".  Some patches may be backported to a maintenance
+branch.  If you think your patch is appropriate for the maintenance
+branch (see L<perlpolicy/MAINTENANCE BRANCHES>), please explain why
+when you submit it.
 
 =head2 Getting your patch accepted
 
@@ -220,8 +262,8 @@ can do to help the Perl 5 Porters accept your patch.
 If you used git to check out the Perl source, then using C<git
 format-patch> will produce a patch in a style suitable for Perl.  The
 C<format-patch> command produces one patch file for each commit you
-made.  If you prefer to send a single patch for all commits, you can use
-C<git diff>.
+made.  If you prefer to send a single patch for all commits, you can
+use C<git diff>.
 
   % git checkout blead
   % git pull
@@ -305,12 +347,12 @@ readers understand what you did and why you did it.
 
 =head3 Comments, Comments, Comments
 
-Be sure to adequately comment your code.  While commenting every line is
-unnecessary, anything that takes advantage of side effects of
+Be sure to adequately comment your code.  While commenting every line
+is unnecessary, anything that takes advantage of side effects of
 operators, that creates changes that will be felt outside of the
 function being patched, or that others may find confusing should be
-documented.  If you are going to err, it is better to err on the side of
-adding too many comments than too few.
+documented.  If you are going to err, it is better to err on the side
+of adding too many comments than too few.
 
 The best comments explain I<why> the code does what it does, not I<what
 it does>.
@@ -327,11 +369,16 @@ sources:
 
 =item *
 
-8-wide tabs (no exceptions!)
+4-wide indents for code, 2-wide indents for nested CPP C<#define>s,
+with 8-wide tabstops.
 
 =item *
 
-4-wide indents for code, 2-wide indents for nested CPP #defines
+Use spaces for indentation, not tab characters.
+
+The codebase is a mixture of tabs and spaces for indentation, and we
+are moving to spaces only.  Converting lines you're patching from 8-wide
+tabs to spaces will help this migration.
 
 =item *
 
@@ -360,7 +407,7 @@ should be at end-of-line otherwise
 
 =item *
 
-In function definitions, name starts in column 0 (return value is on
+In function definitions, name starts in column 0 (return value-type is on
 previous line)
 
 =item *
@@ -393,8 +440,8 @@ In-line functions that are in headers that are accessible to XS code
 need to be able to compile without warnings with commonly used extra
 compilation flags, such as gcc's C<-Wswitch-default> which warns
 whenever a switch statement does not have a "default" case.  The use of
-these extra flags is to catch potential problems in legal C code, and is
-often used by Perl aggregators, such as Linux distributors.
+these extra flags is to catch potential problems in legal C code, and
+is often used by Perl aggregators, such as Linux distributors.
 
 =back
 
@@ -508,8 +555,8 @@ core.
 
 For changes significant enough to warrant a F<pod/perldelta.pod> entry,
 the porters will greatly appreciate it if you submit a delta entry
-along with your actual change.  Significant changes include, but are not
-limited to:
+along with your actual change.  Significant changes include, but are
+not limited to:
 
 =over 4
 
@@ -692,9 +739,9 @@ authors, ... Perl is supposed to be easy.
 Working code is always preferred to pie-in-the-sky ideas.  A patch to
 add a feature stands a much higher chance of making it to the language
 than does a random feature request, no matter how fervently argued the
-request might be.  This ties into "Will it be useful?", as the fact that
-someone took the time to make the patch demonstrates a strong desire
-for the feature.
+request might be.  This ties into "Will it be useful?", as the fact
+that someone took the time to make the patch demonstrates a strong
+desire for the feature.
 
 =head1 TESTING
 
@@ -703,27 +750,28 @@ The core uses the same testing style as the rest of Perl, a simple
 considerations.
 
 There are three ways to write a test in the core: L<Test::More>,
-F<t/test.pl> and ad hoc C<print $test ? "ok 42\n" : "not ok 42\n">.  The
-decision of which to use depends on what part of the test suite you're
-working on.  This is a measure to prevent a high-level failure (such as
-Config.pm breaking) from causing basic functionality tests to fail.
+F<t/test.pl> and ad hoc C<print $test ? "ok 42\n" : "not ok 42\n">.
+The decision of which to use depends on what part of the test suite
+you're working on.  This is a measure to prevent a high-level failure
+(such as Config.pm breaking) from causing basic functionality tests to
+fail.
 
 The F<t/test.pl> library provides some of the features of
 L<Test::More>, but avoids loading most modules and uses as few core
 features as possible.
 
 If you write your own test, use the L<Test Anything
-Protocol|http://testanything.org>.
+Protocol|https://testanything.org>.
 
 =over 4
 
 =item * F<t/base>, F<t/comp> and F<t/opbasic>
 
-Since we don't know if require works, or even subroutines, use ad hoc
+Since we don't know if C<require> works, or even subroutines, use ad hoc
 tests for these three.  Step carefully to avoid using the feature being
-tested.  Tests in F<t/opbasic>, for instance, have been placed there rather
-than in F<t/op> because they test functionality which F<t/test.pl> presumes
-has already been demonstrated to work.
+tested.  Tests in F<t/opbasic>, for instance, have been placed there
+rather than in F<t/op> because they test functionality which
+F<t/test.pl> presumes has already been demonstrated to work.
 
 =item * F<t/cmd>, F<t/run>, F<t/io> and F<t/op>
 
@@ -741,14 +789,65 @@ used.  You can also use the full suite of core modules in the tests.
 =back
 
 When you say "make test", Perl uses the F<t/TEST> program to run the
-test suite (except under Win32 where it uses F<t/harness> instead).  All
-tests are run from the F<t/> directory, B<not> the directory which
-contains the test.  This causes some problems with the tests in F<lib/>,
-so here's some opportunity for some patching.
+test suite (except under Win32 where it uses F<t/harness> instead).
+All tests are run from the F<t/> directory, B<not> the directory which
+contains the test.  This causes some problems with the tests in
+F<lib/>, so here's some opportunity for some patching.
 
 You must be triply conscious of cross-platform concerns.  This usually
-boils down to using L<File::Spec> and avoiding things like C<fork()>
-and C<system()> unless absolutely necessary.
+boils down to using L<File::Spec>, avoiding things like C<fork()>
+and C<system()> unless absolutely necessary, and not assuming that a
+given character has a particular ordinal value (code point) or that its
+UTF-8 representation is composed of particular bytes.
+
+There are several functions available to specify characters and code
+points portably in tests.  The always-preloaded functions
+C<utf8::unicode_to_native()> and its inverse
+C<utf8::native_to_unicode()> take code points and translate
+appropriately.  The file F<t/charset_tools.pl> has several functions
+that can be useful.  It has versions of the previous two functions
+that take strings as inputs -- not single numeric code points:
+C<uni_to_native()> and C<native_to_uni()>.  If you must look at the
+individual bytes comprising a UTF-8 encoded string,
+C<byte_utf8a_to_utf8n()> takes as input a string of those bytes encoded
+for an ASCII platform, and returns the equivalent string in the native
+platform.  For example, C<byte_utf8a_to_utf8n("\xC2\xA0")> returns the
+byte sequence on the current platform that form the UTF-8 for C<U+00A0>,
+since C<"\xC2\xA0"> are the UTF-8 bytes on an ASCII platform for that
+code point.  This function returns C<"\xC2\xA0"> on an ASCII platform, and
+C<"\x80\x41"> on an EBCDIC 1047 one.
+
+But easiest is, if the character is specifiable as a literal, like
+C<"A"> or C<"%">, to use that; if not so specificable, you can use use
+C<\N{}> , if the side effects aren't troublesome.  Simply specify all
+your characters in hex, using C<\N{U+ZZ}> instead of C<\xZZ>.  C<\N{}>
+is the Unicode name, and so it
+always gives you the Unicode character.  C<\N{U+41}> is the character
+whose Unicode code point is C<0x41>, hence is C<'A'> on all platforms.
+The side effects are:
+
+=over 4
+
+=item *
+
+These select Unicode rules.  That means that in double-quotish strings,
+the string is always converted to UTF-8 to force a Unicode
+interpretation (you can C<utf8::downgrade()> afterwards to convert back
+to non-UTF8, if possible).  In regular expression patterns, the
+conversion isn't done, but if the character set modifier would
+otherwise be C</d>, it is changed to C</u>.
+
+=item *
+
+If you use the form C<\N{I<character name>}>, the L<charnames> module
+gets automatically loaded.  This may not be suitable for the test level
+you are doing.
+
+=back
+
+If you are testing locales (see L<perllocale>), there are helper
+functions in F<t/loc_tools.pl> to enable you to see what locales there
+are on the current platform.
 
 =head2 Special C<make test> targets
 
@@ -782,8 +881,8 @@ Run the test suite with the F<t/harness> controlling program, instead
 of F<t/TEST>.  F<t/harness> is more sophisticated, and uses the
 L<Test::Harness> module, thus using this test target supposes that perl
 mostly works.  The main advantage for our purposes is that it prints a
-detailed summary of failed tests at the end.  Also, unlike F<t/TEST>, it
-doesn't redirect stderr to stdout.
+detailed summary of failed tests at the end.  Also, unlike F<t/TEST>,
+it doesn't redirect stderr to stdout.
 
 Note that under Win32 F<t/harness> is always used instead of F<t/TEST>,
 so there is no special "test_harness" target.
@@ -816,8 +915,8 @@ non-conflicting test scripts itself, and there is no standard interface
 to C<make> utilities to interact with their job schedulers.
 
 Note that currently some test scripts may fail when run in parallel
-(most notably F<ext/IO/t/io_dir.t>).  If necessary, run just the failing
-scripts again sequentially and see if the failures go away.
+(most notably F<dist/IO/t/io_dir.t>).  If necessary, run just the
+failing scripts again sequentially and see if the failures go away.
 
 =head2 Running tests by hand
 
@@ -858,9 +957,9 @@ Run the torture tests as well as the normal set.
 
 =item * -re=PATTERN
 
-Filter the file list so that all the test files run match PATTERN.  Note
-that this form is distinct from the B<-re LIST OF PATTERNS> form below
-in that it allows the file list to be provided as well.
+Filter the file list so that all the test files run match PATTERN.
+Note that this form is distinct from the B<-re LIST OF PATTERNS> form
+below in that it allows the file list to be provided as well.
 
 =item * -re LIST OF PATTERNS
 
@@ -898,9 +997,9 @@ F<./perl>).
 
 =item * PERL_SKIP_TTY_TEST
 
-if set, tells to skip the tests that need a terminal.  It's actually set
-automatically by the Makefile, but can also be forced artificially by
-running 'make test_notty'.
+if set, tells to skip the tests that need a terminal.  It's actually
+set automatically by the Makefile, but can also be forced artificially
+by running 'make test_notty'.
 
 =back
 
@@ -925,8 +1024,8 @@ This sets a variable in op/numconvert.t.
 =item * PERL_TEST_MEMORY
 
 Setting this variable includes the tests in F<t/bigmem/>.  This should
-be set to the number of gigabytes of memory available for testing,
-eg. C<PERL_TEST_MEMORY=4> indicates that tests that require 4GiB of
+be set to the number of gigabytes of memory available for testing, eg.
+C<PERL_TEST_MEMORY=4> indicates that tests that require 4GiB of
 available memory can be run safely.
 
 =back
@@ -934,6 +1033,76 @@ available memory can be run safely.
 See also the documentation for the Test and Test::Harness modules, for
 more environment variables that affect testing.
 
+=head2 Performance testing
+
+The file F<t/perf/benchmarks> contains snippets of perl code which are
+intended to be benchmarked across a range of perls by the
+F<Porting/bench.pl> tool. If you fix or enhance a performance issue, you
+may want to add a representative code sample to the file, then run
+F<bench.pl> against the previous and current perls to see what difference
+it has made, and whether anything else has slowed down as a consequence.
+
+The file F<t/perf/opcount.t> is designed to test whether a particular
+code snippet has been compiled into an optree containing specified
+numbers of particular op types. This is good for testing whether
+optimisations which alter ops, such as converting an C<aelem> op into an
+C<aelemfast> op, are really doing that.
+
+The files F<t/perf/speed.t> and F<t/re/speed.t> are designed to test
+things that run thousands of times slower if a particular optimisation
+is broken (for example, the utf8 length cache on long utf8 strings).
+Add a test that will take a fraction of a second normally, and minutes
+otherwise, causing the test file to time out on failure.
+
+=head2 Building perl at older commits
+
+In the course of hacking on the Perl core distribution, you may have occasion
+to configure, build and test perl at an old commit.  Sometimes C<make> will
+fail during this process.  If that happens, you may be able to salvage the
+situation by using the Devel::PatchPerl library from CPAN (not included in the
+core) to bring the source code at that commit to a buildable state.
+
+Here's a real world example, taken from work done to resolve
+L<perl #72414|https://rt.perl.org/Ticket/Display.html?id=72414>.
+Use of F<Porting/bisect.pl> had identified commit
+C<ba77e4cc9d1ceebf472c9c5c18b2377ee47062e6> as the commit in which a bug was
+corrected.  To confirm, a P5P developer wanted to configure and build perl at
+commit C<ba77e4c^> (presumably "bad") and then at C<ba77e4c> (presumably
+"good").  Normal configuration and build was attempted:
+
+    $ sh ./Configure -des -Dusedevel
+    $ make test_prep
+
+C<make>, however, failed with output (excerpted) like this:
+
+    cc -fstack-protector -L/usr/local/lib -o miniperl \
+      gv.o toke.o perly.o pad.o regcomp.o dump.o util.o \
+      mg.o reentr.o mro.o hv.o av.o run.o pp_hot.o sv.o \
+      pp.o scope.o pp_ctl.o pp_sys.o doop.o doio.o regexec.o \
+      utf8.o taint.o deb.o universal.o globals.o perlio.o \
+      perlapi.o numeric.o mathoms.o locale.o pp_pack.o pp_sort.o  \
+      miniperlmain.o opmini.o perlmini.o
+    pp.o: In function `Perl_pp_pow':
+    pp.c:(.text+0x2db9): undefined reference to `pow'
+    ...
+    collect2: error: ld returned 1 exit status
+    makefile:348: recipe for target 'miniperl' failed
+    make: *** [miniperl] Error 1
+
+Another P5P contributor recommended installation and use of Devel::PatchPerl
+for this situation, first to determine the version of perl at the commit in
+question, then to patch the source code at that point to facilitate a build.
+
+    $ perl -MDevel::PatchPerl -e \
+        'print Devel::PatchPerl->determine_version("/path/to/sourcecode"), "\n";'
+    5.11.1
+    $ perl -MDevel::PatchPerl -e \
+        'Devel::PatchPerl->patch_source("5.11.1", "/path/to/sourcecode");'
+
+Once the source was patched, C<./Configure> and C<make test_prep> were called
+and completed successfully, enabling confirmation of the findings in RT
+#72414.
+
 =head1 MORE READING FOR GUTS HACKERS
 
 To hack on the Perl guts, you'll need to read the following things:
@@ -975,7 +1144,7 @@ source, and we'll do that later on.
 Gisle Aas's "illustrated perlguts", also known as I<illguts>, has very
 helpful pictures:
 
-L<http://search.cpan.org/dist/illguts/>
+L<https://search.cpan.org/dist/illguts/>
 
 =item * L<perlxstut> and L<perlxs>
 
@@ -1000,21 +1169,21 @@ wanting to go about Perl development.
 
 =head1 CPAN TESTERS AND PERL SMOKERS
 
-The CPAN testers ( http://testers.cpan.org/ ) are a group of volunteers
+The CPAN testers ( L<http://cpantesters.org/> ) are a group of volunteers
 who test CPAN modules on a variety of platforms.
 
-Perl Smokers ( http://www.nntp.perl.org/group/perl.daily-build/ and
-http://www.nntp.perl.org/group/perl.daily-build.reports/ )
+Perl Smokers ( L<https://www.nntp.perl.org/group/perl.daily-build/> and
+L<https://www.nntp.perl.org/group/perl.daily-build.reports/> )
 automatically test Perl source releases on platforms with various
 configurations.
 
 Both efforts welcome volunteers.  In order to get involved in smoke
 testing of the perl itself visit
-L<http://search.cpan.org/dist/Test-Smoke/>.  In order to start smoke
+L<https://search.cpan.org/dist/Test-Smoke/>.  In order to start smoke
 testing CPAN modules visit
-L<http://search.cpan.org/dist/CPANPLUS-YACSmoke/> or
-L<http://search.cpan.org/dist/minismokebox/> or
-L<http://search.cpan.org/dist/CPAN-Reporter/>.
+L<https://search.cpan.org/dist/CPANPLUS-YACSmoke/> or
+L<https://search.cpan.org/dist/minismokebox/> or
+L<https://search.cpan.org/dist/CPAN-Reporter/>.
 
 =head1 WHAT NEXT?
 
@@ -1040,8 +1209,8 @@ README if you find anything missing or changed over a new OS release.
 =item *
 
 Find an area of Perl that seems interesting to you, and see if you can
-work out how it works.  Scan through the source, and step over it in the
-debugger.  Play, poke, investigate, fiddle! You'll probably get to
+work out how it works.  Scan through the source, and step over it in
+the debugger.  Play, poke, investigate, fiddle! You'll probably get to
 understand not just your chosen area but a much wider range of
 F<perl>'s activity as well, and probably sooner than you'd think.
 
@@ -1115,4 +1284,3 @@ metaphor, so being meta is, after all, what it's for.
 
 This document was originally written by Nathan Torkington, and is
 maintained by the perl5-porters mailing list.
-