Hi newbie01,

On Monday 19 Apr 2010 12:52:53 newbie01 perl wrote:
> 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 ... :-)

See http://perl-begin.org/ for links to resources explaining that and many 
other things. Please use a module, or even a class hierarchy for inheritance, 
instead of implementing it in non-idiomatic and sub-optimal Perl.

> 
> 
> 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";
> }

1. In that case, you can write a portable directory listing in pure-Perl.

2. Don't use $ARGV[0] directly. What happens if you move it to $ARGV[1]? 
You'll need to change all the occurrences of it. Instead do:

[code]
my ($filename) = @ARGV;
[/code]

> 
> 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?

Well, the above is shell notation (ksh/bash/etc.), but you can do something 
similar in Perl using a module or using classes and objects.

> 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.

Please read one of the resources on http://perl-begin.org/ to get some 
guidance with writing modules. For example:

* http://perl-begin.org/tutorials/perl-for-newbies/part3/

* http://www.perl.org/books/beginning-perl/

If you still don't understand something, then post a reply to the mailing list 
and we'll explain.

Regards,

        Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
The Case for File Swapping - http://shlom.in/file-swap

Deletionists delete Wikipedia articles that they consider lame.
Chuck Norris deletes deletionists whom he considers lame.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to