[sf-perl] dynamic use command

Herb Rubin herbr at pfinders.com
Fri Apr 4 18:21:01 PDT 2008


Joe,

So, will eval have the module loaded outside the eval? 
Or will I have to require it "for real"?

I thought eval was a sandbox of sorts.

Herb

----- Original Message -----
From: "Joe Brenner" <doom at kzsu.stanford.edu>
To: "San Francisco Perl Mongers User Group" <sanfrancisco-pm at pm.org>
Sent: Friday, April 4, 2008 4:34:36 PM (GMT-0800) America/Los_Angeles
Subject: Re: [sf-perl] dynamic use command


This is what I was trying to tell you about "barewords".

Try this:

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


Herb Rubin <herbr at pfinders.com> wrote:

> 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?
>
> Herb
>
> ----- 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'.
_______________________________________________
SanFrancisco-pm mailing list
SanFrancisco-pm at pm.org
http://mail.pm.org/mailman/listinfo/sanfrancisco-pm


-- 
Herb Rubin
Pathfinders Software
http://www.pfinders.com



More information about the SanFrancisco-pm mailing list