Re: Stupidity on my side - readdir(), chdir() ..

Michael Ludwig <[email protected]> Mon, 28 Mar 2011 22:26:54 +0200
Newsgroups gmane.comp.lang.perl.active-perl
Message-ID <20110328202654.GB3792@wladimir>
Claus Kick schrieb am 28.03.2011 um 21:36 (+0200):
> Hello all,
> 
> I am writing a small tool just to update contents of a couple of
> directories. The directories are just stored in a flat text file like
> this:
> 
> d:\updates\Imports\MCM_Import_CSG\Config
> d:\updates\Imports\MCM_Import_CSV
> [...]
> f:\sap_import_prod\bin
> 
> I figured, I just leave windows notation and then switch to proper
> notation before doing anything.
> Well, I did not get very far.
> If I do the following

You should have the following at the beginning of your script:

use strict;
use warnings;

> use Data::Dumper;
> 
> my $list = "importe.properties";
> my @directories;
> 
> my $file_types = ("bat|cfg|cmd|properties|pl|xml|xsl|xslt");

This variable is never used.

> open (FILE, "<".$list) or die "cannot open file";
> @directories = <FILE>;
> close (FILE);

chomp @directories;

> my @files;

Delete the above.

> foreach my $dir (@directories)
> {
> 	@files = undef;

Replace the above with:

my @files;

> 	chomp;

You're chomping $_, but your data is in $dir. This is your bug. I'd say
it happened because you didn't chomp your data straight away, instead
leaving it in an unusable state requiring further action.

> 	$dir =~ s/\\/\//g;
> 	#if ($dir =~ /[A-Z]{1}:(.+)/)
> 	#{
> 	#	$dir = $1;
> 	#}
> 	print "DIR: ".$dir."\n";
> 	opendir(DIR, $dir) or die "cannot open directory: $dir - $!";
> 	@files = readdir(DIR);
> 	closedir(DIR);
> 	print Dumper \@files;
> }
> 
> the script dies at the die with  - No such file or directory at
> importe.pl line 50.

There should be the dir name (containing the newline) on the preceding
line.

-- 
Michael Ludwig
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs