Parsing a string with GetOpt::Long::GetOptions

0 votes

I need to parse a string in the same manner as CLI arguments upon being passed to Getopt::Long. The string could have possible command line arguments that it gets by using a Read-Eval-Print-Loop program. Like say, this is my string:

$str = '--infile /tmp/infile_location --outfile /tmp/outfile'

Now, I need to have it parsed with GetOptions in order to have the scope for adding new options later.
I figured maybe splitting the string at its whitespaces and replacing @ARGV with new array could let me call GetOptions on it. So, here's how I've tried doing it so far:

my @arg_arr = split (/\s/, $input_line);

# This is done so that GetOptions reads these new arguments
@ARGV = @arg_arr;
print "ARGV is : @ARGV\n";
GetOptions (
            'infile=s'  => \$infile,
            'outfile=s' => \$outfile

But this just seems like a workaround, is there a more direct approach or a better way to do it? Please Help!

Nov 15, 2018 in Others by Bharani
• 4,560 points

1 answer to this question.

0 votes

Hey, have a look at the section where it says parsing options from an arbitrary string in the man page for Getopt::Long. It seems to be doing the exact same thing you're trying to accomplish.

answered Nov 15, 2018 by nirvana
• 3,060 points

Related Questions In Others

0 votes
1 answer
0 votes
1 answer

Execute a shell script with php

One of the possible reason for this ...READ MORE

answered Feb 23, 2019 in Others by Omkar
• 69,040 points
+1 vote
0 answers

How to split a number with coma betweeen two colunms

Jul 3, 2019 in Others by anonymous
0 votes
1 answer
0 votes
1 answer

Validating String against USPS state abbreviations

This is how I'd do it: private static ...READ MORE

answered Nov 9, 2018 in Others by DataKing99
• 8,150 points
0 votes
1 answer

Issue with the perl script

I don't really know what might the ...READ MORE

answered Jun 22, 2018 in Other DevOps Questions by ajs3033
• 7,280 points
0 votes
1 answer

Bitcoin private key: translate from bash to perl

use bignum; # Get arbitrary precision arithmetic # ...READ MORE

answered Sep 4, 2018 in Blockchain by slayer
• 29,270 points
0 votes
1 answer

Using Bitcoin's sendmany function with perl

Replace the line  my $action = $api->call('sendmany','',%recipients); with  my ...READ MORE

answered Sep 11, 2018 in Blockchain by digger
• 26,670 points
0 votes
1 answer

JSON valid numerical param in Perl

Use 0.0 + sprintf('%.8f', $btc), 0.0 + $data{'amount'} READ MORE

answered Sep 11, 2018 in Blockchain by digger
• 26,670 points