Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Checking files in a directory

by Aim9b (Monk)
on Apr 20, 2011 at 01:22 UTC ( [id://900265]=perlquestion: print w/replies, xml ) Need Help??

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

Requesting a little Monk Mercy here... I have a program that loads all matching file names into an array, then searches each one for a string. It runs great, but it uses 6-8 parameters. I wanted to learn Hashes so I thought this would be a good use of one. I read in an ini file in the abc=123 format and load the keys and values from there. I always display a prompt "Are these parameters correct (Y/N)" So getting that far and seeing the same parms that I used to enter on the command line, I think I'm home free. No such luck. I fall down into my existing logic, and now I'm not getting any matches on my files. Can anyone see what I'm doing wrong? Supposedly this portion of the code didn't change. I did modify it a few times to test, but I always put it back. Things like the ^ in the pattern to get all files that BEGIN with the mask, or a wildcard * in the mask, versus just the 5 characters, etc.
# Open up directory for reading the filenames if ( ! -d $directory ) { print "'$directory' is not found\n"; exit 1; + } chdir $directory; opendir DIR, "." || die "Cannot open directory $directory\n"; # Look for '$filename' files... while ( $file = readdir DIR ) { if ( $file =~ /^$filemask/i ) { chomp; push @chk_files, $file; print "\n\t$file "; if ($Mode =~ /D/) {chomp ($dummy = <STDIN>);} # End of Mode } # End of IF } # End of While closedir DIR;
When I look in he debugger, @chk_files is empty, yet a display line shows that I'm in the correct dir and the files are passed one by one. Thanks for the help.

Replies are listed 'Best First'.
Re: Checking files in a directory
by wind (Priest) on Apr 20, 2011 at 02:22 UTC

    Instead of testing for the directory existance, just add error checking to your chdir.

    Additionally, your opendir error check will never be utilized because you used || instead of or.

    I'd also recommend that you use a lexical directory handle instead of a glob.

    chdir $directory or die "Can't cd to $directory: $!"; opendir my $dh, '.' or die "Can't open $directory: $!"; while (my $file = readdir $dh) { print "$file\n"; } closedir $dh;

    Within your loop, are you trying to match files that begin with the literal $filemask? If so, then you'll want to escape that using quotemeta or \Q...\E.

    And as ww pointed out, I don't think you need the bareword chomp.

    Don't know what $Mode is about...

    if ( $file =~ /^\Q$filemask\E/i ) { push @chk_files, $file; print "\n\t$file "; } # End of IF

    And finally, be sure to have use strict and use warnings turned on.

Re: Checking files in a directory
by ww (Archbishop) on Apr 20, 2011 at 01:44 UTC

    From the top of my head (since I can't see the code you're using to create the array of file_names_to_match -- which may be at the root of your problem):

    1. you have a chomp at line 9 that should probably be exercised whilst creating the file_names array.
    2. Have you checked the content of $filemask with a debugging print when first available? Also, is $filemask created using qr?

    And, OT, unsolicited observation: your line 1 above has at least one problem: the single quotes around $directory in your homebrew error message will prevent Perl from interpolating the var. You may wish to use a more canonical form of the (wise!) attempt to check your conditional:

    if ( ! -d $directory ) { die "No such \$directory, $directory: $!"; }
      ... problem: the single quotes around  $directory in your homebrew error message will prevent Perl from interpolating the var.

      Not so. The scalar is interpolated within double-quotes; the single-quotes in the same string have no effect on interpolation.

      >perl -wMstrict -le "my $string = 'hi there'; my $strung = qq{'$string'}; print $strung; " 'hi there'
Re: Checking files in a directory
by Aim9b (Monk) on Apr 20, 2011 at 10:36 UTC
    Thank you all so much. I will make these changes. I am just getting back into perl after a few years off, and was only new even then, so some of your questions and suggestions are a little over my head, but I have books and a thirst for knowledge, so I'll keep working on it. The ironic part is the old pprogram still works. I just added an ini file read upstream of this logic. I'll will undoubtedly be back. Thanks again, so glad the monastery is here.
      Well I added in the changes, and I can see some improvements, but the array was still empty. So determining the INI file was the issue, I dumped it in hex to find every line including my file mask parameter had trailing blanks. So I would never get a match. It's running fine now. I sent the program to a couple of people this morning, and you've made a perl newbie look like a rock star. Thank you all so much for your help.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://900265]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-19 22:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found