Perl Script Problem.

Hi,
 The below Perl Script reads directories and sub directories recursively and process the image files in them by calling another application.

The problem is I am calling this Perl Script from another Perl script. It is being invoked and it is processing all the images but after completion of all the image processing  it is not exiting the script.

I mean it is looping again and processing the images from first rather than calling the next script in line.


#!/usr/local/bin/perl-w

use File::Find;
use XML::Simple;
use File::Basename;

# Get the directory from the command line or use the default directory

$search = shift || '/usr/qa/files' ;


#!/usr/local/bin/perl-w

use File::Find;
use XML::Simple;
use File::Basename;

# Get the directory from the command line or use the default directory

$search = shift || '/usr/qa/files' ;

# Get an array of all subdirectories

find sub { push @dirs, $File::Find::name if -d }, $search ;

for $dir ( @dirs ) {

   opendir $dh, $dir or do { warn "Cannot open '$dir' $!" ; next ; } ;

   opendir( DIR, "$dir" );

   @files = grep( /\.jpg$/, readdir( DIR ) ) ;

   closedir( DIR ) ;

(my $logdir = $dir)=~s|^\Q$search/\E||i;

   $logdir =~ s|/IMAGES$||i;

  $logdir = "/usr/qalogs/$logdir/FITS";


     mkdir $logdir;



   foreach $file ( @files ) {


      print "$dir/$file\n" ;


       $logfilename =$file;

       $logfilename =~s/\.jpg$/.xml/;


      @args = ("/usr/qa/fits-0.4.2/fits.sh", "-i", "$dir/$file","/usr/qa/fits-0.4.2/fits.sh", "-o", "$logdir/$logfilename");

         system( @args ) == 0 or die "system @args failed: $?";

   }
}




new_perl_userAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

FishMongerCommented:
Your script has a lot of problems, most of which I don't have time right now to cover.

Start by removing the -w switch and add the following 2 pragmas, which should be in EVERY Perl script you write.

use strict;
use warnings;

Everything you're doing in those 2 foreach blocks should be incorporated into the sub that find uses.  Doing that means a major rework of those 2 parts to get rid of the unneeded parts.

I don't know what your fits.sh script does, but it's a very good bet that it would be better to handle its functionality directly in the Perl script.
TintinCommented:
Is that your complete script?

Why are you using XML::SImple?

Does it need to be a Perl script?
new_perl_userAuthor Commented:
Hi,
 
 This is my complete script . Sorry I was earlier using Simple::XML for some logic and I don't need that logic now and forgot to remove it from the script.

And fits is an application I need ti invoke to process that image files which does all the verification like whether it is an valid JPG image or not....

This script works fine if I call it alone , but if I bundle it with other perl scripts to call one by one  its looping and not coming out of the script.
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

wilcoxonCommented:
I think this should work...
wilcoxonCommented:
EE forgot to attach my code...

I think this should work...
#!/usr/local/bin/perl

# always use strict and warnings
use strict;
use warnings;
use File::Basename;
use File::Find;
use File::Path qw(make_path);

# Get the directory from the command line or use the default directory

my $search = shift || '/usr/qa/files';

# Get an array of all subdirectories

my @dirs;

find sub { push @dirs, $File::Find::name if -d }, $search;

foreach my $dir ( @dirs ) {

    opendir DIR, $dir or do { warn "Cannot open '$dir' $!"; next; };
    my @files = grep /\.jpg$/, readdir(DIR);
    closedir( DIR ) ;

    my $logdir = $dir;
    $logdir =~ s{^\Q$search/\E}{}i;
    $logdir =~ s{/IMAGES$}{}i;
    $logdir = "/usr/qalogs/$logdir/FITS";
    # use make_path to create the full path - always check if it exists first
    unless (-d $logdir) {
        make_path($logdir) or do { warn "could not create $logdir: $!"; next; };
    }

    foreach my $file ( @files ) {

        print "$dir/$file\n" ;
        my $logfilename =$file;
        $logfilename =~s/\.jpg$/.xml/;

        my @args = ("/usr/qa/fits-0.4.2/fits.sh", "-i", "$dir/$file","/usr/qa/fits-0.4.2/fits.sh", "-o", "$logdir/$logfilename");
        system( @args ) == 0 or die "system @args failed: $?";

   }
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TintinCommented:
Personally, I think a bash solution looks much better
#!/bin/bash

search=${1:-/usr/qa/files}

for file in $(find $search -type f -name "*.jpg")
do
  dir=$(dirname $file)
  dir=/usr/qalogs/${dir//IMAGES/}/FITS
  mkdir -p $dir
  filename=$(basename $file)
  /usr/qa/fits-0.4.2/fits.sh -i $file -o $dir/${filename/jpgs/xml}
done

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.