Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Accessing Getopt::Long values

by kcott (Chancellor)
on Mar 25, 2013 at 05:43 UTC ( #1025230=note: print w/replies, xml ) Need Help??

in reply to Using getopt::long and how to use the value of one of the command line options in a subroutine

G'day NewLondonPerl1,

This looks like a simple assignment to me:

#!/usr/bin/env perl use 5.010; use strict; use warnings; use Getopt::Long; my %opts; GetOptions(\%opts, qw{conf|c=s}); my $master_conf = $opts{conf}; say $master_conf;

Here it is in action:

$ --conf conf_value conf_value

Depending on where you do the assignment, you may want:

$master_conf = $opts->{conf};

The only place in your posted code that you mention $master_conf is in what appears to be a standalone piece of code with no obvious connection to other parts of your script:

my $json_data = extract_json("$master_conf");

Where were you intending to declare this variable? Where were you intending to assign it a value? I suspect one problem you have here is that you can't see the wood for the trees: probably 80% of the code you've posted has nothing to do with the issue at hand. Try making a simple, cut-down version of the script; when that's working, add in more complexity.

From the code you've posted, my best guess as to what you want is something like this:

# shebang and use <whatever> lines here my $opts = getoptions(); my $master_conf = $opts->{conf}; # do something with $master_conf here (validation perhaps) my $json_data = extract_json($master_conf); my $decoded_json_obj = decode_json($json_data); # do something with $decoded_json_obj here # subroutine definitions here

-- Ken

Replies are listed 'Best First'.
Re^2: Using getopt::long and how to use the value of one of the command line options in a subroutine
by NewLondonPerl1 (Acolyte) on Mar 25, 2013 at 06:16 UTC
    Thanks ever so much for your advice Ken. You are right it does make sence to break this down into smaller script first and get that working. I will try out your suggestions and try and get this working before trying to add in more complexity

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1025230]
[marioroy]: Yes, will do for PM. I love PM.
Lady_Aleena ponders a meditation of her own, but it would be on a far far less important topic.
[marioroy]: I'm hoping to have a release in about a week's time.
Discipulus supermario..
marioroy MCE isn't important by any means. Just wanted to complete this task. Hobo needed more love and MCE::Shared as well. It wasn't easy at all.
[marioroy]: Discipulus No supermario. I'm a humbled person within. Just wanted to complete this journey.

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2017-05-26 08:58 GMT
Find Nodes?
    Voting Booth?