This is a common issue that you should be handling pretty much as you have shown, depending on circumstances. Please see Truth and Falsehood | the first paragraph in Declarations (kinda) or better yet, What is true and false in Perl? (update: or the link (third paragraph) in Eily's post).

Update 1: "Truth and Falsehood" has absquatulated, so had to fix some links.

Update 2:

I find that when I have a string that is just "0" ...
...
I need to use
 if (defined($string)) ...
Just that case might arise if you're reading lines/records from a filehandle with readline($filehandle), e.g., if the last line of the file is "0" and is not newline-terminated (i.e., is just "0"/false and not "0\n"/true). That's why the Perl compiler will "optimise" (if that's the right term) a while-loop condition expression like
    while (my $line = <$fh>) { ... }
to
    while (defined(my $line = <$fh>)) { ... }
c:\@Work\Perl\monks>perl -wMstrict -MO=Deparse,-p -le "my $filename = 'foo'; open my $fh, '<', $filename or die qq{opening '$filename': $!}; ;; while (my $line = <$fh>) { print $line; } " BEGIN { $^W = 1; } BEGIN { $/ = "\n"; $\ = "\n"; } use strict 'refs'; (my $filename = 'foo'); (open(my $fh, '<', $filename) or die("opening '${filename}': $!")); while (defined((my $line = <$fh>))) { do { print($line) }; } -e syntax OK
See O and B::Deparse. Compiled under Perl version 5.8.9.


Give a man a fish:  <%-{-{-{-<


In reply to Re: methods for dealing with zero '0' as a string or char (updated) by AnomalousMonk
in thread methods for dealing with zero '0' as a string or char by boleary

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.