pmarcoen:

The statement isn't necessarily wrong--perl does automatic conversions between numbers and strings, and strings that don't "look like a number" get converted to 0 when you treat them like a number. After all, in a data stream, there may be multiple different data types that you would want to handle differently without lots of explicit type conversions and such. (If you're a fan of explicit conversions, perhaps you should be a Java programmer? </joke>)

Here's a simple (useless!) example of treating different types of data differently:

$ cat 938362.pl #!/usr/bin/perl use strict; use warnings; use Scalar::Util qw(looks_like_number); my $t; while ($t = <DATA>) { $t=~s/\s+$//; print "\ninput = '$t'\n"; if (looks_like_number($t)) { print "looks numeric\n"; if ($t == 0) { print "Gotta have somethin' if ya wanna be with me\n"; } else { print "There are $t ways to leave your lover\n"; } } else { print "non-numeric\n"; if ($t eq "Godzilla") { print "Oh, no! There goes Tokyo, go go $t!\n"; } elsif ($t =~ /horse/i) { print "A horse is a horse of course, of course\n"; } else { print "insert song lyric here...\n"; } } } __DATA__ Godzilla 50 A horse of a different color 0 zebra $ perl 938362.pl input = 'Godzilla' non-numeric Oh, no! There goes Tokyo, go go Godzilla! input = '50' looks numeric There are 50 ways to leave your lover input = 'A horse of a different color' non-numeric A horse is a horse of course, of course input = '0' looks numeric Gotta have somethin' if ya wanna be with me input = 'zebra' non-numeric insert song lyric here...

Since using text strings where numbers are expected is a common logic error, the warnings package tells you when you try to convert a string that doesn't look like a number into a number.

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: Why is this evaluating to true? by roboticus
in thread Why is this evaluating to true? by pmarcoen

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.