For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2006 > import modules at begin block









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author import modules at begin block
Ken Perl

2006-01-22, 9:56 pm

Hi,
May I import all modules located in a directory called
'/opt/myperlmodules' in Begin block like this?

$myModules =3D "/opt/myperlmodules";
lib->import($myModules);

--
perl -e 'print unpack(u,"62V5N\"FME;G\!E<FQ`9VUA:6PN8V]M\"\@``
")'
Xavier Noria

2006-01-23, 3:55 am

On Jan 23, 2006, at 2:20, Ken Perl wrote:

> Hi,
> May I import all modules located in a directory called
> '/opt/myperlmodules' in Begin block like this?
>
> $myModules = "/opt/myperlmodules";
> lib->import($myModules);


You want to load them, or to let use() be able to find them? The
solution to the former looks like a combination of File::Find and
require() or some similar technique, whereas the solution to the
latter looks like common use of the lib pragma.

-- fxn

Tom Phoenix

2006-01-23, 7:56 am

On 1/22/06, Ken Perl <kenperl@gmail.com> wrote:

> May I import all modules located in a directory called
> '/opt/myperlmodules' in Begin block like this?


What happened when you tried it?

> $myModules =3D "/opt/myperlmodules";
> lib->import($myModules);


I don't think it will work, but maybe you have a different version of
Perl installed on your machine than I have in my brain. :-)

Maybe you're doing something quite unusual, but... It's probably not a
defensible programming technique to use whichever modules are found in
a given directory, for much the same reason that recipes seldom
suggest choosing ingredients by location. "Begin with the rightmost
box on the shelf with the breakfast cereal. Next, stir in half of the
largest container on the bottom shelf in the refrigerator. Add a
spoonful of every third spice from the second row of the spice
rack...."

Modules: Ask for them by name.

Hope this helps!

--Tom Phoenix
Stonehenge Perl Training
Peter Scott

2006-01-23, 6:56 pm

On Sun, 22 Jan 2006 17:39:47 -0800, Tom Phoenix wrote:
> On 1/22/06, Ken Perl <kenperl@gmail.com> wrote:
>
>
> What happened when you tried it?
>
>
> I don't think it will work, but maybe you have a different version of Perl
> installed on your machine than I have in my brain. :-)


What perl do you have installed in your brain? Do you have a full CPAN? :-)

> Maybe you're doing something quite unusual, but... It's probably not a
> defensible programming technique to use whichever modules are found in a
> given directory, for much the same reason that recipes seldom suggest
> choosing ingredients by location. "Begin with the rightmost box on the
> shelf with the breakfast cereal. Next, stir in half of the largest
> container on the bottom shelf in the refrigerator. Add a spoonful of every
> third spice from the second row of the spice rack...."
>
> Modules: Ask for them by name.


Maybe I'm missing something, but the poster appears to be asking how to
achieve the common and useful pattern of plugins, where you control
dynamic behavior by deciding what modules are in a particular directory.
Of course there's a lot more scaffolding than the poster alluded to, but
at some point you do have to load all the modules in a directory. My
current favorite way is to eval each file one at a time and extract
package directives to push class names onto an array.

Ken: that won't work. You need BEGIN blocks, glob(), and a require
followed by a $class->import inside the loop.

--
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/

Shawn Corey

2006-01-23, 6:56 pm

Peter Scott wrote:
> Maybe I'm missing something, but the poster appears to be asking how to
> achieve the common and useful pattern of plugins, where you control
> dynamic behavior by deciding what modules are in a particular directory.
> Of course there's a lot more scaffolding than the poster alluded to, but
> at some point you do have to load all the modules in a directory. My
> current favorite way is to eval each file one at a time and extract
> package directives to push class names onto an array.
>
> Ken: that won't work. You need BEGIN blocks, glob(), and a require
> followed by a $class->import inside the loop.
>


Every now and again someone gets the bright idea to load modules
dynamically. Usually they do this to save time. The correct way to do
this is to rewrite the modules for AutoLoader. Then you just 'use'
everything. See `perldoc AutoLoader` for details.


--

Just my 0.00000002 million dollars worth,
--- Shawn

"Probability is now one. Any problems that are left are your own."
SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is available at http://perldoc.perl.org/
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com