Vous êtes sur la page 1sur 4

PERL – Cross platform development tips

PERL – Cross platform development tips

Page 1 of 4
PERL – Cross platform development tips

Introduction
This document is intended for PERL development, where development is done
on one environment and deployment is to be done on different environment. Here
Perl refers to Perl batch programs not in reference to web development in Perl CGI.
Though Perl is interpreted and termed as platform independent, Perl programs
cannot be used as it is in different environments, mainly where Perl enables user to
access OS specific functionalities. This document may give Idea about type of
problems faced when development environment is different from deployment
environment, but will not be sufficient to get complete picture of it as this is written
from experiences from PERL development on windows and deployment on UNIX
system only.

Perl normally used for calling modules/jobs/programs which are developed using
some tools and not able to achieve complete functionality as a system. E.g. in data-
warehousing environment ETL tools are preferred for actual data transportation but
triggering of these jobs on some event needs some other interface. Because of this
Perl normally need to use OS specific commands and hence create problems in cross
platform development.

Page 2 of 4
PERL – Cross platform development tips

1. PATH and File names


Though Perl file operation commands are not OS dependant, it requires
filename, Path as parameter. We also store file names, paths in variables, we
do several activities like separating file name from complete path that is why
this point needs to be considered.

Windows uses “\” whereas Unix uses “/” .


Also windows complete filename starts from drive name e.g. “c:\temp\...”
whereas UNIX absolute file path starts from root e.g. “/usr/bin”
Unix and windows also has different structure for storing information about
file and directory owner, read/write privileges.

To avoid code changes due to this while deployment

Instead of hard coding or defining constant at the beginning of the program


for filenames and paths, maintain separate file only for these variables and
include that in Perl script. It is good programming practice also but it makes
huge difference in this type of case for maintenance of code.

In case of splitting file name from complete path use variable to hold value of
separator ( \ or / ). Refer same from include file.

2. File transfer
ftp or rcp are most commonly used methods for transferring files from
different machines on network. rcp being specific to Unix can not be used
when development on different platform. It cannot be tested if development is
on windows.

ftp is in different network layer and independent of OS, so ftp commands like
cd, lcd, get ,put are independent of OS. But the command interface with OS is
different. Though it is accessible from OS command prompt with command
ftp, options / flags in Unix and windows are very much different. Also path
and filenames within ftp commands becomes OS specific as described in
earlier bullet point.

For taking ftp commands from file in windows it looks like


ftp –s:<ftpcommandsfile>

whereas in unix it would be


ftp < <ftpcommandsfile>

where <ftpcommandsfile> is created dynamically depending upon files to be


transferred.

Page 3 of 4
PERL – Cross platform development tips

In Perl ‘system’ command is used to trigger OS commands. To make ftp


command independent of OS command can be created dynamically referring
variable from include file.

e.g. in include file define variable


for Windows value will be
$FTP_Command = ‘ftp –s:’
For Unix
$FTP_Command = ‘ftp <’

3. Triggering child processes

Many times we need to trigger child processes so as to achieve parallel


processing. ‘fork’ is popular Perl command to trigger child processes where
parent process has control over child process. But this is not supported in
windows, so we need to make use of ‘OPEN’ or ‘SYSTEM’.

4. Sending Mail
Unix mail command may be the simplest way to send mails through scripts
but Perl Mail module can be useful if scripts need to be OS independent.

5. Other OS commands
There are lot of simple options in Unix scripts for doing small things. But if we
need to have scripts OS independent, there are options in Perl for each of
them, may not be as straightforward as shell command but definitely achive
the functionality.

wc –l can be replaced by a perl routine to open file and just scan thru to count
lines.
There is pattern matching in perl to replace grep in Unix.
Commands for Opening, closing, deleting file, checking existence of file in perl
are already OS independent.

Page 4 of 4

Vous aimerez peut-être aussi