Search code examples
perlgetopt-long

Passing multiple file lists to perl script


I want to pass two file lists to my perl script and have them handled with Getopt::Long for storing an array (via a reference) in a dictionary.

#!/usr/bin/env perl

# author:sb2


use strict;
use warnings;
use Getopt::Long;
use File::Basename;
use Data::Dumper;


print Dumper(@ARGV);

my($config);

$config = &configure(scalar @ARGV);

sub configure{
    my $args = shift;
    my $config = {};
    my @current_samples = ();
    #my @old_samples = ();
    $config = {'current_samples' => \@current_samples};
    #$config = {'old_samples' => \@old_samples};
    GetOptions($config, 
           #"old_samples=s{,}",
           "current_samples=s{,}",
               "help|h!", )
        || warn "error : $!\n";
    print Dumper($config);
    return($config);
}

I can happily pass one file list and have it stored as expected:

[sb2 ~]$ perl test.pl -current_samples WS*
$VAR1 = '-current_samples';
$VAR2 = 'WS68726_1401';
$VAR3 = 'WS68726_1402';
$VAR4 = 'WS68726_1500';
$VAR5 = 'WS68726_1501';
$VAR1 = {
          'current_samples' => [
                                 'WS68726_1401',
                                 'WS68726_1402',
                                 'WS68726_1500',
                                 'WS68726_1501'
                               ]
        };

However, when I uncomment my second list parameter and use that my 'current_samples' variable is now a string with a single filename. Although the 'old_samples' variable has parsed correctly (as above):

[sb2 ~]$ perl test.pl -current_samples WS* -old_samples HG*
$VAR1 = '-current_samples';
$VAR2 = 'WS68726_1401';
$VAR3 = 'WS68726_1402';
$VAR4 = 'WS68726_1500';
$VAR5 = 'WS68726_1501';
$VAR6 = '-old_samples';
$VAR7 = 'HG001';
$VAR8 = 'HG002';
$VAR9 = 'HG003';
$VAR1 = {
          'current_samples' => 'WS68726_1501'
          'old_samples' => [
                             'HG001',
                             'HG002',
                             'HG003'
                           ]
        };

I tried swapping the order of variables around and the only one that made a difference was switching the config assignment ones:

sub configure{
    my $args = shift;
    my $config = {};
    my @current_samples = ();
    #my @old_samples = ();
    $config = {'current_samples' => \@current_samples};
    #$config = {'old_samples' => \@old_samples};
    GetOptions($config, 
           "current_samples=s{,}",
           "old_samples=s{,}",
               "help|h!", )
        || warn "error : $!\n";
    print Dumper($config);
    return($config);
}

Produces:

[sb2 ~]$ perl test.pl -current_samples WS* -old_samples HG*
$VAR1 = '-current_samples';
$VAR2 = 'WS68726_1401';
$VAR3 = 'WS68726_1402';
$VAR4 = 'WS68726_1500';
$VAR5 = 'WS68726_1501';
$VAR6 = '-old_samples';
$VAR7 = 'HG001';
$VAR8 = 'HG002';
$VAR9 = 'HG003';
$VAR1 = {
          'current_samples' => [
                                 'WS68726_1401',
                                 'WS68726_1402',
                                 'WS68726_1500',
                                 'WS68726_1501'
                               ],
          'old_samples' => 'HG003'
        };

I can't see anything in the GetOptions CPAN page which alludes to this ordering affect so any help would be greatly appreciated!


Solution

  • From your commented code it looks like you are overwriting $config with these lines:

    $config = {'current_samples' => \@current_samples};
    #$config = {'old_samples' => \@old_samples};
    

    Instead, do all config assignments in one line:

    my $config = {
      'current_samples' => \@current_samples,
      'old_samples' => \@old_samples,
    };
    

    Or you can do them in single lines and assign to the keys of the hashref:

    my $config = {};
    $config->{'current_samples'} = \@current_samples;
    $config->{'old_samples'} = \@old_samples;