koolgirl has asked for the wisdom of the Perl Monks concerning the following question:
It's been quite a while since I've humiliated myself on SoPW, so I figured I'd better get to work. Basically what I'm trying to do is this; I've written a utility for myself (mainly for extra practice with Perl since I've just recently finished the llama book, but the need for the utility is there as well), that reads through the directory in which I hold all my programming files, matches against seven different regex's with different criteria, then creates an index file, with a list of links to all files matching on each seperate keyword.
Of course at first I received nothing but gads of error reports since creation (about 4 days ago), due to which I've written, re-written and written the code one more time. I've FINALLY got it free of all error reports, as a matter of fact it sends nothing at all back, just the next command prompt. However, to my amazement, the file still hadn't been created...? I've seen lots of crazy things during my ride on the llama, but never have I seen an open file statement that didn't send back an error report when prompted to do so, but didn't open the file either.
So, after that, I went in and started writing de-bug print statements, and then my mind was really blown to bits. Let me show you:
This is the code:#!usr/bin/perl use strict; use warnings; my $file; my $path = "/mnt/data/Programming/"; my $i = 0; my @regex = qw( file split opendir push sort sub % STDIN); my @lines; my $prev_line; my $curr_line; # This program looks through a directory, reads through each file con +tained # within, and sorts them based on several different pattern matching +criteria, # then sends links to each file containing a match for each keyword, +to an index # page. opendir (PRO, "/mnt/data/Programming") || die $!; foreach $file (readdir (PRO)) { open (IN, "$file") || die $!; while (<IN>) { #print $_; # de-bug print statement 1 push(@lines, $_); #print $lines[31];# de-bug print statement 2 if ($lines[$i] =~ /$regex[$i]/) { if ($i >= 1) { $prev_line = $lines[$i - 1]; } # end if $curr_line = $lines[$i]; open (OUT, ">/mnt/data/Programming/links") || die $!; print OUT " < a HREF=\"$path . $file\">$regex[$i]</a>"; print OUT $file . "\n"; print OUT $prev_line . "\n"; print OUT $curr_line . "\n"; $i++; close(OUT); } # end if } # end while close(IN); } # end foreach
This:
is what I get in the two de-bug print statements, you see within my code ( or something confusingly similar ). Now is there something obviously normal about this strange output that I in my primitive state of being haven't come upon just yet, or are there grimlins behind my command prompt?* zero or more /x ign. wh.space ?: * zero or more /x ign. wh.space ?: * zero or more /x ign. wh.space ?: * zero or more /x ign. wh.space ?: * zero or more /x ign. wh.space ?: * zero or more /x ign. wh.space ?: * zero or more /x ign. wh.space ?: * zero or more /x ign. wh.space ?: * zero or more /x ign. wh.space ?: * zero or more /x ign. wh.space ?: * zero or more /x ign. wh.space ?: * zero or more /x ign. wh.space ?: * zero or more /x ign. wh.space ?: * zero or more /x ign. wh.space ?: * zero or more /x ign. wh.space ?: * zero or more /x ign. wh.space ?: * zero or more /x ign. wh.space
Any help would be greatly appreciated and needed. As usual, I thank you very much for your attention and time, hopefully one day soon during my new ride on the big bad camel, I'll be answering questions instead of asking them ;)
koolgirlUPDATE:
Thank everyone so much for all the insightful replies. I did learn a few lessons, ( not a good idea to parse a directory which is also your working directory (however I did learn that Perl was bad a#$ enough to parse itself!!), that my structure needs a bit of cleaning up, because the loops, the method of storing the lines read and the open statements, all could have been structured much more efficiently ), and I am currently in the process of de-bugging it and re-structuring it, and I have of course downloaded all code examples given to help me along in this process. With my busy life, if I seem to take a while to update, de-bug, reply, etc, please forgive, it's not bad manners, just lack of time to do it. In closing, and this is the most important part of this update, a lot of the replies ( gwadej, ig, lsh, and roboticus ) really did help to correct my thinking about code ( not just this particular de-bug ), which is an awesome opportunity for my skills to advance, and I thank you dearly for that! :))
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: File not opening, but no error report
by gwadej (Chaplain) on Mar 25, 2009 at 20:37 UTC | |
by shmem (Chancellor) on Mar 25, 2009 at 21:20 UTC | |
|
Re: File not opening, but no error report
by zwon (Abbot) on Mar 25, 2009 at 20:25 UTC | |
|
Re: File not opening, but no error report
by runrig (Abbot) on Mar 25, 2009 at 20:32 UTC | |
|
Re: File not opening, but no error report
by roboticus (Chancellor) on Mar 26, 2009 at 13:01 UTC | |
|
Re: File not opening, but no error report
by ig (Vicar) on Mar 26, 2009 at 01:28 UTC | |
|
Re: File not opening, but no error report
by Ish (Acolyte) on Mar 26, 2009 at 02:39 UTC |