Passing options to a subroutine

Discussion in 'Perl Misc' started by littlepope@gmail.com, Oct 5, 2005.

  1. Guest

    Hello all,
    I am trying to pass a message to a subroutine and then use Getopt::Long

    to break the options down in the message. The problem is that I get
    nothing in return. If I put a print statement in I am getting the
    message passed to the subroutine but the GetOptions does not seam to
    work.


    Here is an example of what I am trying to do.
    #!/usr/bin/perl -w
    use strict;
    use Getopt::Long;


    my $message ='-r fatal -m "this is a test"';
    parse($message);


    sub parse
    {
    my @ARGV=@_;
    #print @ARGV;
    my $r = ''; # option variable with default value
    my $m = ''; # option variable with default value
    GetOptions ('r=s' => \$r, 'm=s' => \$m);
    print "$r $m\n";



    }


    When this is run I get a blank return.

    Any ideas??
    Thanks,
     
    , Oct 5, 2005
    #1
    1. Advertising

  2. Paul Lalli Guest

    wrote:
    > Hello all,
    > I am trying to pass a message to a subroutine and then use Getopt::Long
    >
    > to break the options down in the message. The problem is that I get
    > nothing in return. If I put a print statement in I am getting the
    > message passed to the subroutine but the GetOptions does not seam to
    > work.
    >
    >
    > Here is an example of what I am trying to do.
    > #!/usr/bin/perl -w
    > use strict;
    > use Getopt::Long;
    >
    >
    > my $message ='-r fatal -m "this is a test"';
    > parse($message);
    >
    >
    > sub parse
    > {
    > my @ARGV=@_;
    > #print @ARGV;
    > my $r = ''; # option variable with default value
    > my $m = ''; # option variable with default value
    > GetOptions ('r=s' => \$r, 'm=s' => \$m);
    > print "$r $m\n";
    >
    >
    >
    > }


    You have at least two separate problems with this code.
    First, GetOptions operates on @main::ARGV, not a lexical variable.
    That is, you need to give the global @ARGV array a temporary value,
    rather than delcaring a lexical array named @ARGV.
    Second, @ARGV needs to contain a list of the command line options, not
    one single space-separated string of arguments. Ordinarily, your shell
    splits the command line and feeds the list of arguments to your script
    before perl is ever run. If you're doing it manually, you need to make
    sure you process the string correctly as well (ie, taking care to keep
    strings enclosed by quotes together as one argument). Fortunately, the
    answer to how to solve this problem is in the FAQ: perldoc -q split

    Putting both corrections together leaves us with:
    #!/usr/bin/perl
    use strict;
    use warnings;
    use Getopt::Long;
    use Text::parseWords;

    my $message ='-r fatal -m "this is a test"';
    parse($message);

    sub parse{
    local @ARGV = quotewords (" ", 0, $_[0]); #local, not my !!
    my $r = ''; # option variable with default value
    my $m = ''; # option variable with default value
    GetOptions ('r=s' => \$r, 'm=s' => \$m);
    print "r: '$r', m: '$m'\n";

    }
    __END__

    r: 'fatal', m: 'this is a test'

    Hope this helps,
    Paul Lalli
     
    Paul Lalli, Oct 5, 2005
    #2
    1. Advertising

  3. Guest

    Excellent, thanks for the help this makes more sense now, and it works!
     
    , Oct 5, 2005
    #3
  4. Paul Lalli Guest

    wrote:
    > Excellent, thanks for the help this makes more sense now, and it works!


    You're welcome. You can best show your appreciation by following the
    standard customs and traditions of this group, and quoting an
    appropriate amount of context when you post a reply. (Please also have
    a look at the Posting Guidelines for this group).

    Looking into the Text::parseWords module a bit further, I see that the
    module contains a special case for exactly the purpose of splitting a
    list of strings as would be done by the shell. My call to quotewords()
    in the original reply can be replaced by the much shorter and distinct:

    local @ARGV = shellwords(@_);

    Just an FYI,
    Paul Lalli
     
    Paul Lalli, Oct 5, 2005
    #4
    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. Replies:
    6
    Views:
    2,771
    Eric Bohlman
    Apr 4, 2005
  2. Cliff  Martin
    Replies:
    1
    Views:
    3,080
    Larry Smith
    Jan 31, 2007
  3. jacksu
    Replies:
    0
    Views:
    537
    jacksu
    Oct 9, 2007
  4. Utkado
    Replies:
    2
    Views:
    2,222
  5. king
    Replies:
    5
    Views:
    210
Loading...

Share This Page