NAME

Argv - Provide an O-O interface to an ARGV


SYNOPSIS

    use Argv;
    # A roundabout way of getting perl's version.
    my $pl = Argv->new(qw(perl -v));
    $pl->exec;
    # Run /bin/cat, showing how to provide "predigested" options.
    Argv->new('/bin/cat', [qw(-u -n)], @ARGV)->system;
    # A roundabout way of globbing.
    my $echo = Argv->new(qw(echo M*));
    $echo->glob;
    my $globbed = $echo->qx;
    print "'echo M*' globs to: $globbed";
    # A demonstration of the builtin xargs-like behavior.
    my @files = split(/\s+/, $globbed);
    my $ls = Argv->new(qw(ls -d -l), @files);
    $ls->parse(qw(d l));
    $ls->dbglevel(1);
    $ls->qxargs(1);
    my @long = $ls->qx;
    $ls->dbglevel(0);
    print @long;
    # A demonstration of how to use option sets in a wrapper program.
    @ARGV = qw(Who -a -y foo -r);       # hack up an @ARGV
    my $who = Argv->new(@ARGV);         # instantiate
    $who->dbglevel(1);                  # set verbosity
    $who->optset(qw(UNAME FOO WHO));    # define 3 option sets
    $who->parseUNAME(qw(a m n p));      # parse these to set UNAME
    $who->parseFOO(qw(y=s z));          # parse -y and -z to FOO
    $who->parseWHO('r');                # for the 'who' cmd
    warn "got -y flag in option set FOO\n" if $who->flagFOO('y');
    print Argv->new('uname', $who->optsUNAME)->qx;
    $who->prog(lc $who->prog);          # force $0 to lower case
    $who->exec(qw(WHO));                # exec the who cmd

More advanced examples can be lifted from the test script or the ./examples subdirectory.


RAISON D'ETRE

This module presents an O-O approach to command lines, allowing you to instantiate an 'argv object', manipulate it, and eventually run it, e.g.:

    my $ls = Argv->new('ls', ['-l']));
    my $rc = $ls->system;       # or $ls->exec or $ls->qx

Which raises the immediate question - what value does this mumbo-jumbo add over Perl's native support, e.g.:

    my $rc = system(qw(ls -l));

The answer comes in a few parts:

All of these behaviors can be toggled, either as class or instance attributes. See EXECUTION ATTRIBUTES below.


DESCRIPTION

An Argv object treats a command line as 3 separate entities: the program, the options, and the args. The options may be futher subdivided into user-defined option sets by use of the optset method. When one of the execution methods is called, the parts are reassmbled into a single list and passed to the underlying Perl execution function.

Compare this with the way Perl works natively, keeping the 0th element of the argv in $0 and the rest in @ARGV.

By default there's one option set, known as the anonymous option set, whose name is the null string. All parsed options go there. The advanced user can define more option sets, parse options into them according to Getopt::Long-style descriptions, query or set the parsed values, and then reassemble them in any way desired at exec time. Declaring an option set automatically generates a set of methods for manipulating it (see below).

All argument-parsing within Argv is done via Getopt::Long.


AUTOLOADING

Argv employs the same technique made famous by the Shell module to allow any command name to be used as a method. E.g.

        $obj->date->exec;

will run the 'date' command. Internally this is translated into

        $obj->argv('date')->exec;


FUNCTIONAL INTERFACE

Because the extensions to system/exec/qx described here may be useful in writing portable programs, they're made available for export as traditional functions. Thus:

    use Argv qw(system exec qv);

will override the Perl builtins. There's no way to override the operator qx() so an alias qv() is provided.


CONSTRUCTOR

    my $obj = Argv->new(@list)

The @list is what will be parsed/executed/etc by subsequent method calls. During initial construction, the first element of the list is separated off as the program; the rest is lumped together as part of the args until and unless option parsing is done, in which case matched options are shifted into collectors for their various option sets. You can also create a ``predigested'' instance by passing any or all of the prog, opt, or arg parts as array refs. E.g.

    Argv->new([qw(cvs ci)], [qw(-l -n)], qw(file1 file2 file3));

Predigested options are placed in the default (anonymous) option set.

The constructor can be used as a class or instance method. In the latter case the new object is a deep (full) clone of its progenitor. In fact 'clone' is aliased to 'new', allowing clones to be created via:

        my $copy = $orig->clone;

The first argument to new() or clone() can be a hash-ref, which will be used to set execution attributes at construction time. I.e.:

    my $obj = Argv->new({autochomp => 1, stderr => 0}, @ARGV);

you may choose to add the command line later:

    my $obj = Argv->new;
    $obj->prog('cat');
    $obj->args('/etc/motd');

Or

    my $obj = Argv->new({autochomp=>1});
    my $motd = $obj->argv(qw(cat /etc/motd))->qx;

Or (using the autoloading interface)

    my $motd = $obj->cat('/etc/motd')->qx;


METHODS

INSTANCE METHODS

EXECUTION METHODS

The three methods below are direct analogues of the Perl builtins. They simply reassemble a command line from the prog, opts, and args parts according to the option-set rules described below and invoke their builtin equivalent on it.

EXECUTION ATTRIBUTES

The behavior of the execution methods system, exec, and qx is governed by a set of execution attributes, which are in turn manipulated via a set of eponymous methods. These methods are auto-generated and thus share certain common characteristics:


PORTING

This module is known to work on Solaris 2.5-7 and Windows NT 4.0SP3-5, and with perl 5.004_04 and 5.005_03. As these two platforms are quite different, there should be no major portability issues, but please send bug reports or patches to the address below.


BUGS

Argv uses Getopt::Long::Configure() to modify configurable parameters in order to do some advanced option parsing with Getopt::Long. Unfortunately, older versions of Getopt::Long offer no way to set those parameters back the way they were; they can only be set to their default values. Therefore, when using Getopt::Long 2.23 or above Argv will restore prior settings but with older versions it will reset them to their defaults instead. This may lead to confusing behavior if the using code also calls Getopt::Long::Configure().


AUTHOR

David Boyce <dsb@world.std.com>


COPYRIGHT

Copyright (c) 1999,2000 David Boyce. All rights reserved. This Perl program is free software; you may redistribute and/or modify it under the same terms as Perl itself.


SEE ALSO

perl(1), Getopt::Long(3), IPC::ChildSafe(3)