Andreas Bizzozero
Andreas Bizzozero

Reputation: 45

Perl - Print output to files in a different directory

Good morning!

I am sorry if this has been posted before - I could not find it. I just need a little point into the right direction, this is my homework and I feel it is almost done. What I want to do is take data from files in a different folder from where the script is run, process the data inside Perl, then print the output to another directory. Now I got the two parts done, but what I fail with is that Perl does not find the path to where I want to save the files. It just sais "No file or directory with that names exists", but it does! Here is the part of the script for that:

my @files = <$ENV{HOME}/Docs/unprocessed/*.txt>;
my $path = "$ENV{HOME}/Docs/results";

<looping through @files, processing each file in the unprocessed folder...>

open (OUTFILE, $path . '>$file') or die $!;
print OUTFILE ""; # "" Is really the finished calculations from the loop, not important here.
close FILE;
close OUTFILE;

I bet its something stupid...

Upvotes: 2

Views: 13785

Answers (1)

cdhowie
cdhowie

Reputation: 169018

Because you are mixing the "write" token > in with the filename. This:

open (OUTFILE, $path . '>$file')

Should be:

open (OUTFILE, ">$path/$file")

You will also likely have to strip the .../Docs/unprocessed/ prefix from your filename:

use File::Basename;

open (OUTFILE, ">$path/" . basename($file))

Upvotes: 3

Related Questions