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: Beginner:
How to handle different amout of arguments in Perl

 



digioleg54
User

May 15, 2017, 12:58 PM

Post #1 of 4 (1253 views)
How to handle different amout of arguments in Perl Can't Post

When I convert shell to Perl I run into situation that the program can get different amount of arguments, and the second argument can be a date, report name, or directory.

How can I handle such situation in Perl?
I know I can use GetOpts.something like

Code
sub option 
{
my %options;
GetOpts 'a:b:c', %options;

if (defined $options{a})
{
my $date = $options{a};
}
(and so on)
}


But the question is if the whole program will know variables from sub option?

Thanks


(This post was edited by digioleg54 on May 15, 2017, 1:40 PM)


FishMonger
Veteran / Moderator

May 15, 2017, 2:47 PM

Post #2 of 4 (1240 views)
Re: [digioleg54] How to handle different amout of arguments in Perl [In reply to] Can't Post

Your %options hash is private to that sub and is not visible anywhere else in your script. Why are you wrapping that in a sub?

IMO, it would be better to use Getopt::Long instead of Getopt::Std.
https://metacpan.org/pod/Getopt::Long


digioleg54
User

May 16, 2017, 6:25 AM

Post #3 of 4 (1219 views)
Re: [FishMonger] How to handle different amout of arguments in Perl [In reply to] Can't Post

I don't really understand, how it works. For example:

Code
system ("/versions/perl-5.22.2/bin/perl $data_path.'/'.coolwcpfile.pl p $procdt $srcdir r $rpt_file");

in coolwcpfile.pl

Code
use Getopt::Long; 

GetOptions(
'procdate=d' => \$procdt ,(d for date or it is string?)
'soursdir=s' => \$srcdir ,
'report_file=s' => \$rpt_file,
) or die "Incorrect usage!\n";

However, my program is using by some other scripts, which can send several more options. Howcan I handle this situation?
Example

Code
GetOptions( 
'procdate=d' => \$procdt ,(d for date or it is string?)
'soursdir=s' => \$srcdir ,
'report_file=s' => \$rpt_file,
'copy=i' =>\$copy, (which can be 1 or not, if not, I will check it in the future and not copy file)
and the same way some other arguments, which for one program will be empty(not define), for another with value
) or die "Incorrect usage!\n";


How can I handle such situation?


FishMonger
Veteran / Moderator

May 16, 2017, 8:05 PM

Post #4 of 4 (1194 views)
Re: [digioleg54] How to handle different amout of arguments in Perl [In reply to] Can't Post


Quote
'procdate=d' => \$procdt ,(d for date or it is string?)

d is not a valid type used in the module. You need to define the date option as a string via the s type.

The syntax in your system call is wrong. Your attempt to concatenate the path won't work and you need to prefix a - hyphen to the option name.

Code
system ("/versionsb/perl-5.22.2/bin/perl $data_path/coolwcpfile.pl -p $procdt -s $srcdir -r $rpt_file");



Quote
However, my program is using by some other scripts, which can send several more options. Howcan I handle this situation?

You need to define all possible options that may be needed.


(This post was edited by FishMonger on May 16, 2017, 8:06 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