📄 perlport.1
字号:
\& for (my $x = 0; $x <= 10000000; ++$x) {} # good\&\& @lines = <VERY_LARGE_FILE>; # bad\&\& while (<FILE>) {$file .= $_} # sometimes bad\& $file = join(\*(Aq\*(Aq, <FILE>); # better.Ve.PPThe last two constructs may appear unintuitive to most people. Thefirst repeatedly grows a string, whereas the second allocates alarge chunk of memory in one go. On some systems, the second ismore efficient that the first..Sh "Security".IX Subsection "Security"Most multi-user platforms provide basic levels of security, usuallyimplemented at the filesystem level. Some, however, donot\*(-- unfortunately. Thus the notion of user id, or \*(L"home\*(R" directory,or even the state of being logged-in, may be unrecognizable on manyplatforms. If you write programs that are security-conscious, itis usually best to know what type of system you will be runningunder so that you can write code explicitly for that platform (orclass of platforms)..PPDon't assume the \s-1UNIX\s0 filesystem access semantics: the operatingsystem or the filesystem may be using some \s-1ACL\s0 systems, which arericher languages than the usual rwx. Even if the rwx exist,their semantics might be different..PP(From security viewpoint testing for permissions before attempting todo something is silly anyway: if one tries this, there is potentialfor race conditions\*(-- someone or something might change thepermissions between the permissions check and the actual operation.Just try the operation.).PPDon't assume the \s-1UNIX\s0 user and group semantics: especially, don'texpect the \f(CW$<\fR and \f(CW$>\fR (or the \f(CW$(\fR and \f(CW$)\fR) to workfor switching identities (or memberships)..PPDon't assume set-uid and set-gid semantics. (And even if you do,think twice: set-uid and set-gid are a known can of security worms.).Sh "Style".IX Subsection "Style"For those times when it is necessary to have platform-specific code,consider keeping the platform-specific code in one place, making portingto other platforms easier. Use the Config module and the specialvariable \f(CW$^O\fR to differentiate platforms, as described in\&\*(L"\s-1PLATFORMS\s0\*(R"..PPBe careful in the tests you supply with your module or programs.Module code may be fully portable, but its tests might not be. Thisoften happens when tests spawn off other processes or call externalprograms to aid in the testing, or when (as noted above) the testsassume certain things about the filesystem and paths. Be careful notto depend on a specific output style for errors, such as when checking\&\f(CW$!\fR after a failed system call. Using \f(CW$!\fR for anything else thandisplaying it as output is doubtful (though see the Errno module fortesting reasonably portably for error value). Some platforms expecta certain output format, and Perl on those platforms may have beenadjusted accordingly. Most specifically, don't anchor a regex whentesting an error value..SH "CPAN Testers".IX Header "CPAN Testers"Modules uploaded to \s-1CPAN\s0 are tested by a variety of volunteers ondifferent platforms. These \s-1CPAN\s0 testers are notified by mail of eachnew upload, and reply to the list with \s-1PASS\s0, \s-1FAIL\s0, \s-1NA\s0 (not applicable tothis platform), or \s-1UNKNOWN\s0 (unknown), along with any relevant notations..PPThe purpose of the testing is twofold: one, to help developers fix anyproblems in their code that crop up because of lack of testing on otherplatforms; two, to provide users with information about whethera given module works on a given platform..PPAlso see:.IP "\(bu" 4Mailing list: cpan\-testers@perl.org.IP "\(bu" 4Testing results: http://testers.cpan.org/.SH "PLATFORMS".IX Header "PLATFORMS"As of version 5.002, Perl is built with a \f(CW$^O\fR variable thatindicates the operating system it was built on. This was implementedto help speed up code that would otherwise have to \f(CW\*(C`use Config\*(C'\fRand use the value of \f(CW$Config{osname}\fR. Of course, to get moredetailed information about the system, looking into \f(CW%Config\fR iscertainly recommended..PP\&\f(CW%Config\fR cannot always be trusted, however, because it was builtat compile time. If perl was built in one place, then transferredelsewhere, some values may be wrong. The values may even have beenedited after the fact..Sh "Unix".IX Subsection "Unix"Perl works on a bewildering variety of Unix and Unix-like platforms (seee.g. most of the files in the \fIhints/\fR directory in the source code kit).On most of these systems, the value of \f(CW$^O\fR (hence \f(CW$Config{\*(Aqosname\*(Aq}\fR,too) is determined either by lowercasing and stripping punctuation from thefirst field of the string returned by typing \f(CW\*(C`uname \-a\*(C'\fR (or a similar command)at the shell prompt or by testing the file system for the presence ofuniquely named files such as a kernel or header file. Here, for example,are a few of the more popular Unix flavors:.PP.Vb 10\& uname $^O $Config{\*(Aqarchname\*(Aq}\& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\& AIX aix aix\& BSD/OS bsdos i386\-bsdos\& Darwin darwin darwin\& dgux dgux AViiON\-dgux\& DYNIX/ptx dynixptx i386\-dynixptx\& FreeBSD freebsd freebsd\-i386 \& Linux linux arm\-linux\& Linux linux i386\-linux\& Linux linux i586\-linux\& Linux linux ppc\-linux\& HP\-UX hpux PA\-RISC1.1\& IRIX irix irix\& Mac OS X darwin darwin\& MachTen PPC machten powerpc\-machten\& NeXT 3 next next\-fat\& NeXT 4 next OPENSTEP\-Mach\& openbsd openbsd i386\-openbsd\& OSF1 dec_osf alpha\-dec_osf\& reliantunix\-n svr4 RM400\-svr4\& SCO_SV sco_sv i386\-sco_sv\& SINIX\-N svr4 RM400\-svr4\& sn4609 unicos CRAY_C90\-unicos\& sn6521 unicosmk t3e\-unicosmk\& sn9617 unicos CRAY_J90\-unicos\& SunOS solaris sun4\-solaris\& SunOS solaris i86pc\-solaris\& SunOS4 sunos sun4\-sunos.Ve.PPBecause the value of \f(CW$Config{archname}\fR may depend on thehardware architecture, it can vary more than the value of \f(CW$^O\fR..Sh "\s-1DOS\s0 and Derivatives".IX Subsection "DOS and Derivatives"Perl has long been ported to Intel-style microcomputers running undersystems like PC-DOS, MS-DOS, \s-1OS/2\s0, and most Windows platforms you canbring yourself to mention (except for Windows \s-1CE\s0, if you count that).Users familiar with \fI\s-1COMMAND\s0.COM\fR or \fI\s-1CMD\s0.EXE\fR style shells shouldbe aware that each of these file specifications may have subtledifferences:.PP.Vb 4\& $filespec0 = "c:/foo/bar/file.txt";\& $filespec1 = "c:\e\efoo\e\ebar\e\efile.txt";\& $filespec2 = \*(Aqc:\efoo\ebar\efile.txt\*(Aq;\& $filespec3 = \*(Aqc:\e\efoo\e\ebar\e\efile.txt\*(Aq;.Ve.PPSystem calls accept either \f(CW\*(C`/\*(C'\fR or \f(CW\*(C`\e\*(C'\fR as the path separator.However, many command-line utilities of \s-1DOS\s0 vintage treat \f(CW\*(C`/\*(C'\fR asthe option prefix, so may get confused by filenames containing \f(CW\*(C`/\*(C'\fR.Aside from calling any external programs, \f(CW\*(C`/\*(C'\fR will work just fine,and probably better, as it is more consistent with popular usage,and avoids the problem of remembering what to backwhack and whatnot to..PPThe \s-1DOS\s0 \s-1FAT\s0 filesystem can accommodate only \*(L"8.3\*(R" style filenames. Underthe \*(L"case-insensitive, but case-preserving\*(R" \s-1HPFS\s0 (\s-1OS/2\s0) and \s-1NTFS\s0 (\s-1NT\s0)filesystems you may have to be careful about case returned with functionslike \f(CW\*(C`readdir\*(C'\fR or used with functions like \f(CW\*(C`open\*(C'\fR or \f(CW\*(C`opendir\*(C'\fR..PP\&\s-1DOS\s0 also treats several filenames as special, such as \s-1AUX\s0, \s-1PRN\s0,\&\s-1NUL\s0, \s-1CON\s0, \s-1COM1\s0, \s-1LPT1\s0, \s-1LPT2\s0, etc. Unfortunately, sometimes thesefilenames won't even work if you include an explicit directoryprefix. It is best to avoid such filenames, if you want your codeto be portable to \s-1DOS\s0 and its derivatives. It's hard to know whatthese all are, unfortunately..PPUsers of these operating systems may also wish to make use ofscripts such as \fIpl2bat.bat\fR or \fIpl2cmd\fR toput wrappers around your scripts..PPNewline (\f(CW\*(C`\en\*(C'\fR) is translated as \f(CW\*(C`\e015\e012\*(C'\fR by \s-1STDIO\s0 when reading fromand writing to files (see \*(L"Newlines\*(R"). \f(CW\*(C`binmode(FILEHANDLE)\*(C'\fRwill keep \f(CW\*(C`\en\*(C'\fR translated as \f(CW\*(C`\e012\*(C'\fR for that filehandle. Since it is ano-op on other systems, \f(CW\*(C`binmode\*(C'\fR should be used for cross-platform codethat deals with binary data. That's assuming you realize in advancethat your data is in binary. General-purpose programs shouldoften assume nothing about their data..PPThe \f(CW$^O\fR variable and the \f(CW$Config{archname}\fR values for variousDOSish perls are as follows:.PP.Vb 10\& OS $^O $Config{archname} ID Version\& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\& MS\-DOS dos ? \& PC\-DOS dos ? \& OS/2 os2 ?\& Windows 3.1 ? ? 0 3 01\& Windows 95 MSWin32 MSWin32\-x86 1 4 00\& Windows 98 MSWin32 MSWin32\-x86 1 4 10\& Windows ME MSWin32 MSWin32\-x86 1 ?\& Windows NT MSWin32 MSWin32\-x86 2 4 xx\& Windows NT MSWin32 MSWin32\-ALPHA 2 4 xx\& Windows NT MSWin32 MSWin32\-ppc 2 4 xx\& Windows 2000 MSWin32 MSWin32\-x86 2 5 00\& Windows XP MSWin32 MSWin32\-x86 2 5 01\& Windows 2003 MSWin32 MSWin32\-x86 2 5 02\& Windows CE MSWin32 ? 3 \& Cygwin cygwin cygwin.Ve.PPThe various MSWin32 Perl's can distinguish the \s-1OS\s0 they are running onvia the value of the fifth element of the list returned from \&\fIWin32::GetOSVersion()\fR. For example:.PP.Vb 4\& if ($^O eq \*(AqMSWin32\*(Aq) {\& my @os_version_info = Win32::GetOSVersion();\& print +(\*(Aq3.1\*(Aq,\*(Aq95\*(Aq,\*(AqNT\*(Aq)[$os_version_info[4]],"\en";\& }.Ve.PPThere are also \fIWin32::IsWinNT()\fR and \fIWin32::IsWin95()\fR, try \f(CW\*(C`perldoc Win32\*(C'\fR,and as of libwin32 0.19 (not part of the core Perl distribution)\&\fIWin32::GetOSName()\fR. The very portable \fIPOSIX::uname()\fR will work too:.PP.Vb 2\& c:\e> perl \-MPOSIX \-we "print join \*(Aq|\*(Aq, uname"\& Windows NT|moonru|5.0|Build 2195 (Service Pack 2)|x86.Ve.PPAlso see:.IP "\(bu" 4The djgpp environment for \s-1DOS\s0, http://www.delorie.com/djgpp/and perldos..IP "\(bu" 4The \s-1EMX\s0 environment for \s-1DOS\s0, \s-1OS/2\s0, etc. emx@iaehv.nl,http://www.leo.org/pub/comp/os/os2/leo/gnu/emx+gcc/index.html orftp://hobbes.nmsu.edu/pub/os2/dev/emx/ Also perlos2..IP "\(bu" 4Build instructions for Win32 in perlwin32, or under the Cygnus environmentin perlcygwin..IP "\(bu" 4The \f(CW\*(C`Win32::*\*(C'\fR modules in Win32..IP "\(bu" 4The ActiveState Pages, http://www.activestate.com/.IP "\(bu" 4The Cygwin environment for Win32; \fI\s-1README\s0.cygwin\fR (installed as perlcygwin), http://www.cygwin.com/.IP "\(bu" 4The U/WIN environment for Win32,http://www.research.att.com/sw/tools/uwin/.IP "\(bu" 4Build instructions for \s-1OS/2\s0, perlos2.Sh "Mac\ \s-1OS\s0".IX Subsection "MacOS"Any module requiring \s-1XS\s0 compilation is right out for most people, becauseMacPerl is built using non-free (and non-cheap!) compilers. Some \s-1XS\s0modules that can work with MacPerl are built and distributed in binaryform on \s-1CPAN\s0..PPDirectories are specified as:.PP.Vb 6\& volume:folder:file for absolute pathnames\& volume:folder: for absolute pathnames\& :folder:file for relative pathnames\& :folder: for relative pathnames\& :file for relative pathnames\& file for relative pathnames.Ve.PPFiles are stored in the directory in alphabetical order. Filenames arelimited to 31 characters, and may include any character except fornull and \f(CW\*(C`:\*(C'\fR, which is reserved as the path separator..PPInstead of \f(CW\*(C`flock\*(C'\fR, see \f(CW\*(C`FSpSetFLock\*(C'\fR and \f(CW\*(C`FSpRstFLock\*(C'\fR in theMac::Files module, or \f(CW\*(C`chmod(0444, ...)\*(C'\fR and \f(CW\*(C`chmod(0666, ...)\*(C'\fR..PPIn the MacPerl application, you can't run a program from the command line;programs that expect \f(CW@ARGV\fR to be populated can be edited with somethinglike the following, which brings up a dialog box asking for the commandline arguments..PP.Vb 3\& if (!@ARGV) {\& @ARGV = split /\es+/, MacPerl::Ask(\*(AqArguments?\*(Aq);\& }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -