Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

To the OP: Although below I'll show a way to put multiple commands on one command line, I actually don't necessarily recommend this, nor would I recommend building a monolithic "one script to do it all", instead follow stevieb's and Athanasius's advice to modularize. One element of the Unix philosophy is to make each tool "Do One Thing and Do It Well". If your scripts need to share common functions, then do that through a module, rather than shoving all the code into one script.

You can not get command line options twice within the same program.

That statement is just a little too absolute for my tastes, it's totally possible to get too clever with @ARGV :-)

use warnings; use strict; use Data::Dump; use Getopt::Std; use Getopt::Long; # fake command line local @ARGV = qw/ one -abc -- two -d -e -- three -abcdef -- four --foo --bar --bar --bar -- five --foo --quz baz /; while (@ARGV) { my $cmd = shift @ARGV; getopts('abcdef', \my %o); dd $cmd, \%o; last if $o{f}; } while (@ARGV) { my $cmd = shift @ARGV; GetOptions(\my %o, 'foo!', 'bar+', 'quz!'); dd $cmd, \%o; last if $o{quz}; } dd @ARGV; __END__ ("one", { a => 1, b => 1, c => 1 }) ("two", { d => 1, e => 1 }) ("three", { a => 1, b => 1, c => 1, d => 1, e => 1, f => 1 }) ("four", { bar => 3, foo => 1 }) ("five", { foo => 1, quz => 1 }) "baz"

Also, it's possible to read options from strings, such as the lines of STDIN. Getopt::Long has GetOptionsFromString, and with Getopt::Std it's possible with a little trick.


In reply to Re^2: Wrap multiple programs by haukex
in thread Wrap multiple programs by jnarayan81

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 learning in the Monastery: (8)
As of 2024-04-24 10:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found