A world with only one operating system makes portability easy, and life boring. We prefer a larger genetic pool of operating systems, as long as the ecosystem doesn't divide too cleanly into predators and prey. Perl runs on dozens of operating systems, and because Perl programs aren't platform dependent, the same program can run on all of those systems without modification.
Well, almost. Perl tries to give the programmer as many features as possible, but if you make use of features particular to a certain operating system, you'll necessarily reduce the portability of your program to other systems. In this section, we'll provide some guidelines for writing portable Perl code. Once you make a decision about how portable you want to be, you'll know where the lines are drawn, and you can stay within them.
Looking at it another way, writing portable code is usually about willfully limiting your available choices. Naturally, it takes discipline and sacrifice to do that, two traits that Perl programmers might be unaccustomed to.
Be aware that not all Perl programs have to be portable. There is no reason not to use Perl to glue Unix tools together, or to prototype a Macintosh application, or to manage the Windows registry. If it makes sense to sacrifice portability, go ahead.[1]
In general, note that the notions of a user ID, a "home" directory, and even the state of being logged in will exist only on multi-user platforms.
The special $^O
variable tells you
what operating system your Perl was built on. This is provided to speed
up code that would otherwise have to use Config
to
get the same information via $Config{osname}
. (Even
if you've pulled in Config
for other reasons, it
still saves you the price of a tied-hash lookup.)
To get more detailed information about the platform, you
can look at the rest of the information in the
%Config
hash, which is made available by the standard
Config
module. For example, to check whether the
platform has the lstat
call, you can check
$Config{d_lstat}
. See Config
's
online documentation for a full description of available variables, and
the perlport manpage for a listing of the behavior
of Perl built-in functions on different platforms. Here are the Perl
functions whose behavior varies the most across platforms:
-
X
(file tests),
accept
, alarm
,
bind
, binmode
,
chmod
, chown
,
chroot
, connect
,
crypt
, dbmclose
,
dbmopen
, dump
,
endgrent
, endhostent
,
endnetent
, endprotoent
,
endpwent
, endservent
,
exec
, fcntl
,
fileno
, flock
,
fork
, getgrent
,
getgrgid
, getgrnam
,
gethostbyaddr
, gethostbyname
,
gethostent
, getlogin
,
getnetbyaddr
, getnetbyname
,
getnetent
, getpeername
,
getpgrp
, getppid
,
getpriority
, getprotobyname
,
getprotobynumber
, getprotoent
,
getpwent
, getpwnam
,
getpwuid
, getservbyport
,
getservent
, getservbyname
,
getsockname
, getsockopt
,
glob
, ioctl
,
kill
, link
,
listen
, lstat
,
msgctl
, msgget
,
msgrcv
, msgsnd
,
open
, pipe
, qx
,
readlink
, readpipe
,
recv
, select
,
semctl
, semget
,
semop
, send
,
sethostent
, setgrent
,
setnetent
, setpgrp
,
setpriority
, setprotoent
,
setpwent
, setservent
,
setsockopt
, shmctl
,
shmget
, shmread
,
shmwrite
, shutdown
,
socket
, socketpair
,
stat
, symlink
,
syscall
, sysopen
,
system
, times
,
truncate
, umask
,
utime
, wait
,
waitpid