develooper Front page | perl.beginners | Postings from April 2010

Writing Perl scripts to run on any OS'es

Thread Next
From:
newbie01 perl
Date:
April 19, 2010 02:53
Subject:
Writing Perl scripts to run on any OS'es
Message ID:
p2zb74aaa0f1004190252qeec5af40j68cee02e64fb789d@mail.gmail.com
Hi all,

Am wanting to change my Perl scripts to get it to run on several UNIX
flavours and Windows. At the moment, the only way I can think of how to
accomplish that is always having the following checks on each and every sub
that I have.

Can anyone please suggest if there is a better way of doing this besides
what am doing now? Am not sure whether creating a module for each OS to use
is the solution although I don't know how to create a module anyway. I found
one tutorial and get lost somewhere along the way on how to create a module
... :-)


if ($^O eq 'MSWin32') {
    system "dir $ARGV[0]";
}
elsif ($^O eq 'solaris') {
    system "ls -l $ARGV[0]";
}
elsif ($^O eq 'AIX') {
    system "ls -l $ARGV[0]";
}
else {
    warn "$0: WARNING: Program might not work on '$^O'\n";
}

Anyway, the other solution that am looking at is creating another file that
contains all the function calls and then I make the OS specific commands on
those file/s instead of the Perl script. For example, if I have a Perl
script that simply contains run_df(), then the run_df() sub are in a file
called subs.aix or subs.solaris depending on the value of $^O and each subs
file should contain for example as below:

subs.aix:

run_df()
{
   df -g;
}

subs.solaris:

run_df()
{
   df -h;
}

So if this is possible, then at least am not changing the Perl script or do
not need to which makes it easier since I only need to change the subs file?


Is this possible or am I being crazy? I don't know how else to explain what
am wanting to do. Anyway, hopefully someone can understand what I mean and
provide some guidance.

Thanks in advance.

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About