in reply to Re: Using a paramter value to call a module
in thread Using a paramter value to call a module

Interesting solution, but could you please explain the eval statement in the require_if_present sub? It looks as if you are using a here document, but would a try-catch-block eval not be more appropriate here? E.g.

sub require_if_present { my $mod = shift; eval { require $mod; return 1; } return 0 if $@; }

pike

Replies are listed 'Best First'.
Re: Re: Re: Using a paramter value to call a module
by ihb (Deacon) on Feb 07, 2003 at 15:04 UTC

    require() has different meanings depending on the argument. See perlfunc. It even has an example of what you're trying to do, and why it's helps to use eval EXPR.

    <nit>eval requires a semi-colon after it.</nit>

    ihb
Re: Re: Re: Using a paramter value to call a module
by hawtin (Prior) on Feb 08, 2003 at 10:07 UTC

    Interesting solution, but could you please explain the eval statement in the require_if_present sub? It looks as if you are using a here document, but would a try-catch-block eval not be more appropriate here?

    TMTOWTDI: That just happens to be the way I implemented it. A here document is convenient for some of the other things I do this way, but that is not a good reason to use it here. Using an eval as you suggest sounds like a cleaner approach. But against that this works (I have used it many times in the last year) and I try not to fix things that work.

    Update: Thinking about this last night I realised why I used a here document to do an eval, its because this was a very common construct in Perl 4 (now that is a really bad reason for doing it here).