-5

Possible Duplicate:
how to import multiple custom modules in our own perl script?

I have some packages. i want to import them all at once. Right now, i am doing like

use lib 'path to packages';

use package1;

use package2;

......

use packageN;

Which is working fine. but i want to import all packages in just a single command. Can anybody suggest me an easiest way to do it ?

Community
  • 1
  • 1
  • i do not want to use 'use' statement for each package. Tell me some good ways to do???? – Navrattan Bansal Apr 04 '12 at 09:11
  • 1
    What is the motivation behind this design objective? – DavidO Apr 04 '12 at 09:16
  • Actually i am testing guy. i am creating some subroutines which i will use in my almost every testing script. So I have created package for each subroutine but now importing is the problem? – Navrattan Bansal Apr 04 '12 at 09:27
  • 3
    What's the difference with [your other question](http://stackoverflow.com/questions/9993478/how-to-import-multiple-custom-modules-in-our-own-perl-script)? – Stamm Apr 04 '12 at 09:35
  • @user970553 — You should have a package for all your subroutines, not a package for each subroutine. – Quentin Apr 04 '12 at 09:48
  • Do you have answer. Then Please give it. – Navrattan Bansal Apr 04 '12 at 09:51
  • 1
    Do you not see the answer I posted below? If it fails to answer the question you asked, please post a comment explaining what I missed. – DavidO Apr 04 '12 at 09:56

2 Answers2

3

Audrey Tang has a relatively new module on the CPAN called 'use' which will do exactly what you're asking. From its synopsis:

use use qw[ strict warnings methods invoker ];

It has other features, including the ability to specify options such as import lists, so read the docs. But minimally, just place your own module names in place of the ones enumerated in my example code.

Update: So to spell it out as clearly as I can: First, install the 'use' module that I linked to at the top of this answer. Second, modify the code you posted in your question so that it looks like this:

use lib 'path to packages';
use use qw[ package1 package2 package3 package4 ..... ];
DavidO
  • 13,812
  • 3
  • 38
  • 66
  • i am getting confuse that what to write in my module and what to write in ny script which is using that module??? – Navrattan Bansal Apr 04 '12 at 09:58
  • Can you elaborate by giving any example please??? – Navrattan Bansal Apr 04 '12 at 10:03
  • I need a solution without installation of any module sir.
    So Even Is there any way to import all packages from given directory.
    – Navrattan Bansal Apr 04 '12 at 10:07
  • Why? ...nevermind. Just copy and paste the code out of the 'use' module. It's only 33 lines of code. Follow the link I provided, then click on the 'Source' link near the top of that page, copy the code. Or implement your own solution by wrapping `eval "use $_;" for @modlist;` inside a BEGIN block and hope for the best. – DavidO Apr 04 '12 at 10:11
  • I copied source code and paste into file name use.pm . Now What to sir ??? – Navrattan Bansal Apr 04 '12 at 10:16
2

Well, you could make a module that does it for you. For example:

use v5.10;

use strict; 
use warnings;

use mro     (); 
use feature ();

sub import {
    warnings->import();
    strict->import(); 
} 

1;

Now, the above module doesn't work, but you get the gist. If you want a working example of this, look at the source code for Modern::Perl.

If you do something like this though, you only have to write ONE LINE!

Hope this helps!

Dynamic
  • 921
  • 1
  • 12
  • 31