Larry
Larry

Reputation:

How can I make Perl's Getopt::Long not process a literal = in a parameter name?

I wrote a Perl program "transfer.pl" and the input parameter is the hash value (the key and the value are strings). The code segment is:

my %transfers = ();

if (!GetOptions("transfer=s" => \%transfers))
{
    Usage();
    exit(1);
}

I used the windows system. On the command line, I typed:

perl tranfer.pl --transfer "table = %s"="[TableName=%s]"

I hope the key is table = %s and the value is [TableName=%s], but it seems Getopt::Long always finds the first = so the key is table and the value is %s=[TableName=%s].

When I typed

perl tranfer.pl --transfer "table \= %s"="[TableName\=%s]"

The key is table \, the value is %s=[TableName\=%s].

I want to know how to bypass the "=" in my string value and make the code do what I expect?

Thank you very much!

Upvotes: 2

Views: 334

Answers (2)

ysth
ysth

Reputation: 98388

Getopt::Long will not allow this; the first = is always used to separate the key from the value. You will need to use a user-defined subroutine to handle the option, or split up the key=value pairs after GetOptions is done or use some arbitrary escaping mechanism like using "%25" to represent a = in a key.

Upvotes: 2

Brad Gilbert
Brad Gilbert

Reputation: 34120

From Getopt::Long

If the option destination is a reference to a hash, the option will take, as value, strings of the form key=value. The value will be stored with the specified key in the hash.

GetOptions ("define=s" => \%defines);

Alternatively you can use:

GetOptions ("define=s%" => \$defines);

When used with command line options:

--define os=linux --define vendor=redhat

It will always split on the first "=".

Upvotes: 1

Related Questions