Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Modules as configuration files

by Discipulus (Abbot)
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:
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 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:
perl -MConfigDefault program.pl --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 ConfFind.pm you can invoke find this way:
perl ConfigFind.pm | xargs find
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.

Replies are listed 'Best First'.
Re: Modules as configuration files -- benefical side effect
by Discipulus (Abbot) 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.

    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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2022-01-26 18:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (69 votes). Check out past polls.

    Notices?