Well why not All

So no MooseX or AD&D post tonight as I go sidetracked on anotehr project but at least I did manage to get one post out of it and finally found out how to do something in Perl I have been wanting to do for years.

You see I came back to Perl after a number of years playing with Java and JSPs, I was happy with JSP and Java there where some annoying things (hard to manipulate text) and some very good things, strong OO. In my move back to Perl some 10 years after last playing with in (early 90s) I regret leaving Java that much but there was always always one thing I missed from that world, the 'import' command

For the work I was doing at the time is was great, as I could simply use a single import and get all the classes I needed using the wild card like this

 
import DA.*

and all my classes under DA would be nicely loaded.

I always found this lacking in Perl until I ran into this All. Though it has been around for quite a while I had never seen it used or even suggested to me so I was wondering where it is been hiding as I really could of used it a few times over the years. The name space might of hidden it from me as I would never of searched for it under just 'All'. Anyway no use crying over past problems lets just go with it.

Now what I like is that 'All' is even nicer than JAVA's 'import' as not only can I do a wildcard like this

 
use all 'DBIX::DA::*';

and get all the classes under a namespace, though like JAVA's 'import' if I want to get 'DBIX::DA' as well I have to add that is so now my code looks like this

 
use 'DBIX::DA';
use all 'DBIX::DA::*';
Rather than;
 
use 'DBIX::DA';
use 'DBIX::DA::SQL';
use 'DBIX::DA::Select';
use 'DBIX::DA::Update';
...

Now the neat thing is I can also enter more that one namespace per line if I want to like this

 
use all 'DBIX::DA::*', 'Sys::*';

Now there are some limits to this as so far it has formed me to no longer 'export' subs from modules but I rarely do this so no big loss for me.

There is nothing really majik going on here the 'all' simply iterates over the present path to find you mods, so it may slow things down (not very much it seems) and add in a few extra dependencies but for me not having to type in 65 use statements to get all my packages in place it is a blessing at least for what I had to work on today.

Oh well back to MooseX tomorrow I guess

article-0-096D53A3000005DC-358_468x333.jpg

3 Comments

'all' does perform import() on each module it use's. So the default exports are still imported into your namespace. It's the optional or custom import which must be imported manually. Which makes sense in general.

However, one can suggest or patch 'all' so it accepts a syntax like:

use all 'Foo::*', -import => [qw/a b c/], 'Bar::*';

which performs import() on each found Foo.

Also, instead of:

use 'DBIX::DA';
use all 'DBIX::DA::*';

you can also say (shorter):

use all 'DBIX::DA','DBIX::DA::*';

which is, it turns out, superfluous, as:

use all Foo;

already tries to load Foo and everything under Foo. So like the Java's import Foo.* statement.

Leave a comment

About byterock

user-pic Long time Perl guy, a few CPAN mods allot of work on DBD::Oracle and a few YAPC presentations