Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Include a package based on OS.

by techman2006 (Beadle)
on Jan 20, 2014 at 05:07 UTC ( [id://1071266]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

I have a script which run on Windows as well as on Linux. But it contains a particular functionality which is present only for Linux (which requires specific packages which are installed on Linux only).

So is there a way through which I can make inclusion of packages based on OS.

Below is pseudo code which I am trying to put.

use strict; use warnings; . . . if ($^O eq "Linux") { use DBI; } . . .

Any pointer on the same will be great.

Replies are listed 'Best First'.
Re: Include a package based on OS.
by tobyink (Canon) on Jan 20, 2014 at 05:37 UTC

    use executes at compile time, before your if statement even gets evaluated. The result of that is that use DBI happens on all platforms, not just Linux.

    Instead you need to load the module with require, a la:

    if ($^O eq 'Linux') { require DBI; }

    There's a module called if, which is part of the core Perl distribution. It wraps up the above pattern allowing you to use the somewhat neater:

    use if ($^O eq 'Linux'), 'DBI';

    It's worth noting that this is not some kind of extension to Perl's syntax. You're simply useing the if module and passing its import method two parameters: the first is a boolean (the result of evaluating $^O eq 'Linux'); the second is a module name as a string. The if module then contains the logic to load the named module only if the boolean is true.

    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
Re: Include a package based on OS (ssearch)
by Anonymous Monk on Jan 20, 2014 at 09:27 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1071266]
Approved by tobyink
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-19 13:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found