0

Ok, so I'm trying to learn to properly use log4perl.

It seems like it would make sense that I could write a module that uses log4perl, but can also be distributed to a system where log4perl is not installed and still run without failing, just not log. If I have use Log::Log4Perl qw(:easy); in the module, compile fails with:

Can't locate Log/Log4Perl.pm in @INC ...

Basically, what is standard and best practice among users of log4perl to enable log4perl compatibility in a module without forcing it? It seems like something that should be common, but I can't find it to save my life.

Thanks in advance.

EDIT: Thanks for the feedback! I have edited the question to reflect that I am looking for the best practice among users of log4perl in this situation, given that certainly this must be a common issue among log4perl users... right?

ipetrik
  • 1,749
  • 18
  • 28
  • 2
    See [`How can I check if I have a perl module before using it`](http://stackoverflow.com/questions/251694/how-can-i-check-if-i-have-a-perl-module-before-using-it). Seems to be an exact duplicate. The point is, you need to do it at runtime, and then need to use `require`. – zdim Aug 03 '16 at 23:41
  • Thanks! That is certainly *a* solution, but I'm wondering what *the* best practice solution among log4perl users is. I have edited my question to reflect this. – ipetrik Aug 04 '16 at 19:00
  • 1
    Alright. While I don't know what is usual among `log4perl` users, the main point here _is_ that you can only do such checks at runtime, while `use` is a `BEGIN` block. Thus you must resort to loading the module using `require` (and then `import`), at runtime, and there aren't too many ways to do that. I don't see how a module or its users can go around that, unless you use yet other modules. This is a generic problem and I think that you want to go with what would be Perl's most sensible and idiomatic way, not some particular trick. The linked post offers good discussion and examples. – zdim Aug 05 '16 at 09:42
  • You are in-fact correct. :-) I did finally find this in the faq: http://log4perl.sourceforge.net/releases/Log-Log4perl/docs/html/Log/Log4perl/FAQ.html#e193b – ipetrik Aug 10 '16 at 21:14
  • Great :) In my opinion it is important to seek best practices! Also -- you were correct in looking for something `log4perl` related, from what I see in your link the `import`s are specific! – zdim Aug 10 '16 at 21:20

0 Answers0