in reply to Conditional use of modules

Hello tkguifan,

To determine the platform on which the script is running, you can use either the built-in variable $^O (also spelled $OSNAME; see perlvar#General-Variables) or $Config{osname} from Config. See the recent thread Cross-platform accented character file names sorting, which also discusses the correct syntax for using the if pragma.

To test whether or not a module is present in the system, use or require it in an eval and then immediately test the value of $@:

22:42 >perl -wE "eval qq[use Unknown;]; say $@ ? 'failed' : 'succeeded +';" failed 22:43 >perl -wE "eval qq[use Data::Dump;]; say $@ ? 'failed' : 'succee +ded';" succeeded 22:56 >

Hope that helps,

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

Replies are listed 'Best First'.
Re^2: Conditional use of modules
by Anonymous Monk on May 23, 2015 at 14:46 UTC
    use or require it in an eval and then immediately test the value of $@

    See for example the section "Background" in Try::Tiny for a discussion of the problems with $@. It's safer to do something like this:

    print eval 'use Data::Dumper; 1' ? 'success' : 'failure', "\n"; print eval 'use Unknown; 1' ? 'success' : 'failure', "\n"; __END__ success failure