If memory serves, I picked that up from something merlyn once said or posted.my $have_SWE; BEGIN { $have_SWE = 0; eval { require Spreadsheet::WriteExcel }; unless ($@) { Spreadsheet::WriteExcel->import(); $have_SWE = 1; } } if ($have_SWE == 1) { ... }
Update:
Super Search for "module installed" is what lead me to this thread, where I learnt of BEGIN, eval, and require.
Update:
After discussing with brother BUU, the lone advantage I can see of BEGIN... eval is that it allows you to include a more user-friendly message in the event that Tk isn't installed. As always, Wiser Monks Than I may very well rightly say that I'm fullabeans. {grin}
Untested, but perhaps a wee bit cleaner than my original code example:
my $have_Tk; BEGIN { $have_Tk = 0; eval { require Tk }; $have_Tk = 1 unless ($@); } if ($have_Tk == 1) { ... }
In reply to Re: using 'use' conditionally (require instead of use)
by ybiC
in thread using 'use' conditionally
by xafwodahs
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |