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

Writing Perl scripts to run on any OS'es

Thread Next
newbie01 perl
April 19, 2010 02:53
Writing Perl scripts to run on any OS'es
Message ID:
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:


   df -g;


   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 Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About