Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I am writing a script to open a file that contains a list of path to various files and open each file at the specified path. I have written a function that generates the file containing the path to the files. However, when I open that file to try to open the path of a file, my script is not picking up the entire path.

Example:

The file containing the paths is database.txt. Within database.txt are various paths such as C:\edman\file1.xml, c:\edman\file2.xml. note that each path is on a separte line in database.txt. When I open database.txt any try to read the path open file1 from the specified path, the $_ command only picks up c:\edman\fi thus causing the file not to be opened. Can any monk kindly help me solve this problem?. Pls see my code below.

$| = 1; sub GenerateKeywordsDatabase { print "\nDate: ".localtime(time)."\n"; $kwords = $path."keywords_database.txt"; $temp = $path."temp.txt"; if (opendir (KWORDS, $path) && open(LIST, ">$kwords")) { print LIST "\n================================================ +================\n"; print LIST "\nDATE: ".localtime(time)."\n"; print LIST "\nNAME: ".$ENV{USERNAME}."\n"; print LIST "\n================================================ +================\n\n"; @kwds = sort grep {$_ ne '.' and $_ ne '..'} readdir(KWORDS); foreach(@kwds) { if($_ eq "database.txt") { print ("I can process $_, without calling GenerateTest +File!!!\n"); } elsif($_ ne "database.txt") { GenerateTestFileList(); if (open(DBASE, $path."database.txt")) { while(<DBASE>) { chomp; open(TEMP, ">$temp") || die "cannot open [$tem +p], $!\n"; print TEMP "$_\n"; open(XFILE, $_) || die ("Cannot open [$_], $!\ +n"); while (<XFILE>) { chomp; #if ($_ =~ /\<name\>|\<\/name\>/) #{ print (LIST "$_\n"); #} } } } else { die ("File does not exist, $!\n") } } } } else { die ("Could not open $path, $!\n"); } } 1;

The GenerateTestFileList() function is used to generate the path to the various files I which to open. I have check the paths generated and the are all fine.

Thanks for your assistance.

Replies are listed 'Best First'.
Re: Getting path from file
by john_oshea (Priest) on Mar 16, 2006 at 18:34 UTC

    Your paths are being truncated and you say "The GenerateTestFileList() function is used to generate the path to the various files ..."

    I'd say we need to see the code for GenerateTestFileList()...

Re: Getting path from file
by smokemachine (Hermit) on Mar 16, 2006 at 14:52 UTC
    perl -e '$kwords="test"; $temp="/tmp/db.tmp";open LIST, ">$kwords"; while(<$path/*>){next if m#$path/database.txt$#;open DBASE,"$path/database.txt"; @dbase=<DBASE>; open TEMP, ">$temp" or die; $,=$/; print TEMP @dbase; foreach(@dbase){open XFILE, $_ or die; print LIST <XFILE>}}'