On Mon, Oct 11, 2010 at 5:28 PM, Jyoti <jcutiep...@gmail.com> wrote:

> Hello,
>
> Can anyone help me out to call all subroutines from different .pm files in
> a
> specific directory please ?
>
> Many Thanks,
> Jyoti
>


Hi Jyoti,

I think you should be a little more specific I assume you are asking how to
include a module file in your script, but there might be a few other things
you are asking for... so lets do the whole basic thing of module, script
include and call in one go. :-)

So you have a module called: *Module.pm* which has a single sub called: *
do_stuff*, you have dropped it in a directory called: *My_modules*. Your
script is called: *Script.pl* and sits in it's own directory. So this is
what it all looks like when you list the directory that you have placed your
script in.

.
..
My_modules.Module.pm
Script.pl

So now in your script you want to call this do_stuff sub, so first of all
you tell your script to use your module by using the following bit of code:

#!/usr/local/bin/perl

use strict;
use warnings;
*use lib "./My_modules";*
*use Module;*

These two italic lines basically do two things, first of all it adds one
additional entry to the library path your script was started with
"./My_Modules" which then allows you to call your module as if it where
normally on the path. The second line then simply includes your module just
like you do with strict and warnings. From there on it becomes very simple,
you just call the sub like you would call a sub in your script..

if ( $equation ) {
 do_stuff ( "Some string handed in to do stuff with" );
}

That should pretty much do the trick... of course you will run into some
problems due to the relative path that is used for the use lib statement, so
you might want to think about a fixed path or maybe have a look at something
like the FindBin module to determine where your script is in relation to the
rest of the file system. But that is all to advanced to bother with for the
basic I have a module in a directory and I want to call the function in
it...

Oh, one more thing if you want to have a hierarchy in your modules folder
like for instance:

.
..
My_modules/
My_modules/Transport/
My_modules/Transport/Car.pm
My_modules/Transport/Bike.pm
My_modules/Transport/Plane.pm

Etc, then you will want to call your module like this:
use Transport::Car;

Which of course will require your module to also have that name on the first
line: package Transport::Car; is what Car.pm would start with, but by the
time you start doing stuff like that you might indeed want to have a look at
the link Thomas suggested as that is beginning to look a lot like you are
trying to work with classes and object oriented code which is a whole other
can of worms, certainly in good old perl5 OO can be a little more difficult
than one would like OO to be.

Regards,

Rob Coops

Reply via email to