Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Re: Re: Re: Re: Load all modules in directory

by graff (Chancellor)
on Dec 11, 2003 at 04:52 UTC ( [id://313972]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Re: Re: Load all modules in directory
in thread Load all modules in directory

I'm sorry, but something about this train of thought makes little or no sense.

... the solution I'm looking for will allow me to put modules in the directory (i.e. a new module called Module4.pm) and then have the script automatically recognize the existence of this new mod.

What I don't understand is: if you create or install a new module, and you want to use it in an existing perl script, don't you have to edit that existing script anyway, to add whatever logic will be invoking the objects/methods/etc being provided by the new module? And if you have to edit an existing script anyway, why not go ahead and add the specific "use Newmodule;" statement at the top? Why is it a problem to do that?

If I had to maintain a setup like yours, where all the modules in a given location are being loaded into scripts implicitly, I think I might end up losing sanity... Some modules do not export their function calls by default, and this is done, in part, so that the programmer who uses the module has to declare specifically when he is using a particular function from the given module. This is good for maintenance, because it's easier to keep track of where stuff comes from.

Update: Another reason why modules don't export by default is, of course, to guard against the inevitable collisions in the symbol table when two separate modules happen to export the same name. I suppose that if you're in a situation where your scripts are being created automatically by some other script(s), it might make sense to have this sort of "automatic scan and load" operation over a whole module directory -- but if you're doing that, it still makes more sense for the auto-generated script to have all those specific "use ModuleN" statements spelled out programmatically at the top. Why not?

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Re: Load all modules in directory
by eric256 (Parson) on Dec 11, 2003 at 19:37 UTC

    Actualy it is not hard at all to picture a case where this behavior is usefull. Take for instance a program that has plugins. It scans that directory and loads all the modules form it, recording there name so it can later list all of the plugins. This can even then be worked so that the module can occasionaly check for new plugins and load them while still running. In this way you could have any type of program that runs 24/7 and you can still update/fix/add plugins on the fly. :-) I've used code like ...

    my $temp = $class; $temp =~ s/::/\\/igs; require "$temp.pm"; $self->{bots}->{$name} = $class->new(@args); # create the base cla +ss

    That is dynamicaly loading a module based on a name passed to it.


    ___________
    Eric Hodges

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://313972]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-03-19 08:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found