CUFP
Discipulus
Ok is not so cool nor elaborate but i find it useful.. <BR>
Nowadays i end writing programs with a lot of options and I'm happy using [cpan://Getopts::Long] to grab them all. During testing but even
during normal usage of such programs I hate tought to have a long command line, spwaning two or more lines of my terminal screen. As happens with long arguments list given to the <c>find</c> program.<BR><BR>
Along many other faults, i tend to write programs with core only modules, and no modules for configuration files are in the core.<BR><BR>
Even if minimalist a configuration file can contain comments and indentation..<BR><BR>
The basic idea of Perl module come in the rescue. Infact a module can be conviently imported even before any program you wrote by simply
adding <c>-M</c> as in <c>perl -MModuleName program.pl</c> and as long the module resides in the current directory, no other Perl's switches are needed.<BR>
Even more: since <c>@ARGV</c> is global by nature, the module can modify <c>@ARGV</c> before [cpan://Getopts::Long] starts his wonderful work inside the main program.<BR><BR>
The results is a short module like this:
<CODE>
use strict;
use warnings;
unshift @ARGV,$_ for reverse
map {s/\s*#.*$|^\s*#.*$//; (split ' ',$_,2)}
split /\n/, <<'EOCONF'
--put here
-your
--list of arguments and
#any
#indented
--comment #comments
--too
EOCONF
;
print "@ARGV" unless caller;
1;
</CODE>
Unshifting them let an option specified in the module to be overwritten by the same option in the command line. For example if the module
contains <c>--verbosity 3</c> you can call the program <c>perl -MConfigDefault program.pl --verbosity 0</c> and have the right behaviour.<BR>
Please note that <c>split</c> has <c>LIMIT</c> specified, so in the above example <c>--list</c> has the value <c>of arguments and</c> ie only two arguments are created for each line. This is the desired behaviour.<br>The final result seems like:
<c>
perl -MConfigDefault program.pl --verbosity 0
</c>
The <c>print "@ARGV" unless caller;</c> is inspired by the [cpan://Modulino::Demo|Modulino] idea: when the program is invoked as program he build up the list and also print them. This way the module can contains configuration also for non Perl programs and receive them via <c>xargs</c>.<br>
For example if you have a long <c>find</c> configuration in <c>ConfFind.pm</c> you can invoke <c>find</c> this way:
<c>
perl ConfigFind.pm | xargs find
</c>
i hope you'll find it useful<BR><BR>L*<BR><BR><P>
<div class="pmsig"><div class="pmsig-174111">
There are no rules, there are no thumbs..<BR>
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
</div></div><!-- Wiki2Monks {"version":1.16} -->