在perl中向glob函数传递更多参数

时间:2013-12-05 00:02:11

标签: perl glob

push @hex_locations, glob("$ptxdist_env->{root}/project/platform-$PLATFORM-$BUILD_SUBTYPE/build-target/gnss-*");
push @hex_locations, glob("$ptxdist_env->{root}/project/platform-$PLATFORM-$BUILD_SUBTYPE/build-target/gps-q6image-*");
push @hex_locations, glob("$ptxdist_env->{root}/project/platform-$PLATFORM-$BUILD_SUBTYPE/build-target/hexagon-infra-*");
push @hex_locations, glob("$ptxdist_env->{root}/project/platform-$PLATFORM-$BUILD_SUBTYPE/build-target/tfcs-*");

有没有更好的方法来做到这一点,比如用一行而不是4行?我不得不使用glob,因为它最后有通配符,当我尝试在单行中使用它时,glob抱怨太多的争论。

感谢。

2 个答案:

答案 0 :(得分:1)

以下是四个:

  1. my @hex_locations = (
        glob("$base/gnss-*"),
        glob("$base/gps-q6image-*"),
        glob("$base/hexagon-infra-*"),
        glob("$base/tfcs-*"),
    );
    
  2. my @hex_locations = glob(join(' ',
        "$base/gnss-*",
        "$base/gps-q6image-*",
        "$base/hexagon-infra-*",
        "$base/tfcs-*",
    ));
    
  3. my @hex_locations = map glob("$base/$_-*"),
       qw( gnss gps-q6image hexagon-infra tfcs );
    
  4. my @hex_locations = glob("$base/{gnss,gps-q6image,hexagon-infra,tfcs}-*");
    

答案 1 :(得分:0)

你可以尝试:

my @loc = ("gnss-*","gps-q6image-*","hexagon-infra-*","tfcs-*");

for (@loc) {
    push @hex_locations, glob("$ptxdist_env->{root}/project/platform-$PLATFORM-$BUILD_SUBTYPE/build-target/$_");
}