getting arguments

Discussion in 'Perl Misc' started by frytaz@gmail.com, Jul 28, 2007.

  1. Guest

    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
    , Jul 28, 2007
    #1
    1. Advertising

  2. kens Guest

    On Jul 28, 4:01 pm, "" <> 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
    kens, Jul 28, 2007
    #2
    1. Advertising

  3. kens Guest

    On Jul 28, 4:49 pm, kens <> wrote:
    > On Jul 28, 4:01 pm, "" <> 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
    kens, Jul 28, 2007
    #3
  4. <> 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 ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, Jul 28, 2007
    #4
  5. Guest

    On Jul 28, 10:59 pm, (Jens Thoms Toerring) wrote:
    > <> 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 ___
    > \__________________________ 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?
    , Jul 28, 2007
    #5
  6. Uri Guttman Guest

    >>>>> "fc" == frytaz@gmail com <> 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 ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Jul 28, 2007
    #6
  7. 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
    Gunnar Hjalmarsson, Jul 29, 2007
    #7
  8. 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
    Gunnar Hjalmarsson, Jul 29, 2007
    #8
  9. <> 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 ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, Jul 29, 2007
    #9
  10. Gunnar Hjalmarsson <> 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 ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, Jul 29, 2007
    #10
  11. Jens Thoms Toerring wrote:
    > Gunnar Hjalmarsson <> wrote:
    >> 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?


    I understand now that your way would permit to _not_ group words
    together with e.g. quotes. OTOH you also say:

    "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"

    I think I tend to believe that a recommendation to use quotes for
    grouping is preferrable.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jul 29, 2007
    #11
  12. Gunnar Hjalmarsson <> wrote:
    > Jens Thoms Toerring wrote:
    > > Gunnar Hjalmarsson <> wrote:
    > >> 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?


    > I understand now that your way would permit to _not_ group words
    > together with e.g. quotes. OTOH you also say:


    > "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"


    > I think I tend to believe that a recommendation to use quotes for
    > grouping is preferrable.


    Definitely, but the OP may have his reasons to do it otherwise
    and who am I to tell him what to do;-)

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, Jul 29, 2007
    #12
  13. On Sat, 28 Jul 2007 17:07:22 -0400, Sherm Pendley
    <> wrote:

    >Sorry, this is "abuse" - "arguments" is down the hall on your left. :)


    Huh, sorry for being dense... is this the famous Monty Pyton's sketch?


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
    Michele Dondi, Jul 30, 2007
    #13
  14. On Mon, 30 Jul 2007 16:38:13 -0400, Sherm Pendley
    <> wrote:

    >> Huh, sorry for being dense... is this the famous Monty Pyton's sketch?

    >
    >Yep. :)
    >
    >I'm glad someone got it - I dread the day when a Monty Python reference is
    >too obscure for this group...


    Well, I was about to make a joke about a very well known programming
    language occasionally compared with Perl... but I'm refraining to...


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
    Michele Dondi, Jul 31, 2007
    #14
  15. On 31 Jul 2007 07:56:40 -0500, Lawrence Statton
    <> wrote:

    >Something like ...
    >
    > The American comedy troupe Monty Perl was far superior to those lame
    > brits....


    :)


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
    Michele Dondi, Jul 31, 2007
    #15
  16. Ted Zlatanov Guest

    On 31 Jul 2007 07:56:40 -0500 Lawrence Statton <> wrote:

    LS> Michele Dondi <> writes:
    >> Well, I was about to make a joke about a very well known programming
    >> language occasionally compared with Perl... but I'm refraining to...
    >>


    LS> Something like ...

    LS> The American comedy troupe Monty Perl was far superior to those lame
    LS> brits....

    LS> "This is an undef parrot, he has been garbage collected ..... "

    (Waitress) "We have the $ and %browns, $ and @eggs, $! and $@, $! $! $! $@ $@ and $?, $_ $_ $?
    $! $! $_ cheese and $."

    [NB for those who don't have it in their country: "hash browns" are a type of food]

    (Hag) "Do you have anything without $"

    (W) "Well, the @$ref doesn't have much $ in it..."

    (Vikings) "$! $_ $! $_ $? $@ $_ \$_ \$_ $!"

    (H) "I don't like $ and I miss Java variables!!!"



    Ted
    Ted Zlatanov, Jul 31, 2007
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edward Diener
    Replies:
    14
    Views:
    4,930
    Josiah Carlson
    Apr 6, 2004
  2. Neo
    Replies:
    10
    Views:
    656
    sushant
    Jan 20, 2005
  3. tutmann
    Replies:
    4
    Views:
    434
  4. jmborr
    Replies:
    1
    Views:
    409
    Stargaming
    Nov 3, 2007
  5. Replies:
    3
    Views:
    812
Loading...

Share This Page