将多个文件列表传递给perl脚本

时间:2015-05-20 14:58:45

标签: perl getopt-long

我想将两个文件列表传递给我的perl脚本,并使用Getopt::Long处理它们,以便在字典中存储数组(通过引用)。

#!/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);
}

我可以愉快地传递一个文件列表并按预期存储它:

[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'
                               ]
        };

然而,当我取消注释我的第二个列表参数并使用我的' current_samples'变量现在是一个具有单个文件名的字符串。虽然' old_samples'变量已正确解析(如上所述):

[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'
                           ]
        };

我尝试交换变量的顺序,唯一有效的是切换配置分配:

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);
}

产地:

[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'
        };

我无法在GetOptions CPAN页面中看到任何暗示此排序影响的内容,因此我们将非常感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

从您的评论代码中,您似乎用这些行覆盖$config

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

相反,请在一行中执行所有配置分配:

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

或者您可以在单行中执行它们并分配给hashref的键:

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

答案 1 :(得分:1)

作为替代解决方案,Getopt::Declare具有支持将参数加载到数组引用中的语法:

use strict; 
use warnings; 

use Getopt::Declare;

my $args = Getopt::Declare->new(
   join( "\n",
      '[strict]',
      "-current-samples <files>... \t List of file names",
      "-old-samples     <files>... \t List of file names",
   )
) || exit(1);

每个标记后的...告诉Getopt :: Declare将参数收集到数组引用中。

然后,您只需在命令行中指定多个以空格分隔的值:

perl test-getopt-declare.pl -current-samples a b c d e f -old-samples 1 2 3 4 5 6