Hello Monks, :))

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:

* 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
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?

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 ;)

koolgirl

UPDATE:

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! :))


In reply to File not opening, but no error report by koolgirl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.