Re: getting arguments




"Jens Thoms Toerring" <jt@xxxxxxxxxxx> wrote in message
news:5h1p15F3hu78jU1@xxxxxxxxxxxxxxxxxxxx
frytaz@xxxxxxxxx <frytaz@xxxxxxxxx> wrote:
I want to run my per script with arguments, for instance:
./script.pl -one value one text 1 -two value two text 2 -three value
three text 3

The usual convention with atrguments is to have them enclosed in
(double) quotes when they contain spaces, so they arrive as a
whole in your program

and i want to get those arguments in
$one = value one text 1
$two = value two text 2
$three = value three text 3

i tried with regular expression m/-one (.+?) -two (.+?) -three (.+?)/
and it works but when i mix-up arguments like:
./script.pl -two value two text 2 -three value three text 3 -one value
one text 1
then i need to use different regex.

Perhaps this does the trick:

#!/usr/bin/perl

use strict;
use warnings;

my $opts = '^-(one|two|three)$';

$ARGV[ 0 ] =~ /$opts/ or die "Invalid arguments\n";

my $state;
my %args;
for ( @ARGV ) {
if ( /$opts/ ) {
$state = $1;
$args{ $state } = [ ];
next;
}
push @{ $args{ $state } }, $_;
}

my ( $one, $two, $three ) = map { join ' ', @{ $args{ $_ } } }
qw/ one two three /;

Vielen Dank, Jens.
--
Wade Ward


.