I just wanted to follow-up a second time to point out something that I think is a lot more of a valuable lesson than any of the answers you've received, taken individually.

You've just witnessed a thought-process in action. Your script didn't work as you expected. We took a look at it and each came up with a different suggestion. But each was a suggestion that, applied in a more general sense, begins to answer the more important question of "how do I figure out what's going wrong, when something goes wrong?"

One answer is always check return values. Another answer is to develop your scripts under strictures (use strict;) and warnings (use warnings;). Another answer was to use CGI::Carp. Another answer (correctly) pointed you at the issue of opening a filehandle but reading from the <> diamond operator (sans named filehandle). And another was to look more closely at a means of simplifying the logic of your if-else statements.

Collectively these represent a process of programming in such a way that Perl tells you what's wrong, and in such a way that is less likely to develop hard to find errors. Many of the "why doesn't this work" questions posted here end up with a similar thought-process zeroing in on the problem. The implication is that many of the "why doesn't this work" questions a person has regarding his script, he can answer himself by applying the practices and thought processes that simplify debugging: Checking return values, simplifying logic, using strict, using warnings, using Carp (where applicable), using lexical filehandles (which are beneficial for many reasons not the least of which is the fact that a lexical filehandle must pass strictures), and so on. Tackle the bigger issue and the smaller ones fall into place.

This is just a thought that hopefully someone will find entertaining.... not quite worthy of being a meditation, but possibly worthy of mention. ;)


Dave


In reply to Re: Interesting CGI Problem... by davido
in thread Interesting CGI Problem... by Spidy

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.