Professional Web Applications Themes

How do I tell if a module exists? - PERL Beginners

How can I tell whether or not a module exists, and what version it is? i.e. do something like if (defined CGI.pm). I want to make sure all my users are running off of the same modules. Thanks in advance, Dan...

  1. #1

    Default How do I tell if a module exists?

    How can I tell whether or not a module exists, and what version it is?
    i.e. do something like if (defined CGI.pm). I want to make sure all my
    users are running off of the same modules.

    Thanks in advance,

    Dan
    Dan Anderson Guest

  2. #2

    Default Re: How do I tell if a module exists?

    On Wed, Nov 05, 2003 at 07:39:37PM -0500, Dan Anderson wrote:
    > How can I tell whether or not a module exists, and what
    > version it is?
    There's no iron-clad rule, but the convention is for modules
    to put it in the package variable $VERSION.

    sub UNIVERSAL::require {
    my ($module, $version) = _;
    eval "require $module" or die;
    $module->VERSION($version);
    }

    CGI->require(3.00);

    If the module uses Exporter -- and CGI doesn't -- then you can
    also check from the command line.

    % perl -MNet::SMTP=99 -e1
    Net::SMTP version 99 required--this is only version 2.24

    --
    Steve
    Steve Grazzini Guest

  3. #3

    Default Re: How do I tell if a module exists?

    Dan Anderson wrote:
    > How can I tell whether or not a module exists, and what version it is?
    > i.e. do something like if (defined CGI.pm). I want to make sure all my
    > users are running off of the same modules.
    during runtime or when you preparing to install your module? something
    along the line should do it:

    #!/usr/bin/perl -w
    use strict;

    #--
    #-- you might not want that and decided to check
    #-- version yourself after a successful load.
    #--
    eval "use $ARGV[0] $ARGV[1]";

    if($){
    print STDERR $;
    }else{
    print "$ARGV[0] v$ARGV[1]\n";
    }

    __END__

    success:

    [panda]# has_module.pl Date::Manip 5.4
    Date::Manip v5.4

    wrong version:

    [panda]# has_module.pl Date::Manip 6.4
    Date::Manip version 6.4 required--this is only version 5.40 at (eval 2) line
    2. BEGIN failed--compilation aborted at (eval 2) line 2.

    missing module:

    [panda]# has_module.pl perl 0
    Can't locate perl.pm in INC ... etc

    david
    --
    $_=q,015001450154015401570040016701570162015401440 041,,*,=*|=*_,split+local$";
    map{~$_&1&&{$,<<=1,$#.=qq~\x63\x68\x72\x28_[$_..$||3])=>~}}0..s~.~~g-1;*_=*#,

    goto=>print+eval
    David Guest

  4. #4

    Default Re: How do I tell if a module exists?

    a simple, assumption free solution:


    if (Some::Module->isa('UNIVERSAL')) {
    # module exists
    }
    else {
    # module does not exists
    }

    If you do not know the name of the module, isa also works on scalar variables:

    my $module_name = 'Some::Module';

    if ($module_name->isa('UNIVERSAL')) {
    # ...
    }
    else {
    # ...
    }
    Valentin Guest

Similar Threads

  1. RFC: New module 'Module::Bundled::Files'
    By Paul Campbell in forum PERL Modules
    Replies: 3
    Last Post: August 27th, 10:51 PM
  2. Problem installing a perl module "File exists"
    By TheDD in forum PERL Modules
    Replies: 1
    Last Post: June 1st, 05:08 PM
  3. Module Object and sub module function
    By Dan Muey in forum PERL Beginners
    Replies: 15
    Last Post: November 11th, 04:45 PM
  4. method name exists, property value exists, calling method fails
    By Phil Powell in forum PHP Development
    Replies: 0
    Last Post: October 27th, 07:34 PM
  5. Replies: 2
    Last Post: July 11th, 04:20 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139