4

我正在尝试执行以下脚本,但没有获得所需的输出。似乎它仍在保留旧值并继续连接它。

./test.pl -v v10,v11,v12

实际结果 => 'v10','v11','v10','v12','v10','v11','v10'

预期结果 => 'v10','v11','v12'

你能帮忙解决这个问题吗?

#!/usr/bin/perl

use strict;
use warnings;
use Getopt::Long;

my ($versionFlag,$finalVersionList);

sub validateInputAndFilter
{
        my ($passedVersionList) = @_;
        $finalVersionList ="";

        my @versionList = split(",",$passedVersionList);
        foreach my $ver (@versionList)
        {
                if($ver =~ m/^v[0-9][0-9]$/)
                {
                        $finalVersionList .= "'$ver',$finalVersionList";
                }
                else
                {
                        print "\nWARNING: Passed version *** $ver *** is NOT in expected format...skipping...\n";
                }
        }
        $finalVersionList =~ s/\,$//;
        print "\n\nfinalVersionList => $finalVersionList\n\n";
}

GetOptions('v|version=s' => \$versionFlag) || die "Please pass version list separated by comma";

if(defined $versionFlag)
{
        &validateInputAndFilter("$versionFlag");
}
else
{
        die "Please pass version list separated by comma";
}
exit 0;
4

2 回答 2

4

这:

$finalVersionList .= "'$ver',$finalVersionList";

应该只是:

$finalVersionList .= "'$ver,'";

$foo .= $bar; 相当于:

$foo = $foo . $bar;

所以,$finalVersionList .= "'$ver',$finalVerionList";相当于:

$finalVersionList = $finalVersionList . "'$ver',$finalVersionList";
于 2013-04-23T17:07:54.393 回答
2

改变:

 $finalVersionList .= "'$ver',$finalVersionList";

到:

 $finalVersionList .= "'$ver',";

你在这里做多个追加。您最初拥有的内容相当于:

 $finalVersionList = $finalVersionList . "'$ver',$finalVersionList";

这不是你想要的。如果您想'$ver$',成为列表的开头,您可以执行以下操作:

$finalVersionList = "'$ver'," . $finalVersionList;

或者:

 $finalVersionList = "'$ver',$finalVersionList";
于 2013-04-23T17:11:05.250 回答