Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Modules as configuration files

by 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 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:
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;
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 --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:
perl -MConfigDefault --verbosity 0
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 you can invoke find this way:
perl | xargs find
i hope you'll find it useful


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.

Replies are listed 'Best First'.
Re: Modules as configuration files -- benefical side effect
by Discipulus (Canon) on Oct 30, 2015 at 09:35 UTC
    As side benefical effect, using the above tecnique, complitely avoid shell escaping problems: no quotes or escape are needed because only Perl sees our arguments.

    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.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://1146266]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2023-03-23 21:47 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (60 votes). Check out past polls.