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" ...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
...
I need to use
if (defined($string)) ...
See O and B::Deparse. Compiled under Perl version 5.8.9.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
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |