You need to cope with scoping. There are variables $key and $value defined on lines 3 and 6 (outside the loop), and then you define another pair of them inside the loop (lines 12 and 15). Because you use my again, they are not reused, but created anew, masking earlier declaration. Had you used warnings and strict (which is recommended for all programs; use diagnostics will also help you), you would have received a message: "my" variable $key masks earlier declaration in same scope at ..., which means:

A "my", "our" or "state" variable has been redeclared in the current scope or statement, effectively eliminating all access to the previous instance. This is almost always a typographical error. Note that the earlier variable will still exist until the end of the scope or until all closure referents to it are destroyed.
(taken from perldiag). Because of this your while loop checks for upper-scope variables, which get assigned in the beginning of the program, but modifies other ones, which were created inside the loop.

Solution (simplified): use my only once per variable.


In reply to Re^3: Error with User defined data by aitap
in thread Error with User defined data by a.alandkar

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.