I try to open a list of files in a folder. Eventually I want to insert a HTML-Snippet in it. So far I can open the folder and read the list in an array. That´s fine. But when I try to open the files by using a variable as filename, I get an error message every time ("permission denied"). No matter if I use $_ or other variations of putting the values of list items in variables. I have found similar questions here, but didnt find the solution so far. Here is my code:
use strict;
use warnings;
my ($line);
opendir (FOLDER,"path/to/folder/./") or die "$!";
my @folderlist = readdir(FOLDER);
my @sorted_folderlist = sort @folderlist;
close(FOLDER);
foreach (@sorted_folderlist) {
my $filename = $_;
open (READ, ">", "path/to/folder/$filename") or die "$!";
# do something
close (READ);
}
What is the mistake here? And how would I open files with using a variable as filename?
Pjoern
Here is my changed code in order to answer 1:
my $dh;
opendir $dh, "dir/./" or die ...
my @folderlist = grep { -f "dir/$_" } readdir $dh;
close $dh;
my @sorted_folderlist = sort @folderlist;
foreach my $filename (@sorted_folderlist) {
open my $fh, "<", "dir/$filename" or die ...
open my $writeto, ">", "new/$filename" or die ...
print $writeto "$fh";
close $fh;
close $writeto;
}
print $writeto "$fh"is very strange :-)print $writeto "$fh";. That prints to$writetoalright, but what it prints is the filehandle$fhitself (evaluating it under""is not needed and doesn't change anything.); so it print something likeGLOB(0x1820a68). Instead, read from that input, process the line, and print that to outputwhile(my $line = <$fh>) { process-$line; print $writeto $line; }