Using a module when not installed.

Discussion in 'Perl Misc' started by Jazeker, Feb 9, 2005.

  1. Jazeker

    Jazeker Guest

    Well... using is the wrong word, and the right one too.

    I have a script that can work with the GD module, but not necessarily
    has to (It's just better to use it). However, when my code says :

    use GD;

    The script won't run. Is there a way to tell Perl to execute the 'use'
    statement only conditionally ? I tried to put it in an unused sub, but
    to no avail (logical it seems as it's being translated by the 'precompiler')

    thx for any inputs,
    Jaz
    --
    print <<EOF;
    Just a noobish Perl hacker
    EOF
     
    Jazeker, Feb 9, 2005
    #1
    1. Advertising

  2. Jazeker

    Anno Siegel Guest

    Jazeker <> wrote in comp.lang.perl.misc:
    > Well... using is the wrong word, and the right one too.
    >
    > I have a script that can work with the GD module, but not necessarily
    > has to (It's just better to use it). However, when my code says :
    >
    > use GD;
    >
    > The script won't run. Is there a way to tell Perl to execute the 'use'
    > statement only conditionally ? I tried to put it in an unused sub, but
    > to no avail (logical it seems as it's being translated by the 'precompiler')


    "At compile time" is the term. The Perl interpreter has two phases,
    the other is run time.

    Here is one way: Replace

    use MayBe qw( fie foe);

    with

    BEGIN { eval { require MayBe } and MayBe->import( qw( fie foe)) }

    This should have the same effect as "use ..." if MayBe.pm exists in
    @INC, and no effect but no complaint either if it doesn't.

    Anno
     
    Anno Siegel, Feb 9, 2005
    #2
    1. Advertising

  3. Jazeker

    Jazeker Guest

    Anno Siegel wrote:

    > "At compile time" is the term. The Perl interpreter has two phases,
    > the other is run time.
    >
    > Here is one way: Replace
    >
    > use MayBe qw( fie foe);
    >
    > with
    >
    > BEGIN { eval { require MayBe } and MayBe->import( qw( fie foe)) }
    >
    > This should have the same effect as "use ..." if MayBe.pm exists in
    > @INC, and no effect but no complaint either if it doesn't.
    >
    > Anno


    I will look up the things you both have referred me directly or
    indirectly to. Thanks a lot !

    --
    print <<EOF;
    Just a noobish Perl hacker
    EOF
     
    Jazeker, Feb 9, 2005
    #3
  4. Jazeker

    Jazeker Guest

    Anno Siegel wrote:
    > "At compile time" is the term. The Perl interpreter has two phases,
    > the other is run time.
    >
    > Here is one way: Replace
    >
    > use MayBe qw( fie foe);
    >
    > with
    >
    > BEGIN { eval { require MayBe } and MayBe->import( qw( fie foe)) }
    >
    > This should have the same effect as "use ..." if MayBe.pm exists in
    > @INC, and no effect but no complaint either if it doesn't.
    >
    > Anno


    Thx Anno. Did some experimenting with this and the underneath 'code'
    might be a bit more readable for visiting noobs like me :

    #!c:\perl\bin\perl.exe
    #
    BEGIN { $hasit = eval { require GD };
    print "hasit 1 : $hasit\n";
    }
    #script main logic starts
    print "hasit 2 : $hasit\n";
    if ($hasit) { &do_it_the_easy_way; }
    else { &do_it_the_hard_way; }

    --
    print <<EOF;
    Just a noobish Perl hacker
    EOF
     
    Jazeker, Feb 21, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Learner
    Replies:
    4
    Views:
    2,112
    Learner
    Jan 27, 2006
  2. Askari
    Replies:
    1
    Views:
    4,324
    Fredrik Lundh
    Sep 6, 2004
  3. Replies:
    3
    Views:
    479
    Jason
    Apr 28, 2008
  4. Nico Grubert
    Replies:
    0
    Views:
    323
    Nico Grubert
    May 5, 2011
  5. Michel Claveau - MVP

    Re: PIL: The _imaging C module is not installed

    Michel Claveau - MVP, May 6, 2011, in forum: Python
    Replies:
    2
    Views:
    401
    Albert Hopkins
    May 6, 2011
Loading...

Share This Page