Re: getting arguments
- From: "Wade Ward" <zaxfuuq@xxxxxxxxxxx>
- Date: Mon, 30 Jul 2007 03:15:13 -0700
"Jens Thoms Toerring" <jt@xxxxxxxxxxx> wrote in message
news:5h1p15F3hu78jU1@xxxxxxxxxxxxxxxxxxxx
frytaz@xxxxxxxxx <frytaz@xxxxxxxxx> wrote:Vielen Dank, Jens.
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 /;
--
Wade Ward
.
- References:
- getting arguments
- From: frytaz@xxxxxxxxx
- Re: getting arguments
- From: Jens Thoms Toerring
- getting arguments
- Prev by Date: Re: @arts
- Next by Date: Re: Perl threads
- Previous by thread: Re: getting arguments
- Next by thread: Re: getting arguments
- Index(es):