Perhaps a trick stolen from File::Spec could help you? Have a class defining an OS-independant interface, make that class inherit from OS-specific classes. Make the OS-specific classes inherit from an OS-independant base class, if needed.

Something like this:

Update: Use one file per package (<code> block), don't stuff everything into a single file.

package Foo::Bar; use strict; use warnings; use parent "Foo::Bar::$^O"; # will die here for any OS not yet support +ed 1; =pod Documentation here =cut
package Foo::Bar::MSWin32; use strict; use warnings; use parent 'Foo::Bar::base'; use Win32::OLE; sub frob { Win32::OLE->... } # ... 1;
package Foo::Bar::unixoid; use strict; use warnings; use parent 'Foo::Bar::base'; sub frob { # do it without Win32::OLE } # ... 1;
package Foo::Bar::linux; use parent 'Foo::Bar::unixoid'; 1;
package Foo::Bar::solaris; use parent 'Foo::Bar::unixoid'; 1;
package Foo::Bar::cygwin; use parent 'Foo::Bar::MSWin32'; # assuming you can use OLE with cygwin + - never tried it 1;
package Foo::Bar::base; use strict; use warnings; sub new { my $proto=shift; my $class=ref($proto)||$proto; my $self=bless {},$class; $self->init(@_) or die "init() failed"; return $self; } sub init { # ... } sub some_generic_function { # ... } 1;

And elsewhere in your code:

use Foo::Bar; my $f=Foo::Bar->new(); $f->frob('bla','bla'); $f->some_generic_function();

If you don't like or don't need OOP, add something similar to File::Spec::Functions.

Note that you don't need to inherit from Exporter with any sufficiently modern Perl, even if File::Spec::Functions does. File::Spec is old code, with its own set of problems. It's good, but not perfect. Using File::Spec::Unix as default is ok, but making all other classes inherit from File::Spec::Unix looks strange. And File::Spec also has a design problem.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

In reply to Re^3: Modules and crossplatform code by afoken
in thread Modules and crossplatform code by Khariton

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.