If you are confused about what's in a variable at a certain point, the best solution is to run the script in the debugger, and examine the variables.

perl -d myscript.pl

while start Perl, load and compile the script, and wait for you to type a command. Try h as your first command, it will print a summary of available commands. l to list sections of your script; n to execute a line of code; s to step into a subroutine; r to continue until the current subroutine returns; x to display the value of a variable ... note that you need to backslash true arrays and hashes, to examine a reference to it, to properly see what's happening:

DB<1> $h1; DB<2> $h2 = {} DB<3> $h3 = {has => 'value'} DB<4> x $h1 0 undef DB<5> x $h2 0 HASH(0x8406f78) empty hash DB<6> x $h3 0 HASH(0x8406d08) 'has' => 'value'

h1 is a scalar with no value; h2 is a scalar storing a reference to a hash, but the hash contains no data; h3 is a scalar containing a reference to a hash which has a tag/value pair ... but note that both key and value can be true or false in a boolean sense, and they can both even be undef.

As Occam said: Entia non sunt multiplicanda praeter necessitatem.


In reply to Re: Check for existance & definedness for Hash and Array references by TomDLux
in thread Check for existance & definedness for Hash and Array references by kalyanrajsista

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.