![]() |
|
Syntactic Confectionery Delight | |
PerlMonks |
Modules as configuration filesby Discipulus (Canon) |
on Oct 28, 2015 at 12:59 UTC ( #1146266=CUFP: print w/replies, xml ) | Need Help?? |
Ok is not so cool nor elaborate but i find it useful.. Nowadays i end writing programs with a lot of options and I'm happy using 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 find program. Along many other faults, i tend to write programs with core only modules, and no modules for configuration files are in the core. Even if minimalist a configuration file can contain comments and indentation.. 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 -M as in perl -MModuleName program.pl and as long the module resides in the current directory, no other Perl's switches are needed. Even more: since @ARGV is global by nature, the module can modify @ARGV before Getopts::Long starts his wonderful work inside the main program. The results is a short module like this: 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 --verbosity 3 you can call the program perl -MConfigDefault program.pl --verbosity 0 and have the right behaviour. Please note that split has LIMIT specified, so in the above example --list has the value of arguments and ie only two arguments are created for each line. This is the desired behaviour. The final result seems like: The print "@ARGV" unless caller; is inspired by the 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 xargs. For example if you have a long find configuration in ConfFind.pm you can invoke find this way: i hope you'll find it useful L*
There are no rules, there are no thumbs.. Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Back to
Cool Uses for Perl
|
|