Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Any way to call GetOptions with evaluated / interpoltated args ?

by jjmoka (Beadle)
on Nov 26, 2018 at 16:06 UTC ( [id://1226355]=perlquestion: print w/replies, xml ) Need Help??

jjmoka has asked for the wisdom of the Perl Monks concerning the following question:

I'd like to call Getopt::Long qw(GetOptions);
where the arguments are built previously.
Here a normal call to GetOptions:
use Getopt::Long qw(GetOptions); my $arg1; GetOptions( 'arg1=s' => \$arg1);
Is there any way to pass the arguments from a previously built scalar/hash/array ?
use Getopt::Long qw(GetOptions); my arg1; my $opt = 'arg1=s => \$arg1'; my @opt = ('arg1=s', '\$arg1'); my %opt = ('arg1=s' => '\$arg1'); GetOptions($opt); #doesn't work GetOptions(@opt); #doesn't work GetOptions(%opt); #doesn't work
I tried different ecaping (i.e. \$, \\$, \\\$),
eval (i.e. eval "GetOptions(...))
eval with explicit global var notation $::arg1
but I cannot have it working.

Replies are listed 'Best First'.
Re: Any way to call GetOptions with evaluated / interpoltated args ?
by LanX (Saint) on Nov 26, 2018 at 16:15 UTC
    variant 2 and 3 should work if you didn't quote the refs...

    i.e. \$arg1 instead of '\$arg1'

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re: Any way to call GetOptions with evaluated / interpoltated args ?
by Eily (Monsignor) on Nov 26, 2018 at 16:16 UTC

    It sounds like an unusual way of doing things, so maybe you should explain the broader context for your question, but to answer it you'll notice that in GetOptions( 'arg1=s' => \$arg1); there are no quotes around the second argument. You shouldn't have quotes in your other attempts (ie, instead of sending a string, send a reference):

    my @opt = ('arg1=s', \$arg1); my %opt = ('arg1=s' => \$arg1);

      Thank you very much. It works. Here my minor reason to want that. I also know I was entering an unusual field (I would even accept to relax sometimes the 'use strict "refs"' to use symbolic references). My command line script is expected to dump a 'usage' message (with the list of options) and a GetOptions to load them. For my personal taste I didn't like to replicate the same list in the 'usage' message and in GetOptions. I wanted a single list of options and then I'm using that list to both create the message and to feed GetOptions. Thanks again.
Re: Any way to call GetOptions with evaluated / interpolated args ?
by hippo (Bishop) on Nov 26, 2018 at 16:41 UTC

    You've omitted the sigil when declaring $arg1 - were you not using strict? Here's the trivial example, anyway:

    $ cat opts.pl #!/usr/bin/env perl use strict; use warnings; use Getopt::Long 'GetOptions'; my $arg1; my %opt = ('arg1=s' => \$arg1); GetOptions (%opt); print "arg1 is $arg1\n" if defined $arg1; $ ./opts.pl --arg1=foo arg1 is foo $

    "It doesn't work" is as useful to a developer as "I'm ill" is to a medic. Try to be specific. SSCCE is best.

Re: Any way to call GetOptions with evaluated / interpoltated args ?
by tinita (Parson) on Nov 26, 2018 at 19:36 UTC
Re: Any way to call GetOptions with evaluated / interpoltated args ?
by 1nickt (Canon) on Nov 26, 2018 at 16:16 UTC

    Hi, one way might be to use Getopt::Long's OO interface. From the doc:

    use Getopt::Long; $p = Getopt::Long::Parser->new; $p->configure(...configuration options...); # Do some computing here to come up with list of option descriptio +ns if ($p->getoptions(...options descriptions...)) ... if ($p->getoptionsfromarray( \@array, ...options descriptions...)) + ...

    Hope this helps!


    The way forward always starts with a minimal test.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1226355]
Approved by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2024-03-28 10:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found