Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Writing portable code

by Athanasius (Monsignor)
on Mar 08, 2013 at 09:57 UTC ( #1022372=note: print w/ replies, xml ) Need Help??


in reply to Writing portable code

Coming as I do from an OO background, I would approach the problem like this:

#! perl use strict; use warnings; { package FunctionsFactory; my $class = ($^O =~ /Win/) ? 'WinFunctions' : 'LinuxFunctions'; sub new { return $class->new(); } } { package WinFunctions; sub new { return bless {}, shift; } sub f { print "Windows f()\n"; } sub g { print "Windows g()\n"; } } { package LinuxFunctions; sub new { return bless {}, shift; } sub f { print "Linux f()\n"; } sub g { print "Linux g()\n"; } } my $funcs = FunctionsFactory->new(); $funcs->f(); $funcs->g();

Output (on my system!):

19:49 >perl 564_SoPW.pl Windows f() Windows g() 19:53 >

This scheme allows you to keep all the code in one file, and gives a clean/transparent interface to the functions concerned.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


Comment on Re: Writing portable code
Select or Download Code
Re^2: Writing portable code
by tobyink (Abbot) on Mar 08, 2013 at 10:08 UTC

    The disadvantage here is that the WinFunctions package is still parsed and compiled on Linux machines, and the symbol table still exists using up memory, even if it never gets used.

    For a handful of small functions this is unlikely to cause many performance problems.

    What you need to look out for though are cases where, say, WinFunctions not only will not run on Linux, but won't even compile on Linux (e.g. because it uses some Win32::* module). Careful use of run-time require should generally solve this.

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

      Even thinking about having a few unused entries in the symbol table is a ridiculous micro-optimisation.

      If you want to keep the code clean and in one file, then I suggest something like this ...

      use Devel::CheckOS; setup_for_platform(); ... application code goes here ... # decide what platform-specific code to use sub setup_for_platform { if(os_is('MicrosoftWindows')) { *do_stuff = \&do_stuff_for_windows; } elsif(os_is('Unix')) { *do_stuff = \&do_stuff_for_unix; } else { die("Don't know about $^O\n"); } } # platform-specific function implementations sub do_stuff_for_unix { ... } sub do_stuff_for_windows { ... }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1022372]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2014-12-21 14:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (106 votes), past polls