📄 perlport.1
字号:
.PPThis example is preferred over the previous one\*(--even for Unixplatforms\*(--because now any \f(CW\*(C`\e015\*(C'\fR's (\f(CW\*(C`\ecM\*(C'\fR's) are stripped out(and there was much rejoicing)..PPSimilarly, functions that return text data\*(--such as a function thatfetches a web page\*(--should sometimes translate newlines beforereturning the data, if they've not yet been translated to the localnewline representation. A single line of code will often suffice:.PP.Vb 2\& $data =~ s/\e015?\e012/\en/g;\& return $data;.Ve.PPSome of this may be confusing. Here's a handy reference to the \s-1ASCII\s0 \s-1CR\s0and \s-1LF\s0 characters. You can print it out and stick it in your wallet..PP.Vb 2\& LF eq \e012 eq \ex0A eq \ecJ eq chr(10) eq ASCII 10\& CR eq \e015 eq \ex0D eq \ecM eq chr(13) eq ASCII 13\&\& | Unix | DOS | Mac |\& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\& \en | LF | LF | CR |\& \er | CR | CR | LF |\& \en * | LF | CRLF | CR |\& \er * | CR | CR | LF |\& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\& * text\-mode STDIO.Ve.PPThe Unix column assumes that you are not accessing a serial line(like a tty) in canonical mode. If you are, then \s-1CR\s0 on input becomes\&\*(L"\en\*(R", and \*(L"\en\*(R" on output becomes \s-1CRLF\s0..PPThese are just the most common definitions of \f(CW\*(C`\en\*(C'\fR and \f(CW\*(C`\er\*(C'\fR in Perl.There may well be others. For example, on an \s-1EBCDIC\s0 implementationsuch as z/OS (\s-1OS/390\s0) or \s-1OS/400\s0 (using the \s-1ILE\s0, the \s-1PASE\s0 is ASCII-based)the above material is similar to \*(L"Unix\*(R" but the code numbers change:.PP.Vb 4\& LF eq \e025 eq \ex15 eq \ecU eq chr(21) eq CP\-1047 21\& LF eq \e045 eq \ex25 eq chr(37) eq CP\-0037 37\& CR eq \e015 eq \ex0D eq \ecM eq chr(13) eq CP\-1047 13\& CR eq \e015 eq \ex0D eq \ecM eq chr(13) eq CP\-0037 13\&\& | z/OS | OS/400 |\& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\& \en | LF | LF |\& \er | CR | CR |\& \en * | LF | LF |\& \er * | CR | CR |\& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\& * text\-mode STDIO.Ve.Sh "Numbers endianness and Width".IX Subsection "Numbers endianness and Width"Different CPUs store integers and floating point numbers in differentorders (called \fIendianness\fR) and widths (32\-bit and 64\-bit being themost common today). This affects your programs when they attempt to transfernumbers in binary format from one \s-1CPU\s0 architecture to another,usually either \*(L"live\*(R" via network connection, or by storing thenumbers to secondary storage such as a disk file or tape..PPConflicting storage orders make utter mess out of the numbers. If alittle-endian host (Intel, \s-1VAX\s0) stores 0x12345678 (305419896 indecimal), a big-endian host (Motorola, Sparc, \s-1PA\s0) reads it as0x78563412 (2018915346 in decimal). Alpha and \s-1MIPS\s0 can be either:Digital/Compaq used/uses them in little-endian mode; SGI/Cray usesthem in big-endian mode. To avoid this problem in network (socket)connections use the \f(CW\*(C`pack\*(C'\fR and \f(CW\*(C`unpack\*(C'\fR formats \f(CW\*(C`n\*(C'\fR and \f(CW\*(C`N\*(C'\fR, the\&\*(L"network\*(R" orders. These are guaranteed to be portable..PPAs of perl 5.9.2, you can also use the \f(CW\*(C`>\*(C'\fR and \f(CW\*(C`<\*(C'\fR modifiersto force big\- or little-endian byte-order. This is useful if you wantto store signed integers or 64\-bit integers, for example..PPYou can explore the endianness of your platform by unpacking adata structure packed in native format such as:.PP.Vb 3\& print unpack("h*", pack("s2", 1, 2)), "\en";\& # \*(Aq10002000\*(Aq on e.g. Intel x86 or Alpha 21064 in little\-endian mode\& # \*(Aq00100020\*(Aq on e.g. Motorola 68040.Ve.PPIf you need to distinguish between endian architectures you could useeither of the variables set like so:.PP.Vb 2\& $is_big_endian = unpack("h*", pack("s", 1)) =~ /01/;\& $is_little_endian = unpack("h*", pack("s", 1)) =~ /^1/;.Ve.PPDiffering widths can cause truncation even between platforms of equalendianness. The platform of shorter width loses the upper parts of thenumber. There is no good solution for this problem except to avoidtransferring or storing raw binary numbers..PPOne can circumnavigate both these problems in two ways. Eithertransfer and store numbers always in text format, instead of rawbinary, or else consider using modules like Data::Dumper (included inthe standard distribution as of Perl 5.005) and Storable (included asof perl 5.8). Keeping all data as text significantly simplifies matters..PPThe v\-strings are portable only up to v2147483647 (0x7FFFFFFF), that'show far \s-1EBCDIC\s0, or more precisely UTF-EBCDIC will go..Sh "Files and Filesystems".IX Subsection "Files and Filesystems"Most platforms these days structure files in a hierarchical fashion.So, it is reasonably safe to assume that all platforms support thenotion of a \*(L"path\*(R" to uniquely identify a file on the system. Howthat path is really written, though, differs considerably..PPAlthough similar, file path specifications differ between Unix,Windows, Mac\ \s-1OS\s0, \s-1OS/2\s0, \s-1VMS\s0, \s-1VOS\s0, \s-1RISC\s0\ \s-1OS\s0, and probably others.Unix, for example, is one of the few OSes that has the elegant ideaof a single root directory..PP\&\s-1DOS\s0, \s-1OS/2\s0, \s-1VMS\s0, \s-1VOS\s0, and Windows can work similarly to Unix with \f(CW\*(C`/\*(C'\fRas path separator, or in their own idiosyncratic ways (such as havingseveral root directories and various \*(L"unrooted\*(R" device files such \s-1NIL:\s0and \s-1LPT:\s0)..PPMac\ \s-1OS\s0 uses \f(CW\*(C`:\*(C'\fR as a path separator instead of \f(CW\*(C`/\*(C'\fR..PPThe filesystem may support neither hard links (\f(CW\*(C`link\*(C'\fR) norsymbolic links (\f(CW\*(C`symlink\*(C'\fR, \f(CW\*(C`readlink\*(C'\fR, \f(CW\*(C`lstat\*(C'\fR)..PPThe filesystem may support neither access timestamp nor changetimestamp (meaning that about the only portable timestamp is themodification timestamp), or one second granularity of any timestamps(e.g. the \s-1FAT\s0 filesystem limits the time granularity to two seconds)..PPThe \*(L"inode change timestamp\*(R" (the \f(CW\*(C`\-C\*(C'\fR filetest) may really be the\&\*(L"creation timestamp\*(R" (which it is not in \s-1UNIX\s0)..PP\&\s-1VOS\s0 perl can emulate Unix filenames with \f(CW\*(C`/\*(C'\fR as path separator. Thenative pathname characters greater-than, less-than, number-sign, andpercent-sign are always accepted..PP\&\s-1RISC\s0\ \s-1OS\s0 perl can emulate Unix filenames with \f(CW\*(C`/\*(C'\fR as pathseparator, or go native and use \f(CW\*(C`.\*(C'\fR for path separator and \f(CW\*(C`:\*(C'\fR tosignal filesystems and disk names..PPDon't assume \s-1UNIX\s0 filesystem access semantics: that read, write,and execute are all the permissions there are, and even if they exist,that their semantics (for example what do r, w, and x mean ona directory) are the \s-1UNIX\s0 ones. The various \s-1UNIX/POSIX\s0 compatibilitylayers usually try to make interfaces like \fIchmod()\fR work, but sometimesthere simply is no good mapping..PPIf all this is intimidating, have no (well, maybe only a little)fear. There are modules that can help. The File::Spec modulesprovide methods to do the Right Thing on whatever platform happensto be running the program..PP.Vb 6\& use File::Spec::Functions;\& chdir(updir()); # go up one directory\& $file = catfile(curdir(), \*(Aqtemp\*(Aq, \*(Aqfile.txt\*(Aq);\& # on Unix and Win32, \*(Aq./temp/file.txt\*(Aq\& # on Mac OS, \*(Aq:temp:file.txt\*(Aq\& # on VMS, \*(Aq[.temp]file.txt\*(Aq.Ve.PPFile::Spec is available in the standard distribution as of version5.004_05. File::Spec::Functions is only in File::Spec 0.7 and later,and some versions of perl come with version 0.6. If File::Specis not updated to 0.7 or later, you must use the object-orientedinterface from File::Spec (or upgrade File::Spec)..PPIn general, production code should not have file paths hardcoded.Making them user-supplied or read from a configuration file isbetter, keeping in mind that file path syntax varies on differentmachines..PPThis is especially noticeable in scripts like Makefiles and test suites,which often assume \f(CW\*(C`/\*(C'\fR as a path separator for subdirectories..PPAlso of use is File::Basename from the standard distribution, whichsplits a pathname into pieces (base filename, full path to directory,and file suffix)..PPEven when on a single platform (if you can call Unix a single platform),remember not to count on the existence or the contents of particularsystem-specific files or directories, like \fI/etc/passwd\fR,\&\fI/etc/sendmail.conf\fR, \fI/etc/resolv.conf\fR, or even \fI/tmp/\fR. Forexample, \fI/etc/passwd\fR may exist but not contain the encryptedpasswords, because the system is using some form of enhanced security.Or it may not contain all the accounts, because the system is using \s-1NIS\s0. If code does need to rely on such a file, include a description of thefile and its format in the code's documentation, then make it easy forthe user to override the default location of the file..PPDon't assume a text file will end with a newline. They should,but people forget..PPDo not have two files or directories of the same name with differentcase, like \fItest.pl\fR and \fITest.pl\fR, as many platforms havecase-insensitive (or at least case-forgiving) filenames. Also, trynot to have non-word characters (except for \f(CW\*(C`.\*(C'\fR) in the names, andkeep them to the 8.3 convention, for maximum portability, onerous aburden though this may appear..PPLikewise, when using the AutoSplit module, try to keep your functions to8.3 naming and case-insensitive conventions; or, at the least,make it so the resulting files have a unique (case-insensitively)first 8 characters..PPWhitespace in filenames is tolerated on most systems, but not all,and even on systems where it might be tolerated, some utilitiesmight become confused by such whitespace..PPMany systems (\s-1DOS\s0, \s-1VMS\s0 \s-1ODS\-2\s0) cannot have more than one \f(CW\*(C`.\*(C'\fR in theirfilenames..PPDon't assume \f(CW\*(C`>\*(C'\fR won't be the first character of a filename.Always use \f(CW\*(C`<\*(C'\fR explicitly to open a file for reading, or evenbetter, use the three-arg version of open, unless you want the user tobe able to specify a pipe open..PP.Vb 1\& open(FILE, \*(Aq<\*(Aq, $existing_file) or die $!;.Ve.PPIf filenames might use strange characters, it is safest to open itwith \f(CW\*(C`sysopen\*(C'\fR instead of \f(CW\*(C`open\*(C'\fR. \f(CW\*(C`open\*(C'\fR is magic and cantranslate characters like \f(CW\*(C`>\*(C'\fR, \f(CW\*(C`<\*(C'\fR, and \f(CW\*(C`|\*(C'\fR, which maybe the wrong thing to do. (Sometimes, though, it's the right thing.)Three-arg open can also help protect against this translation in caseswhere it is undesirable..PPDon't use \f(CW\*(C`:\*(C'\fR as a part of a filename since many systems use that fortheir own semantics (Mac \s-1OS\s0 Classic for separating pathname components,many networking schemes and utilities for separating the nodename andthe pathname, and so on). For the same reasons, avoid \f(CW\*(C`@\*(C'\fR, \f(CW\*(C`;\*(C'\fR and\&\f(CW\*(C`|\*(C'\fR..PPDon't assume that in pathnames you can collapse two leading slashes\&\f(CW\*(C`//\*(C'\fR into one: some networking and clustering filesystems have specialsemantics for that. Let the operating system to sort it out..PPThe \fIportable filename characters\fR as defined by \s-1ANSI\s0 C are.PP.Vb 4\& a b c d e f g h i j k l m n o p q r t u v w x y z\& A B C D E F G H I J K L M N O P Q R T U V W X Y Z\& 0 1 2 3 4 5 6 7 8 9\& . _ \-.Ve.PPand the \*(L"\-\*(R" shouldn't be the first character. If you want to behypercorrect, stay case-insensitive and within the 8.3 namingconvention (all the files and directories have to be unique within onedirectory if their names are lowercased and truncated to eightcharacters before the \f(CW\*(C`.\*(C'\fR, if any, and to three characters after the\&\f(CW\*(C`.\*(C'\fR, if any). (And do not use \f(CW\*(C`.\*(C'\fRs in directory names.).Sh "System Interaction".IX Subsection "System Interaction"Not all platforms provide a command line. These are usually platformsthat rely primarily on a Graphical User Interface (\s-1GUI\s0) for userinteraction. A program requiring a command line interface mightnot work everywhere. This is probably for the user of the programto deal with, so don't stay up late worrying about it..PPSome platforms can't delete or rename files held open by the system,this limitation may also apply to changing filesystem metainformationlike file permissions or owners. Remember to \f(CW\*(C`close\*(C'\fR files when youare done with them. Don't \f(CW\*(C`unlink\*(C'\fR or \f(CW\*(C`rename\*(C'\fR an open file. Don't\&\f(CW\*(C`tie\*(C'\fR or \f(CW\*(C`open\*(C'\fR a file already tied or opened; \f(CW\*(C`untie\*(C'\fR or \f(CW\*(C`close\*(C'\fRit first..PPDon't open the same file more than once at a time for writing, as someoperating systems put mandatory locks on such files..PPDon't assume that write/modify permission on a directory gives theright to add or delete files/directories in that directory. That isfilesystem specific: in some filesystems you need write/modifypermission also (or even just) in the file/directory itself. In somefilesystems (\s-1AFS\s0, \s-1DFS\s0) the permission to add/delete directory entriesis a completely separate permission.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -