yoda54 has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,
Please enlighten me! I've RTFMed unsuccessfully...

1) Say I want to run a script like this:

user@host $./perl-script --xvariables 1 2 3 4 --yvariables 9 8 7

Now I know about @ARGV but is there a way I can pull another set of parameters on the command line to a different array? Is there an @ARGV2 ??

2) Is there a way to timeout a <STDIN> and continue on if input has not been entered in a given amount of time?

Thanks much for any help!

20040519 Edit by castaway: Changed title from 'Perl misc questions'

Replies are listed 'Best First'.
Re: Processing command line arguments
by Aragorn (Curate) on May 19, 2004 at 08:21 UTC
    1. No, there isn't an @ARGV2. All the elements on the command-line end up in @ARGV. The standard Getopt::Std and Getopt::Long can help you parse the command-line options.
    2. One way of doing this is having a SIGALRM signal sent to your process. This can be done using the alarm system call (perldoc -f alarm).
    Arjen
Re: Processing command line arguments
by El Linko (Beadle) on May 19, 2004 at 08:34 UTC
    2 - Another option is select ( perldoc -f select ).

    The code below will give you 5 seconds to enter text on STDIN.
    A higher level interface seems to be provided by IO::Select.
    use strict; use warnings; my $seconds = 5; my $rin = ""; vec( $rin , fileno( STDIN ) , 1 ) = 1; my ( $found , $left ) = select( $rin , undef , undef , $seconds ); if( $left ) { print "you said " . <STDIN> . "\n"; } else { print "nothing to see\n"; }
Re: Processing command line arguments
by Zaxo (Archbishop) on May 19, 2004 at 11:43 UTC

    None of the getopt library like solutions, such as Getopt::Long, admit the syntax you want. If you can accept the syntax change to ./perl-script --xvariables 1,2,3,4 --yvariables 9,8,7 this won't be too difficult. Just make each multivalued option a string option and then split that string on comma to get arrays of values. This precludes data with commas, of course, but the idea can be modified to accomodate that. The following is adapted from Getopt::Long pod.

    use Getopt::Long; use vars qw/@xvals @yvals/; my $result = GetOptions ( 'xvariables=s' => \@xvals, 'yvariables=s' => \@yvals, ); @xvals = split /,/, join ',', @xvals; @yvals = split /,/, join ',', @yvals;
    That lets you specify multiple values in one or more invocations of the option with comma delimited lists.

    After Compline,
    Zaxo

Re: Processing command line arguments
by DrHyde (Prior) on May 19, 2004 at 08:33 UTC
    The really badly documented Term::ReadLine will solve your timeout issue.
Re: Processing command line arguments
by Abigail-II (Bishop) on May 19, 2004 at 09:15 UTC
    Now I know about @ARGV but is there a way I can pull another set of parameters on the command line to a different array? Is there an @ARGV2 ??
    What other set of parameters would that be? There is only one set of parameters, and those are determined before the program is started. Once started, there is no way of adding "new" parameters. I guess you have a misconception of some kind, but I don't know what it is.

    Abigail

Re: Processing command line arguments
by Scarborough (Hermit) on May 19, 2004 at 13:17 UTC
    I expect Zaxo's answer above is what you need but as a relative beginner myself, I had to do a similar task and used an adaption of the named peramter way of passing into a sub routine.
    My command line arguements look like this XPARAM=robert YPARAM=helen for example.Then
    %params; foreach $arg (@ARGV){ ($key,$val)= split '=',$arg; $params{$key) = $val; }
    I'm sure if you wonted to you could do something like XPARAM='1,2,3' and then split the hash values into an array.

    A bit long winded I know but as a beginner possibly easy to understand.

Re: Processing command line arguments
by tbone1 (Monsignor) on May 19, 2004 at 13:31 UTC
    For part 1), you might have to ignore the Getopt:: schtuff and just roll your own by hacking through @ARGV. It's ugly, it's messy, its mama dresses it funny, but it can work. I don't recommend it if you can avoid it, but sometimes ya gots ta do what ya gots ta do, especially when there's a deadline. Just don't expect leaps of joy and shouts of happiness when you go through a code review.

    --
    tbone1, YAPS (Yet Another Perl Schlub)
    And remember, if he succeeds, so what.
    - Chick McGee

      tbone, just wanted to say thanks for a) giving me a nice laugh to start a day of hard work and b) lending weight to my suspicion that just looping through @ARGV is what's going to get it done.

      Time to make the doughn...er...program!


      I like computer programming because it's like Legos for the mind.