Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > getting arguments

Reply
Thread Tools

getting arguments

 
 
frytaz@gmail.com
Guest
Posts: n/a
 
      07-28-2007
Hi there
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
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.

Someone know other solution ?

Thanks in advance

 
Reply With Quote
 
 
 
 
kens
Guest
Posts: n/a
 
      07-28-2007
On Jul 28, 4:01 pm, "(E-Mail Removed)" <(E-Mail Removed)> wrote:
> Hi there
> 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
> 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.
>
> Someone know other solution ?
>
> Thanks in advance


Take a look at the Getopt::Long module.

perldoc Getopt::Long

Note that if there are spaces in the option strings, they will have to
be quoted on the command line.

For example, ./script.pl -two "value two text 2" -three "value three
text"

HTH, Ken

 
Reply With Quote
 
 
 
 
kens
Guest
Posts: n/a
 
      07-28-2007
On Jul 28, 4:49 pm, kens <(E-Mail Removed)> wrote:
> On Jul 28, 4:01 pm, "(E-Mail Removed)" <(E-Mail Removed)> wrote:
>
>
>
> > Hi there
> > 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
> > 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.

>
> > Someone know other solution ?

>
> > Thanks in advance

>
> Take a look at the Getopt::Long module.
>
> perldoc Getopt::Long
>
> Note that if there are spaces in the option strings, they will have to
> be quoted on the command line.
>
> For example, ./script.pl -two "value two text 2" -three "value three
> text"
>
> HTH, Ken


Of course that example should have been :

../script.pl --two "value two text 2" --three "value three text"

Have to use double dashs before the option if it is not a single
character.

Ken

 
Reply With Quote
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      07-28-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> 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 /;

Regards, Jens
--
\ Jens Thoms Toerring ___ (E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
frytaz@gmail.com
Guest
Posts: n/a
 
      07-28-2007
On Jul 28, 10:59 pm, (E-Mail Removed) (Jens Thoms Toerring) wrote:
> (E-Mail Removed) <(E-Mail Removed)> 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 /;
>
> Regards, Jens
> --
> \ Jens Thoms Toerring ___ (E-Mail Removed)
> \__________________________ http://toerring.de


This script works, but when I'm trying to use it with some other array

sub myagr {
my ($data, $server, $witem) = @_;
my ($cmd, @rest) = split(/ /, $data);

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

$rest[0] =~ /$opts/ or $badarg=1;

if ($badarg eq 1) {
print "Bad arguments";
} else {
$state;
%args;
for ( @rest ) {
if ( /$opts/ ) {
$state = $1;
$args{ $state } = [ ];
next;
}
push @{ $args{ $state } }, $_;
}
my ( $one, $two, $three, $four, $five ) = map { join ' ',
@{ $args{ $_ } } } qw/ one two three four five /;
# ^ in that line $_ doesn't contain any data

print "one $one two $two three $three four $four five $five";
#prints one two three four five
}
}

why $_ losing data?

 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      07-28-2007
>>>>> "fc" == frytaz@gmail com <(E-Mail Removed)> writes:

fc> why $_ losing data?

because you are not using one of the core modules to get command line
arguments. use them and don't write a broken one yourself.

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      07-28-2007
(E-Mail Removed) wrote:
>
> This script works, but when I'm trying to use it with some other array


use strict;
use warnings;

missing.

> sub myagr {
> my ($data, $server, $witem) = @_;
> my ($cmd, @rest) = split(/ /, $data);
>
> $opts = '^-(one|two|three|four|five)$';
>
> $rest[0] =~ /$opts/ or $badarg=1;
>
> if ($badarg eq 1) {
> print "Bad arguments";
> } else {
> $state;
> %args;
> for ( @rest ) {
> if ( /$opts/ ) {
> $state = $1;
> $args{ $state } = [ ];
> next;
> }
> push @{ $args{ $state } }, $_;
> }
> my ( $one, $two, $three, $four, $five ) = map { join ' ',
> @{ $args{ $_ } } } qw/ one two three four five /;
> # ^ in that line $_ doesn't contain any data
>
> print "one $one two $two three $three four $four five $five";
> #prints one two three four five
> }
> }
>
> why $_ losing data?


I don't understand what you mean. Assuming that this line is added to
your script:

myagr ( "$0 @ARGV" );

this is the result I get:

C:\home>perl test.pl "-one first_arg -two second_arg -three third_arg
-four fourth_arg -five fifth_arg"
one first_arg two second_arg three third_arg four fourth_arg five fifth_arg
C:\home>

Another thing is that it is a very odd way of passing arguments to a
script, and has little to do with what Jens showed you...

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      07-29-2007
Jens Thoms Toerring wrote:
>
> 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 /;


Why the deep data structure? Why not just:

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

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

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      07-29-2007
(E-Mail Removed) <(E-Mail Removed)> wrote:
> This script works, but when I'm trying to use it with some other array


> sub myagr {
> my ($data, $server, $witem) = @_;
> my ($cmd, @rest) = split(/ /, $data);


It's impossible to say what's happening here unless you tell
exactly with what arguments the function is called. But it
already looks suspicious that $data is a scalar. It only
could work if you call the function with something like

myagr( ( join ' ', ( $0, @ARGV ) ), ...)

but then I wouldn't know why you first join the scripts name and
the command line arguments into a single string to pass it to
the function, only to split it again within the function...

> $opts = '^-(one|two|three|four|five)$';


> $rest[0] =~ /$opts/ or $badarg=1;


> if ($badarg eq 1) {
> print "Bad arguments";
> } else {
> $state;
> %args;
> for ( @rest ) {
> if ( /$opts/ ) {
> $state = $1;
> $args{ $state } = [ ];
> next;
> }
> push @{ $args{ $state } }, $_;
> }
> my ( $one, $two, $three, $four, $five ) = map { join ' ',
> @{ $args{ $_ } } } qw/ one two three four five /;
> # ^ in that line $_ doesn't contain any data


> print "one $one two $two three $three four $four five $five";
> #prints one two three four five
> }
> }


> why $_ losing data?


My best guess is @rest doesn't contain what you expect it to,
did you try to print it out to see what elements it actually
has?
Regards, Jens
--
\ Jens Thoms Toerring ___ (E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      07-29-2007
Gunnar Hjalmarsson <(E-Mail Removed)> wrote:
> Jens Thoms Toerring wrote:
> >
> > 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 /;


> Why the deep data structure? Why not just:


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


But then you would only store the last of the words, i.e. for the
command line argument "-one one test 1", would would only keep
the "1" and would not get "one test 1" as I understand the OP
wanted. Or am I missing something?

Regards, Jens
--
\ Jens Thoms Toerring ___ (E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Call again a variadic function (... variable number of arguments)with same arguments that its variadic wrapper moreau.steve@gmail.com C Programming 3 12-31-2008 07:13 AM
how to pass a function name and its arguments inside the arguments of other function? jmborr Python 1 11-03-2007 08:20 AM
function default arguments from other arguments tutmann C++ 4 10-17-2006 08:00 PM
function call with arguments which takes no arguments Neo C Programming 10 01-20-2005 06:31 AM
Difference between default arguments and keyword arguments Edward Diener Python 14 04-05-2004 11:26 PM



Advertisments