my %hash = ( foo => 'bar', # Exists, Defined, Positive length, True. baz => '', # Defined, False, Zero-length, Exists. blink => undef, # Exists, Not Defined, Zero-length, False. # buzz => '' # Does not exist, Not defined (because commented out +). ); foreach my $key (qw(foo baz buzz blink)) { print "$key ", (exists $hash{$key} ? "exists.\n" : "does not exist +.\n"); print "$key is ", (defined $hash{$key} ? "defined.\n" : "not defin +ed.\n"); print "$key has a length of ", (defined $hash{$key} ? length($hash +{$key}) : "zero because it is not defined"), ".\n"; print "$key\'s value is ", (defined $hash{$key} ? $hash{$key} : 'u +ndefined'), ".\n"; print "$key\'s Boolean value is ", ($hash{$key} ? "true.\n" : "fal +se.\n"); print "\n"; }

That will produce the following output:

foo exists. foo is defined. foo has a length of 3. foo's value is bar. foo's Boolean value is true. baz exists. baz is defined. baz has a length of 0. baz's value is . baz's Boolean value is false. buzz does not exist. buzz is not defined. buzz has a length of zero because it is not defined. buzz's value is undefined. buzz's Boolean value is false. blink exists. blink is not defined. blink has a length of zero because it is not defined. blink's value is undefined. blink's Boolean value is false.

So, you can test exists to see if the key exists, you can test defined to see if it has a defined value, you can test length to see if it consists of an empty string, and you can test its Boolean value (even for undefined values) for truth and falsehood. Each test has its own unique perspective. Boolean tests, for example, will return false for both 0 and empty, and undefined.

If you know what you are looking for, Perl provides a way to look for it.


Dave


In reply to Re: Checking for blank string by davido
in thread Checking for blank string by Himaja

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.