📄 perlhack.pod
字号:
=head1 NAMEperlhack - How to hack at the Perl internals=head1 DESCRIPTIONThis document attempts to explain how Perl development takes place,and ends with some suggestions for people wanting to become bona fideporters.The perl5-porters mailing list is where the Perl standard distributionis maintained and developed. The list can get anywhere from 10 to 150messages a day, depending on the heatedness of the debate. Most daysthere are two or three patches, extensions, features, or bugs beingdiscussed at a time.A searchable archive of the list is at either: http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/or http://archive.develooper.com/perl5-porters@perl.org/List subscribers (the porters themselves) come in several flavours.Some are quiet curious lurkers, who rarely pitch in and instead watchthe ongoing development to ensure they're forewarned of new changes orfeatures in Perl. Some are representatives of vendors, who are thereto make sure that Perl continues to compile and work on theirplatforms. Some patch any reported bug that they know how to fix,some are actively patching their pet area (threads, Win32, the regexpengine), while others seem to do nothing but complain. In otherwords, it's your usual mix of technical people.Over this group of porters presides Larry Wall. He has the final wordin what does and does not change in the Perl language. Variousreleases of Perl are shepherded by a "pumpking", a porterresponsible for gathering patches, deciding on a patch-by-patch,feature-by-feature basis what will and will not go into the release.For instance, Gurusamy Sarathy was the pumpking for the 5.6 release ofPerl, and Jarkko Hietaniemi was the pumpking for the 5.8 release, andRafael Garcia-Suarez holds the pumpking crown for the 5.10 release.In addition, various people are pumpkings for different things. Forinstance, Andy Dougherty and Jarkko Hietaniemi did a grand job as theI<Configure> pumpkin up till the 5.8 release. For the 5.10 releaseH.Merijn Brand took over.Larry sees Perl development along the lines of the US government:there's the Legislature (the porters), the Executive branch (thepumpkings), and the Supreme Court (Larry). The legislature candiscuss and submit patches to the executive branch all they like, butthe executive branch is free to veto them. Rarely, the Supreme Courtwill side with the executive branch over the legislature, or thelegislature over the executive branch. Mostly, however, thelegislature and the executive branch are supposed to get along andwork out their differences without impeachment or court cases.You might sometimes see reference to Rule 1 and Rule 2. Larry's poweras Supreme Court is expressed in The Rules:=over 4=item 1Larry is always by definition right about how Perl should behave.This means he has final veto power on the core functionality.=item 2Larry is allowed to change his mind about any matter at a later date,regardless of whether he previously invoked Rule 1.=backGot that? Larry is always right, even when he was wrong. It's rareto see either Rule exercised, but they are often alluded to.New features and extensions to the language are contentious, becausethe criteria used by the pumpkings, Larry, and other porters to decidewhich features should be implemented and incorporated are not codifiedin a few small design goals as with some other languages. Instead,the heuristics are flexible and often difficult to fathom. Here isone person's list, roughly in decreasing order of importance, ofheuristics that new features have to be weighed against:=over 4=item Does concept match the general goals of Perl?These haven't been written anywhere in stone, but one approximationis: 1. Keep it fast, simple, and useful. 2. Keep features/concepts as orthogonal as possible. 3. No arbitrary limits (platforms, data sizes, cultures). 4. Keep it open and exciting to use/patch/advocate Perl everywhere. 5. Either assimilate new technologies, or build bridges to them.=item Where is the implementation?All the talk in the world is useless without an implementation. Inalmost every case, the person or people who argue for a new featurewill be expected to be the ones who implement it. Porters capableof coding new features have their own agendas, and are not availableto implement your (possibly good) idea.=item Backwards compatibilityIt's a cardinal sin to break existing Perl programs. New warnings arecontentious--some say that a program that emits warnings is notbroken, while others say it is. Adding keywords has the potential tobreak programs, changing the meaning of existing token sequences orfunctions might break programs.=item Could it be a module instead?Perl 5 has extension mechanisms, modules and XS, specifically to avoidthe need to keep changing the Perl interpreter. You can write modulesthat export functions, you can give those functions prototypes so theycan be called like built-in functions, you can even write XS code tomess with the runtime data structures of the Perl interpreter if youwant to implement really complicated things. If it can be done in amodule instead of in the core, it's highly unlikely to be added.=item Is the feature generic enough?Is this something that only the submitter wants added to the language,or would it be broadly useful? Sometimes, instead of adding a featurewith a tight focus, the porters might decide to wait until someoneimplements the more generalized feature. For instance, instead ofimplementing a "delayed evaluation" feature, the porters are waitingfor a macro system that would permit delayed evaluation and much more.=item Does it potentially introduce new bugs?Radical rewrites of large chunks of the Perl interpreter have thepotential to introduce new bugs. The smaller and more localized thechange, the better.=item Does it preclude other desirable features?A patch is likely to be rejected if it closes off future avenues ofdevelopment. For instance, a patch that placed a true and finalinterpretation on prototypes is likely to be rejected because thereare still options for the future of prototypes that haven't beenaddressed.=item Is the implementation robust?Good patches (tight code, complete, correct) stand more chance ofgoing in. Sloppy or incorrect patches might be placed on the backburner until the pumpking has time to fix, or might be discardedaltogether without further notice.=item Is the implementation generic enough to be portable?The worst patches make use of a system-specific features. It's highlyunlikely that non-portable additions to the Perl language will beaccepted.=item Is the implementation tested?Patches which change behaviour (fixing bugs or introducing new features)must include regression tests to verify that everything works as expected.Without tests provided by the original author, how can anyone else changingperl in the future be sure that they haven't unwittingly broken the behaviourthe patch implements? And without tests, how can the patch's author beconfident that his/her hard work put into the patch won't be accidentallythrown away by someone in the future?=item Is there enough documentation?Patches without documentation are probably ill-thought out orincomplete. Nothing can be added without documentation, so submittinga patch for the appropriate manpages as well as the source code isalways a good idea.=item Is there another way to do it?Larry said "Although the Perl Slogan is I<There's More Than One Wayto Do It>, I hesitate to make 10 ways to do something". This is atricky heuristic to navigate, though--one man's essential addition isanother man's pointless cruft.=item Does it create too much work?Work for the pumpking, work for Perl programmers, work for moduleauthors, ... Perl is supposed to be easy.=item Patches speak louder than wordsWorking code is always preferred to pie-in-the-sky ideas. A patch toadd a feature stands a much higher chance of making it to the languagethan does a random feature request, no matter how fervently argued therequest might be. This ties into "Will it be useful?", as the factthat someone took the time to make the patch demonstrates a strongdesire for the feature.=backIf you're on the list, you might hear the word "core" bandiedaround. It refers to the standard distribution. "Hacking on thecore" means you're changing the C source code to the Perlinterpreter. "A core module" is one that ships with Perl.=head2 Keeping in syncThe source code to the Perl interpreter, in its different versions, iskept in a repository managed by a revision control system ( which iscurrently the Perforce program, see http://perforce.com/ ). Thepumpkings and a few others have access to the repository to check inchanges. Periodically the pumpking for the development version of Perlwill release a new version, so the rest of the porters can see what'schanged. The current state of the main trunk of repository, and patchesthat describe the individual changes that have happened since the lastpublic release are available at this location: http://public.activestate.com/pub/apc/ ftp://public.activestate.com/pub/apc/If you're looking for a particular change, or a change that affecteda particular set of files, you may find the B<Perl Repository Browser>useful: http://public.activestate.com/cgi-bin/perlbrowseYou may also want to subscribe to the perl5-changes mailing list toreceive a copy of each patch that gets submitted to the maintenanceand development "branches" of the perl repository. Seehttp://lists.perl.org/ for subscription information.If you are a member of the perl5-porters mailing list, it is a goodthing to keep in touch with the most recent changes. If not only toverify if what you would have posted as a bug report isn't alreadysolved in the most recent available perl development branch, alsoknown as perl-current, bleading edge perl, bleedperl or bleadperl.Needless to say, the source code in perl-current is usually in a perpetualstate of evolution. You should expect it to be very buggy. Do B<not> useit for any purpose other than testing and development.Keeping in sync with the most recent branch can be done in several ways,but the most convenient and reliable way is using B<rsync>, available atftp://rsync.samba.org/pub/rsync/ . (You can also get the most recentbranch by FTP.)If you choose to keep in sync using rsync, there are two approachesto doing so:=over 4=item rsync'ing the source treePresuming you are in the directory where your perl source residesand you have rsync installed and available, you can "upgrade" tothe bleadperl using: # rsync -avz rsync://public.activestate.com/perl-current/ .This takes care of updating every single item in the source tree tothe latest applied patch level, creating files that are new (to yourdistribution) and setting date/time stamps of existing files toreflect the bleadperl status.Note that this will not delete any files that were in '.' beforethe rsync. Once you are sure that the rsync is running correctly,run it with the --delete and the --dry-run options like this: # rsync -avz --delete --dry-run rsync://public.activestate.com/perl-current/ .This will I<simulate> an rsync run that also deletes files notpresent in the bleadperl master copy. Observe the results fromthis run closely. If you are sure that the actual run would deleteno files precious to you, you could remove the '--dry-run' option.You can than check what patch was the latest that was applied bylooking in the file B<.patch>, which will show the number of thelatest patch.If you have more than one machine to keep in sync, and not all ofthem have access to the WAN (so you are not able to rsync all thesource trees to the real source), there are some ways to get aroundthis problem.=over 4=item Using rsync over the LANSet up a local rsync server which makes the rsynced source treeavailable to the LAN and sync the other machines against thisdirectory.From http://rsync.samba.org/README.html : "Rsync uses rsh or ssh for communication. It does not need to be setuid and requires no special privileges for installation. It does not require an inetd entry or a daemon. You must, however, have a working rsh or ssh system. Using ssh is recommended for its security features."=item Using pushing over the NFSHaving the other systems mounted over the NFS, you can take anactive pushing approach by checking the just updated tree againstthe other not-yet synced trees. An example would be #!/usr/bin/perl -w use strict; use File::Copy; my %MF = map { m/(\S+)/; $1 => [ (stat $1)[2, 7, 9] ]; # mode, size, mtime } `cat MANIFEST`; my %remote = map { $_ => "/$_/pro/3gl/CPAN/perl-5.7.1" } qw(host1 host2); foreach my $host (keys %remote) { unless (-d $remote{$host}) { print STDERR "Cannot Xsync for host $host\n"; next; } foreach my $file (keys %MF) { my $rfile = "$remote{$host}/$file"; my ($mode, $size, $mtime) = (stat $rfile)[2, 7, 9]; defined $size or ($mode, $size, $mtime) = (0, 0, 0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -