CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Open array of files



Feb 8, 2014, 7:54 AM

Post #1 of 3 (1645 views)
Open array of files Can't Post

Edit: I seem to have overlooked this;sb=post_latest_reply;so=ASC;forum_view=forum_view_collapsed;;page=unread#unread
that most likely will solve my problem.

Hey, I have a problem opening an array of files, I have tried in 3-4 different ways but nothing really works. So if anyone could tell me what I have done wrong I would appriciate it.

(I'm using padreperl if that matter)

#!/usr/bin/perl -w 
use strict;

my @matchlist;
my $filename ="";


sub checkdir{

#Set directory
my $directory = 'C:\Users\Kirkegaard\Desktop\Perl hattrick\Matches';

#open it, and read all files into matchlist
opendir (DIR, $directory) or die $!;
@matchlist = readdir(DIR);

#Open all the files and do stuff
foreach $filename (@matchlist) {
#chomp $filename; #not sure if I need this?

open(IN, '<', $filename) or die! "Could not read file\n";
print "Why does it not work?\n";
close IN;


(This post was edited by mk_bold on Feb 8, 2014, 8:20 AM)

Veteran / Moderator

Feb 8, 2014, 10:47 AM

Post #2 of 3 (1627 views)
Re: [mk_bold] Open array of files [In reply to] Can't Post

The readdir function returns a list of files (and/or directories) without the path. So, when you are trying open the files, you need to prepend the path ("C:\Users\Kirkegaard\Desktop\Perl hattrick\Matches") and a backslash to the file names. An additional consideration is that you should check for each entry in your list if it is a regular file (and not a directory), which can be done with the -f file test operator. So your foreach loop could start like this:

foreach my $filename (@matchlist) {  
chomp $filename;
my $full_filename = $directory . '\' . $filename;
next unless -f $full_filename; # skips entries that are not regular files
open my $IN, '<', $full_filename or die "Could not read file $full_filename $!";
# ...

Please also note that 'die!' is incorrect syntax.

Also note that I declared the $filename variable within the loop header; this is much better than a global variable because it limits the scope of the variable to the loop and makes sure that you don't change the content of your variable by accident (admittedly, this is not too likely to happen in a 15 lines script, but when you are going to write longer programs, you will understand that it is far better to declare variables within their smallest possible scope, as this helps preventing bugs that are often difficult to track).

I also used a lexical file handle ($IN) rather than a bare word file handler (IN), this is considered better practice.

Rather than using opendir and readdir, you might consider using the glob function, which returns a list of filenames with their path, so that you don't need to add yourself the directory to the filenames.

A final point is that calling a subroutine with the following syntax:


is still valid, but completely outdated (by about 20 years). You should rather use the more modern version:


Hope this helps.


Feb 9, 2014, 12:35 AM

Post #3 of 3 (1590 views)
Re: [Laurent_R] Open array of files [In reply to] Can't Post

Thank you for your comments!

I solved the find files with "glob" instead but the rest of your comments is a really nice help.


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives