Sharmarke Aden wrote:
> 
> I'm new to Perl and trying to access several directories at the same time
> and simply read the content of the directory and do some processing on the
> files in those directories. The code below is a reference code to get
> started on the real work. The code below however crashes and I'm lost as to
> what the cause is. Any help you could give me I'd appreciate it. This code
> simple in that it looks for 3 letter directories and creates a stats file
> used to report the files in that directory. Eventually what I'll want to do
> is run those files through processing and take advantage of a bitching 8
> processor machine we just got. BTW I'm using Perl 5.6 on win 2000 machine
> (ActivePerl-5.6.0.623-MSWin32-x86-multi-thread to be exact).

I don't use Windows, but from what I understand, the threading in Perl 5.8 is a
lot better than that in 5.6


> use threads;

You should use the warnings and strict pragmas while developing your program.

use warnings;
use strict;


> $currDir = `cd`;
> chop($currDir);

You should use the Cwd module for this.

use Cwd;

my $currDir = cwd;


> opendir(IP_MAIN_DIR, $currDir);

You should ALWAYS verify that opendir worked correctly.

opendir my $ip_main_dir, $currDir or die "Cannot open $currDir: $!";


> #remove '.' and '..'
> 
> @allBatchFolders = grep !/^\.\.?$/, readdir IP_MAIN_DIR;
> #get the names of all the 3 character directories in to a list/array ("aaa"
> and "aab, and "aac" qualify)
> 
> @allBatchFolders = grep /^[a-z][a-z][a-z]$/, @allBatchFolders;

You can skip the "#remove '.' and '..'" step because filenames with only three
letters won't have any dots in them.

my @allBatchFolders = grep /^[a-z]{3}$/, readdir $ip_main_dir;


> #open stat file for writing
> open(STATFILE, ">$currDir\\stat.txt");

You should ALWAYS verify that open worked correctly.  There is no need to use
backslashes in path names as slashes will work just fine.

open my $statfile, '>', "$currDir/stat.txt" or die "Cannot open $currDir/stat.txt: $!;


> $index = 0;
> 
> foreach $aDir (@allBatchFolders)
> {
>   $aThread[$index] = threads->new(\&writeStat, $aDir);
>  @ReturnData = $aThread[$index]->join();
>  $index++;
> }

my @aThread;
foreach my $aDir ( @allBatchFolders )
{
  push @aThread, threads->new( \&writeStat, $aDir );
  my @ReturnData = $aThread[ -1 ]->join();
}


> closedir(IP_MAIN_DIR);
> close(STATFILE);
> 
> sub writeStat
> {
>   $localDir = $currDir . "\\" . $_[0];

    my $localDir = "$currDir/$_[0]";


>   local *DIRECTORY;
>   opendir(DIRECTORY, $localDir);

You should ALWAYS verify that opendir worked correctly.

    opendir my $directory, $localDir or die "Cannot open $localDir: $!";


>   print "Start wile loop: @_[0]\n";
                            ^^^^^
>   print STATFILE "Start wile loop: @_[0]\n";
                                     ^^^^^
You are using an array slice when you should be using a scalar.  If you had warnings
enabled this would have produced a warning message.

    print "Start while loop: $_[0]\n";
    print STATFILE "Start while loop: $_[0]\n";


>   rewinddir( DIRECTORY );
>   while ($file = readdir(DIRECTORY))
>   {
>     writeToFile(@_[0], $file);
                  ^^^^^
      writeToFile( $_[0], $file );

>   }
>   print STATFILE "End wile loop: @_[0]\n";
                                   ^^^^^
    print STATFILE "End while loop: $_[0]\n";


>   print "End wile loop: @_[0]\n";
                          ^^^^^
    print "End while loop: $_[0]\n";


>   closedir(DIRECTORY);
> }
> 
> sub writeToFile
> {
>     use attrs qw(locked);
>     print STATFILE "Thread @_[0]: @_[1]\n";
                             ^^^^^  ^^^^^
      print STATFILE "Thread $_[0]: $_[1]\n";

> }



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to