Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Arrays and GetOpt::Long

by aijin (Monk)
on Jul 04, 2001 at 22:49 UTC ( [id://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: program.pl --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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://93909]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found