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

Hello monks and... monksesses? :-)

This snippet loads a module at runtime and works correctly:

unless (exists $INC{'Spreadsheet/WriteExcel.pm'}) { eval "use Spreadsheet::WriteExcel" ; croak $@ if $@ ; }

Anyway, is there anything wrong in what I am doing here?

Ciao!
--bronto


The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
--John M. Dlugosz

Replies are listed 'Best First'.
Re: %INC and eval use
by broquaint (Abbot) on Mar 10, 2004 at 16:00 UTC
    Nothing wrong there really. Although you could always just avoid eval STRING and use a require instead e.g
    require Spreadsheet::WriteExcel && Spreadsheet::WriteExcel->import unless exists $INC{'Spreadsheet/WriteExcel.pm'};
    HTH

    _________
    broquaint

Re: %INC and eval use
by Juerd (Abbot) on Mar 10, 2004 at 16:07 UTC

    use already skips loading the module if it is in %INC (importing does happen, but IIRC, this specific module doesn't export anything), so why test %INC manually?

    See require if it must happen at runtime.

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

      To avoid evaling more than once; AFAIK evals are expensive, aren't they?

      Ciao!
      --bronto


      The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
      --John M. Dlugosz

        To avoid evaling more than once; AFAIK evals are expensive, aren't they?

        Yes, but why do you use eval in the first place? require is the runtime module loader.

        Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

Re: %INC and eval use
by dragonchild (Archbishop) on Mar 10, 2004 at 18:03 UTC
    There's no reason to test %INC. Just require the module as needed. require checks %INC, AFAIK.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: %INC and eval use
by fraktalisman (Hermit) on Mar 10, 2004 at 22:50 UTC
    You could
    unless (exists $INC{'Spreadsheet/WriteExcel.pm'}) { require Spreadsheet::WriteExcel; import Spreadsheet::WriteExcel; }
    When I first had the problem to use a module only if it exists, I tried to just put use after the if clause. I was quite surprised that use is always used by the compiler. But for what reason anyway?
      Because perl likes to know about functions at compile time, so the import really should be done then if possible. In this case, looks to me as if this is a pure OO module that doesn't export anything, so the import can be skipped.
Re: %INC and eval use
by simonm (Vicar) on Mar 10, 2004 at 21:01 UTC
    Hello monks and... monksesses? :-)

    I know this was a joke, but it's interesting to note that female monastics are still struggling for acceptance as real monks, both in the east and in the west.