Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

cross platform scripts

by timbu (Novice)
on Jan 21, 2003 at 20:20 UTC ( #228825=perlquestion: print w/replies, xml ) Need Help??

timbu has asked for the wisdom of the Perl Monks concerning the following question:

I have a script that I would like to use across several UNIX platforms + Win32. I would like to use the same source on both. The problem is code like this.

sub readConfig { if ($^O eq "MSWin32") { use Win32::TieRegistry; # read some stuff from registry } else { open (CONFIG, '/etc/something') or die "$!:Blech\n"; # read some config info in } return "something important"; }

That work's but on Solaris you need to have the Win32 module installed. I have dreamed up the following options.

1) Use C preprocessor and #ifdef out the win32 code as a build step, when packaging the code. Blech.

2) Create a Win32::TieRegistry module with a "1;" on non Win32 platforms.

3) ?

Any ideas?

Replies are listed 'Best First'.
Re: cross platform scripts
by rob_au (Abbot) on Jan 21, 2003 at 21:16 UTC
    I actually posted a node with a solution to a similar problem in an older thread just a few days ago - The thread in question can be found here.

    In this thread I posted a reference to the if module which allows the selective loading of modules based upon conditional tests. In the thread which I have referenced, I posted a snippet of the code would load separate configuration modules based upon the operating system of execution:

    use if ( $^O eq 'MSWin32' ), 'MyAppWin32'; use if ( $^O ne 'MSWin32' ), 'MyAppUnix';

    Perhaps this is of use to you :-)


    perl -le 'print+unpack("N",pack("B32","00000000000000000000001000100001"))'

      This is exactly what I was looking for.


      <This signature intentionally blank>

      I wonder if the argument to use can simply be conditional, without needing a helper module?

      use ($^O eq 'MSWIn32' ? 'MyAppWin32' : 'MyAppUnix');
      I think that should work in principle, since the comparison will be done before the use. It's just a matter of checking the docs to see what the differences are between barewords and normal strings in the use/require magic.


Re: cross platform scripts
by Mr. Muskrat (Canon) on Jan 21, 2003 at 20:29 UTC
    Perhaps read in the config info in a BEGIN block.

    BEGIN { if ($^O eq "MSWin32") { require Win32::TieRegistry; # require not use # read some stuff from registry } else { open (CONFIG, '/etc/something') or die "$!:Blech\n"; # read some config info in } }
      This will call "import Tie::Registry" in adition to the require, as expected. Also, this method is backward compatible with perl 5.005.

      eval "use Win32::TieRegistry";
Re: cross platform scripts
by zakb (Pilgrim) on Jan 21, 2003 at 20:34 UTC

    Is it vital that you use the registry for configuration on Windows? If not, you could probably use INI files for your config, along with one of the following CPAN modules:

    ...or maybe you could use XML with one of the XML modules.

    Update: forgot to mention, all three of these exist on both CPAN and PPM3.

Re: cross platform scripts
by bart (Canon) on Jan 23, 2003 at 03:59 UTC
    You can use "require", provided the module has an OO interface exclusively — or more correct: provided you use only the OO interface, if you have the choice. The former appears to be the case for this particular module, Win32::TieRegistry. Yes, require() should very likely work.

    Update: Oh, dear. I just noticed that this module uses ("abuses"?) the import method for other purposes. Even if you don't provide any arguments to it, you still might have to call import() explicitely:

    require Win32::TieRegistry; Win32::TieRegistry->import;
    The fact that it is called at runtime, not at compile time, doesn't matter because of the module's OO interface.

    Warning: The code has not been tested to see if it has the desired effect. Since I don't want to mess with my registry just to see if it works, it'll just have to be this way. Sorry.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://228825]
Approved by Mr. Muskrat
Front-paged by scain
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2022-10-07 15:41 GMT
Find Nodes?
    Voting Booth?
    My preferred way to holiday/vacation is:

    Results (30 votes). Check out past polls.