Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Arrays and GetOpt::Long

by aijin (Monk)
on Jul 04, 2001 at 22:49 UTC ( #93909=perlquestion: print w/replies, xml ) Need Help??
aijin has asked for the wisdom of the Perl Monks concerning the following question:

I'm working on a fairly complicated (for me) collection of code and I would really love for one of the scripts to be able to take a list as one of it's arguments.

The problem is, I've no idea how to make this work both for the user accessing this script from the shell (which is highly likely), and for the scripts that will be calling it. It's easy enough to pass a reference to an array to another script being run, but that's not exactly something that a user will be able to do.

Just for reference, the list is a number of queries that the user wants to run through the program, so it's not a case of trying to stack all my options into an array. I have no advance knowledge of what these queries might be, either. Don't panic, though. They don't get eval'd.

I've been asked to keep my code consistent with the other developers, and they all use Getopt::Long to pull in comand line options, so that's what I'm stuck with.

So, does anyone out there have any suggestions how I can get lists as input through Getopt::Long in the command line and when being run by another script?

-aijin, probably missing something obvious...

Replies are listed 'Best First'.
Re: Arrays and GetOpt::Long
by DrZaius (Monk) on Jul 04, 2001 at 23:25 UTC
    Read the manpage for Getopt::Long

    If you pass GetOptions an array ref for one of the values, it will populate it as such.

    So you have to call you program like this: --thing a --thing b --thing c.

    Your script will look something like this:

    #!/usr/bin/perl -w use strict; use Getopt::Long; GetOptions( 'thing=s' => \my @things, ); print join("\n", @things), "\n";
Re: Arrays and GetOpt::Long
by John M. Dlugosz (Monsignor) on Jul 04, 2001 at 23:22 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://93909]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2018-06-24 21:14 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.