-1

How do I search for a specific jar file within a while loop? In the code below, $application, $location, $version are command line arguments

sub do_verify
{
    while($application eq 'abc')
    {
        my $dir = "/viewstore/ccabc/dst_${application}_${version}/abcportal/${location}/target";
        opendir(DIR, $dir) or die $!;
        my $jarfile;
        next unless ($jarfile =~ /\.jar$/);
        print "$jarfile\n";
        do_upload();
    }
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
mahesh
  • 41
  • 1
  • 5
  • Okay, and what is the "error" that you're getting? Which "specific jar file" are you looking for? What's wrong with the code you've written? – Borodin Jun 12 '17 at 10:02
  • Re "*$application, $location, $version are command line arguments*", Then wouldn't `while($application eq 'abc') { ... }` form an infinite loop? – ikegami Jun 12 '17 at 16:32

1 Answers1

0

The following code should work for you. Currently you are not iterating the files inside your specified directory.

sub do_verify {

    while($application eq 'abc') {

        my $dir = "/viewstore/ccabc/dst_${application}_${version}/abcportal/${location}/target";

        opendir(DIR, $dir) or die $!;

        my @jarfiles = grep { (/\.jar$/) && -f "$dir/$_" } readdir(DIR);

        foreach (@jarfiles) {

            my $jarfile = $_;

            print "$jarfile\n";

            do_upload();
        }

    }
}
Naveen P
  • 67
  • 6
  • thanks it worked can you please explain me or point me to a link where i can understand about this grep { (/.jar$/) && -f "$dir/$_" } morly – mahesh Jun 12 '17 at 09:16
  • From the list of files in a given directory, we are trying to extract valid .jar files. The first condition is a regex `/.jar$/` which restricts only the file names ending with extension .jar, the second condition `-f "$dir/$_"` checks if it is a valid file. For more information: https://perlmaven.com/filtering-values-with-perl-grep , https://stackoverflow.com/questions/2483875/how-does-perls-grep-function-work-with-a-regex – Naveen P Jun 12 '17 at 09:20