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

I'm planning to upload my GUI to GitHub but for portability reasons I want to make it depend solely on core perl + Tk and make using other modules conditional. For example the GUI has functions that copy some content to the Windows clipboard, but this is certainly not portable ( it requires an operation system specific module and even the very concept of a clipboard is not portable between operation systems ). What is the prudent way to test whether the clipboard module is present in the system and make the use of the module conditional on this?
use Win32::Clipboard;

Replies are listed 'Best First'.
Re: Conditional use of modules
by Athanasius (Archbishop) on May 23, 2015 at 13:00 UTC

    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,

      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
Re: Conditional use of modules
by Laurent_R (Canon) on May 23, 2015 at 11:14 UTC