Perl: how can I pass a list of script?

I need to pass a list to my script how can I do this?

for example, I script: flow.pl I need to pass it a list of fubs:

fub1, fub2, fub3 

and database path 1 =

 path1 

and database path 2 =

 path2 

and how can i get these parameters?

  (through $ARGV[i]?) 

if i do:

 flow.pl fub1,fub2,fub3 path1 path2 

and in code:

 $listoffubs = $ARGV[0] $path1 = $ARGV[1] $path2 = $ARGV[2] 

the fubs list gets the name fubs as one word.

+4
source share
3 answers

Having lists of positional arguments is ok if the lists are short and simple. After you enter more arguments or you have arguments with an internal structure, you probably want to look at named arguments.

In this case, I think I would use GetOpt :: Long to implement named arguments.

+7
source

Just split the list:

 my @ListOfFubs = split /,/ , $listoffubs; 
+5
source

Your arguments will be separated by a space, so yes, fub1,fub2,fub3 will be one argument. Just use the space instead, and everything will be fine. For instance:.

 flow.pl fub1 fub2 fub3 path1 path2 my $fub1 = shift; # first argument is removed from ARGV and assigned to $fub1 

or

 my $fub1 = $ARGV[0]; # simple assignment 

All at once

 my ($fub1,$fub2,$fub3,$path1,$path2) = @ARGV; # all are assigned at once 

Note that using shift removes arguments from @ARGV .

If you have a list of arguments that may differ, this is a simple fix to put them last, then do:

 flow.pl path1 path2 fub1 fub2 fub3 ... fubn my $path1 = shift; my $path2 = shift; # remove first two arguments, then my @fubs = @ARGV; # assign the rest of the args to an array 

For more complex processing of arguments, use a module, for example Getopt :: Long .

+1
source

Source: https://habr.com/ru/post/1389238/


All Articles