Link to home
Create AccountLog in
Avatar of new_perl_user
new_perl_user

asked on

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: $?";

   }
}




Avatar of FishMonger
FishMonger
Flag of United States of America image

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.
Avatar of Tintin
Tintin

Is that your complete script?

Why are you using XML::SImple?

Does it need to be a Perl script?
Avatar of new_perl_user

ASKER

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.
I think this should work...
ASKER CERTIFIED SOLUTION
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
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