Chapter 4
Portability
Perl is perhaps the most portable programming language ever created. Perl
modules can be written to work on virtually every modern computer system.
21
Similarly, Perl modules can also be written to work with a range of versions of Perl.
In general, this is accomplished by limiting your use of certain Perl features be
they features that work differently on different operating systems or features that
are broken or nonexistent in older versions of Perl. This section will give you infor
mation on the most commonly seen portability problems in CPAN modules; for an
exhaustive list, take a look at the perlport document that comes with Perl.
Operating System Independence
Writing operating system independent code requires you to know which features
might behave differently when used on different operating systems. This topic
alone would be enough to fill a book at least as large as this one, but next you'll find
information on the most commonly encountered portability problems.
Line Endings
Historically operating systems have had different ideas about what to put at the
end of a line in a text file. UNIX systems use a single byte \012 or LF. MacOS also
uses a single byte \015 or CR. Microsoft's operating systems (DOS and Windows)
use 2 bytes \015\012 or CRLF. However, in Microsoft's stdio
22
implementation,
CRLF is translated into LF on input and back into CRLF on output, but only when
reading or writing text files. As a result, in memory the lines have UNIX line endings
but on disk they have the distinctive 2 byte format.
Perl provides the \n escape code for strings and regular expressions that
matches what the platform thinks of as a line ending be it CR or LF so you usually
don't need to think about it when reading and writing text files. However, the issue
has implications for the handling of binary data. Consider the following CGI code
that reads in a binary image file from disk, smoothes it, and prints it to STDOUT:
open(IMAGE, "image.jpg") or die $!; # open the image file
my $image = join( ,); # read the image data into $image
smooth($image); # smooth the image
print STDOUT $image; # print out the image to the client
21. With the notable exception of small scale embedded systems too small for Perl 5. Perl 6
aims to address this deficiency. See the perlport documentation for a full list of currently
supported systems.
22. The system library used by Perl to do file I/O
122
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved