CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate: Re: [digioleg54] I am loosing value of my argument: Edit Log



FishMonger
Veteran / Moderator

Jul 20, 2016, 2:20 PM


Views: 14865
Re: [digioleg54] I am loosing value of my argument

Moving PM communication over to the original thread where it belongs.


Quote
I use it in a different program. But after that statement print args->{queue} = 1, but supposed to be different. And I don't know why.the line is:

if (!GetOptions($args,"help","dir=s","dev","port=s","end_date=s","pam","xrun","tb","nav","use_pos","basis=s","env=s","final","dev","journals","journals_load","close","no_glck","calc_nav","recycle","check_mzpx","performance","queue") || !$args->{end_date} || !$args->{port} )
{
usage(" -port and -end_date are required parameters");
}
I print exactly after the line and get 1.Something is wrong.


That is difficult to read, and difficult to maintain. At the very least you should add vertical whitespace i.e., put each option on a separate line.

Here's a cleaned up version.

Code
#!/usr/bin/perl 

use strict;
use warnings FATAL => 'all';
use Getopt::Long;
use Data::Dumper;

my $args = {};
GetOptions($args,
'help',
'dir=s',
'dev',
'port=s',
'end_date=s',
'pam',
'xrun',
'tb',
'nav',
'use_pos',
'basis=s',
'env=s',
'final',
'dev',
'journals',
'journals_load',
'close',
'no_glck',
'calc_nav',
'recycle',
'check_mzpx',
'performance',
'queue'
) or usage('Error in processing args');


if (! exists $args->{end_date} || ! exists $args->{port}) {
usage('-port and -end_date are required parameters');
}

print Dumper $args;

sub usage {
my $msg = shift || 'Error';
die "$msg\n";
}


Instead of the usage sub, a better approach would be to add the Pod::Usage module. An example of its usage is in the Getopt::Long documentation.
http://search.cpan.org/~jv/Getopt-Long-2.49.1/lib/Getopt/Long.pm#Documentation_and_help_texts


Code
C:\test>Perl-1.pl -end_date now -port 3 -app 
Unknown option: app
Error in processing args


Code
C:\test>Perl-1.pl -end_date now -port 3 
$VAR1 = {
'port' => '3',
'end_date' => 'now'
};



(This post was edited by FishMonger on Jul 20, 2016, 2:24 PM)


Edit Log:
Post edited by FishMonger (Veteran) on Jul 20, 2016, 2:24 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives