Professional Web Applications Themes

use module only if it exists - PERL Beginners

I want my code to call some subroutines in a custom module only if that module exists on the system. I can think of at least three techniques to determine if the module exists: scan INC, use UNIVERSAL::can (not sure that would work), or put the code in an eval block. What is the best solution? I think an eval block sounds right, but I'm not sure. My guess would look something like: eval { use module; module::subroutine(); }; But the use statement of course throws a compiler error, so I think the appropriate thing is to put this code ...

  1. #1

    Default use module only if it exists

    I want my code to call some subroutines in a custom module only if that module
    exists on the system. I can think of at least three techniques to determine
    if the module exists: scan INC, use UNIVERSAL::can (not sure that would
    work), or put the code in an eval block. What is the best solution? I think
    an eval block sounds right, but I'm not sure. My guess would look something like:

    eval
    {
    use module;
    module::subroutine();
    };

    But the use statement of course throws a compiler error, so I think the
    appropriate thing is to put this code into a string and eval that. But this
    code is going to get ted to a large number of files and I want the
    shortest syntax possible, or if there is some better way...

    TIA,

    -John



    Perl.Org Guest

  2. #2

    Default Re: use module only if it exists



    perl.org wrote:
     

    I know of a module that is almost done with that has a function called

    gotmodule()

    if( gotmodule('Foo::Bar') ) {
    print "Foo::Bar is version $Foo::Bar::VERSION\n";
    } else {
    print "You should install Foo::Bar it is pretty nice\n";
    }

    If you're insterested I can let you know when it is done...
    (I'm tracking it for the Author..)

    Lee.M - JupiterHost.Net
    Jupiterhost.Net Guest

  3. #3

    Default Re: use module only if it exists

    perl.org wrote: 

    use constant HAS_CONFIG_PM => (eval "use Config") || !$;

    print "has Config\n" if HAS_CONFIG_PM;
    Randy Guest

  4. #4

    Default Re: use module only if it exists

    On Tue, 13 Jul 2004 12:59:06 -0500, JupiterHost.Net wrote 

    Thanks, but unfortunately I'm working on a very restricted system (not being
    able to install custom modules on the production platform is actually the
    issue I'm facing, so I couldn't install your module there...).

    Perl.Org Guest

  5. #5

    Default Re: use module only if it exists

    Hi.
    This may be not a high enlightened answer but has solved me some time
    looking for an answer to a question like yours:


    my $IMAGE_SIZE=eval{require Image::Size};
    ....
    if ($IMAGE_SIZE) {
    ### Some code
    ### Runs only if Image::Size is installed
    }else{
    ### More code
    ### Runs if it's not installed
    }


    HTH

    -rm-


    ----- Original Message -----
    From: "perl.org" <com>
    To: <org>
    Sent: Tuesday, July 13, 2004 12:39 PM
    Subject: use module only if it exists

     
    module 
    determine 
    think 
    something like: 
    this 

    Ramon Guest

  6. #6

    Default Re: use module only if it exists

    I forgot to mention

    my $IMAGE_SIZE=eval{require Image::Size};
    ....
    if ($IMAGE_SIZE) {
    my ($x, $y) = Image::Size::imgsize($imagen); ###Call the function
    with the module
    }else{
    ### More code
    ### Runs if it's not installed
    }


    -rm-
    ----- Original Message -----
    From: "Ramon Chavez" <com>
    To: <org>
    Sent: Tuesday, July 13, 2004 1:13 PM
    Subject: Re: use module only if it exists

     
    > module 
    > determine 
    > think 
    > something like: 
    > this [/ref]
    the 
    >
    >
    > --
    > To unsubscribe, e-mail: org
    > For additional commands, e-mail: org
    > <http://learn.perl.org/> <http://learn.perl.org/first-response>
    >
    >[/ref]

    Ramon Guest

Similar Threads

  1. How do I tell if a module exists?
    By Dan Anderson in forum PERL Beginners
    Replies: 3
    Last Post: March 16th, 11:28 PM
  2. Determine if sub exists in module
    By Perl.Org in forum PERL Beginners
    Replies: 5
    Last Post: July 8th, 03:10 PM
  3. Problem installing a perl module "File exists"
    By TheDD in forum PERL Modules
    Replies: 1
    Last Post: June 1st, 05:08 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

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