[comp.lang.perl] Novice perl programmer needs help

brad@SSD.CSD.HARRIS.COM (Brad Appleton) (03/28/91)

I just released parseargs on comp.sources.misc. Parseargs contains a perl
function as a perl-interface to parseargs(1). I was hoping some of you 
could help me to write better perl code.

How would you improve the following (make it faster, prettier, etc...)?

PS - To the perl-user that requested parseargs from me by e-mail:
       I lost your e-mail address, please resend your request!!!

;#########################################################################
;# ^FILE: parseargs.pl - parseargs for perl programs
;#
;# ^DESCRIPTION:
;#    This file defines a perl function named parseargs to parse
;#    command-line arguments for perl scripts.
;#
;# ^HISTORY:
;#    02/25/91	Brad Appleton	<brad@ssd.csd.harris.com>	Created
;##^^#####################################################################


;########
;# ^FUNCTION: parseargs - parse command-line argument vectors
;#
;# ^SYNOPSIS:
;#    rc = &parseargs( @argv, $argd )
;#
;# ^PARAMETERS:
;#    argv -- the vector of command-line arguments (usually ARGV).
;#    argd -- the argument-description string
;#
;# ^DESCRIPTION:
;#    Parseargs will invoke parseargs(1) to parse the command-line given
;#    in <argv> for the command defined by <argd>.  The resulting values
;#    will be assigned to the variables indicated by the argument-description
;#    string.
;#
;# ^REQUIREMENTS:
;#    Any desired initial values for variables from the argument-description
;#    string should be assigned BEFORE calling this function.
;#
;#    The following global variables may be assigned before calling parseargs:
;#
;#       PARSEOPTS -- any extra options to pass to parseargs(1) (default="-u")
;#
;# ^SIDE-EFFECTS:
;#    The global variable PARSEARGS will contain the command-line used to
;#    invoke parseargs(1).
;#
;#    ARGV and ARGC may be reset, all other values are (re)set in <arr>.
;#
;# ^RETURN-VALUE:
;#    The exit code returned by parseargs(1).
;#
;# ^ALGORITHM:
;#    - set defaults for PARSEOPTS
;#    - build the parseargs command (dont forget to quote arguments).
;#    - run parseargs(1) and evaluate the output unless $?
;##^^####

sub parseargs {
    local(@argv) = @_;
    local($argd);
    local($parse_output);
    local($_);
    local($[) = 0;
    local($i);

    $argd = pop( @argv );   ## get last arg and remove it

    if ( $PARSEOPTS == "" ) {
        $PARSEOPTS = '-u';
    }
    $PARSEARGS = 'parseargs -s perl ' . $PARSEOPTS . " -- '" . $0 . "'";
    
    ## need to put each element of @argv in single quotes
    for ( $i = $[ ; $i <= $#argv ; $i++ ) {
      $argv[$i] =~ s/'/'\\''/g;
      $PARSEARGS .= " '" . $argv[$i] . "'";
    }
    $parse_output = `echo \'$argd\' | $PARSEARGS`;
    eval $parse_output unless $?;
    if ( $? ) {
      $! = 0;
      die "\n";
    }
}

1;
______________________ "And miles to go before I sleep." ______________________
 Brad Appleton           brad@ssd.csd.harris.com       Harris Computer Systems
                             uunet!hcx1!brad           Fort Lauderdale, FL USA
~~~~~~~~~~~~~~~~~~~~ Disclaimer: I said it, not my company! ~~~~~~~~~~~~~~~~~~~