[sf-perl] dynamic use command

Michael Friedman friedman at highwire.stanford.edu
Fri Apr 4 00:17:30 PDT 2008


The other thing you can do is realize that, according to `perldoc -f  
use`:

	It [use] is exactly equivalent to
       	BEGIN { require Module; import Module LIST; }

So you can put the require & import inside a conditional and get the  
same effect. I've chosen to use this construction for dynamic module  
loading:

if ($needs_feature) {
	require Feature::Module;
	Feature::Module->import();
}

This does the same thing as 'use', but avoids the automatic BEGIN block.

However, evaling the string has the same effect and it'll handle the  
situation where 'use' itself has been overridden by custom code. (very  
rare) TIMTOWTDI. :-)

-- Mike

On Apr 3, 2008, at 6:45 PM, Randy J. Ray wrote:
> Stephen Nelson wrote:
>> I think you meant
>>
>> eval("use Net::SMTP::SSL");
>>
>> if ($@) {
>>     print "$net_smtp_ssl is missing\n";
>>     exit;
>> }
>>
>>
>> Otherwise I suspect it'll just do the block eval and evaluate the  
>> string.
>
> Ummm, yeah. What he said. This sort of thing happens when you're  
> juggling too
> many thought-threads at once...
>
> -- 
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> ======================================================================
> Randy J. Ray                                                rjray at blackperl.com
> Sunnyvale, CA                                              http://www.rjray.org
>                                                             http://www.svsm.org
> _______________________________________________
> SanFrancisco-pm mailing list
> SanFrancisco-pm at pm.org
> http://mail.pm.org/mailman/listinfo/sanfrancisco-pm

---------------------------------------------------------------------
Michael Friedman                     HighWire Press
Phone: 650-725-1974                  Stanford University
FAX:   270-721-8034                  <friedman at highwire.stanford.edu>
---------------------------------------------------------------------




More information about the SanFrancisco-pm mailing list