[sf-perl] dynamic use command

Herb Rubin herbr at pfinders.com
Fri Apr 4 16:19:22 PDT 2008


Now I have too much information on this issue. I am still stuck with this
code not working:

$module = "Net::SMTP";
require $module;

However this works:

use Net::SMTP;

Why is require not letting me use the variable?


----- Original Message -----
From: "Michael Friedman" <friedman at highwire.stanford.edu>
To: "San Francisco Perl Mongers User Group" <sanfrancisco-pm at pm.org>
Sent: Friday, April 4, 2008 4:04:51 PM (GMT-0800) America/Los_Angeles
Subject: Re: [sf-perl] dynamic use command

I'd been waiting to try it out before replying, but it seems to work  
for 'use strict' at least.

However, since the special directives (non-modules) that go with 'use'  
don't actually get a default BEGIN block, there's no need to use  
require and import. You can

... bad code ...

use strict;

... good code ...

no strict;

... bad code ...

as much as you want.

-- Mike

On Apr 4, 2008, at 3:57 PM, Quinn Weaver wrote:
> On Fri, Apr 04, 2008 at 03:04:15AM -0800, Rich Morin wrote:
>> At 00:17 -0700 4/4/08, Michael Friedman wrote:
>>> 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; }
>> Is this true in the case of "use strict"?
> I think so.  Take a look at the strict.pm source code:
> vi `perldoc -l strict`
> There's a 'sub import' in there which seems to set strict refs and/ 
> or subs
> and/or vars according to what you write in 'use strict LIST'.
> -- 
> Quinn Weaver, independent contractor  |  President, San Francisco  
> Perl Mongers
> http://fairpath.com/quinn/resume/     |  http://sf.pm.org/
> 510-520-5217
> _______________________________________________
> 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>

SanFrancisco-pm mailing list
SanFrancisco-pm at pm.org

Herb Rubin
Pathfinders Software

More information about the SanFrancisco-pm mailing list