Passing options to a subroutine

L

littlepope

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,
 
P

Paul Lalli

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
 
P

Paul Lalli

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top