user782642
user782642

Reputation: 221

Perl: how can i pass list to script?

i need to pass list to my script how can i do it?

for example i have script: flow.pl i need to pass to it list of fubs :

fub1, fub2, fub3 

and path to database1 =

path1 

and path to database2 =

path2 

and how can i get this parameters?

 (through $ARGV[i]?)

if i do:

flow.pl fub1,fub2,fub3 path1 path2

and in the code:

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

list of fubs get name of fubs like one word.

Upvotes: 4

Views: 1219

Answers (3)

TLP
TLP

Reputation: 67900

Your arguments will be space separated, so yes, fub1,fub2,fub3 will be one argument. Just use space instead, and you'll be fine. E.g.:

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 is removing arguments from @ARGV.

If you have a list of arguments that may vary, it is a simple fix to put them last, and 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 complicated argument handling, use a module, such as Getopt::Long.

Upvotes: 1

Dave Cross
Dave Cross

Reputation: 69314

Having lists of positional arguments is ok as long as the lists are short and simple. Once you get into a larger number of arguments or you have arguments with an internal structure, then you probably want to look at named arguments.

In this case, I think I'd be looking at using GetOpt::Long to implement named arguments.

Upvotes: 7

NiklasMM
NiklasMM

Reputation: 2972

Simply split the list:

my @ListOfFubs = split /,/ ,  $listoffubs;

Upvotes: 5

Related Questions