in reply to Processing all files in a directory

You have a couple of problems.

foreach my $name (readdir DIR) { next if /^\./;

You want next if $name =~ /^\./; instead. The match operator works on $_ by default.

sub process_file { my $file = @_;

That doesn't do what you expect; it assigns the number of elements in @_ to $file. You want either my ($file) = @_; or my $file = shift; or maybe even my $file = $_[0]; (though I don't recommend that last.)

open (FILE, $file) or die "Could not open file: $!";

Unless your CWD is $dir, you'll need to prepend it to your filename... otherwise it will try to open $file in the current directory.

Oh, you might very well want split ' ', $line instead. That's a special case. Use /\s+/ only if you want to generate a leading null field on lines that begin with space.

Addendum: There are very good (security) reasons to use the three argument form of open() or to, at least, explicitly specify the mode.

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re: Re: Processing all files in a directory
by billie_t (Sexton) on May 26, 2003 at 05:32 UTC
    Thanks sauoq, for all those points. Silly of me not to match to the correct variable. And I'm not speaking the perl lingo fluently enough yet to remember that dumping an array to a scalar just gives the number of items in the array!!

    Prepending the filename was what I needed as well - I assumed that something would "remember" from the opendir.

    I managed to fix the mis-positioning of the close file myself (I was only reading the first line of each file for a while there).

      Prepending the filename was what I needed as well - I assumed that something would "remember" from the opendir.

      Well, I can imagine people assume the results from readdir have the directory name prepended. But if you think so, you wouldn't use /^\./ to weed out filenames, would you? Because then you would either select all filenames in the directory, or none at all, depending whether the directory name starts with a dot or not.

      Abigail

        Heh, that would require thinking logically, now wouldn't it? Thanks for the clarification!