Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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 03:51 UTC ( #1025226=perlquestion: print w/ replies, xml ) Need Help??
NewLondonPerl1 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perlmonks I am trying to get my head around using getopt::long and inparticular how excute a subroutine depending on if a certain command line option has a value assigned. I have read through the perl documentation re using getopt::long but I can't find any example of how to do this so I was hoping you might be able to help me please. Below is the code I have written so far:

#!/bbs/opt/bin/csperl5.12 use strict; no strict "refs"; use warnings; use File::Compare; use File::Basename; use IO::File; use Sys::Hostname; use Data::Dumper; use Tie::File; use Encode; use lib "/bbsrc/bin/prod/lib/site_perl"; use JSON::XS qw( decode_json ); use Getopt::Long; sub usage { my $prog = basename $0; print <<_USAGE_END; Usage:: $prog -h | <tag name> | [-v] [ --validate] <Json cfg locat +ion> <Output file location> --verbose | -v Run tool in verbose mode. --help | -h Usage information. --validate | -V Validate Json object and Validate this again +st Json schema. --conf | -c Json object file location --output | -o Output location for nfs mount report --log | -l Output logfile location. --tag | -t Generate output file for all nfs mounts for a pa +rticular tag. --mount | -m Specify mountpoint name for example software, +tools etc: This will generate an output file detailing all mounts tha +t are required for the current server this tool is being executed on. --host | -H Specify a server hostname and this tool will gen +erate a report detailing every single nfs mount that is required on t +hat particular server. _USAGE_END } sub getoptions { my %opts = qw( verbose 0 ); Getopt::Long::Configure("bundling"); unless ( GetOptions ( \%opts, qw[ verbose|v help|h validate|V conf|c=s output|o=s log|l=s tag|t=s mount|m=s host|H=s ] ) ) { usage(); exit 1; } usage(), exit 0 if $opts{help}; if (!@ARGV) { print "Error:: No arguments specified to $0.\n"; usage(); exit 1; } return \%opts; } #----------------------------------------------------------- MAIN: { my $opts = getoptions(); }

What I am trying to do is if the command line option --conf is specified then I want to set the value of this to $master_conf (this config file is json data that I am decoding). I have the following subroutine which I want to use to decocde this config file but I am not too sure how to set the value of --conf to $master_conf so I can then call this subroutine. Please can you help me on what I need to do here?

sub extract_json { my $file = shift; local $/; #enable slurp open my $fh, "<", "$file"; my $json = <$fh>; return $json; } my $json_data = extract_json("$master_conf"); my $decoded_json_obj = decode_json( $json_data);

Comment on Using getopt::long and how to use the value of one of the command line options in a subroutine
Select or Download Code
Accessing Getopt::Long values
by kcott (Abbot) on Mar 25, 2013 at 05:43 UTC

    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:

    $ pm_getopt_var.pl --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

      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
Re: Using getopt::long and how to use the value of one of the command line options in a subroutine
by GrandFather (Cardinal) on Mar 25, 2013 at 23:18 UTC

    For your own sake don't no strict "refs";. If you are getting errors you don't understand that are suppressed by including that line then you probably have bigger issues. Trim the code down to just enough to show the issue then come here for advice on a better solution.

    True laziness is hard work

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2014-12-18 01:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (41 votes), past polls