Search code examples
perlglob

passing more arguments to glob function in perl


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-*");

is there a better way to do this, like giving it in one single line instead of 4? I had to use glob as it has wildcard at the end, when I try to use it in single line, the glob complains about too many argument.

thanks.


Solution

  • Here are four:

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