Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: 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.

In reply to Modules as configuration files by Discipulus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-25 23:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found