http://www.perlmonks.org?node_id=11107784

ovedpo15 has asked for the wisdom of the Perl Monks concerning the following question:

Hello dear monks,
I wrote the following code:
do { $content = make_get_request($url_reports); $tries--; } while ($content == "[]" && $tries > 0); print $content."\n"; print "check:".($content == "[]")."\n"; if ("$content" == "[]") { print "Failed to report.\n"; } else { print "Successfully reported.\n"; } sub make_get_request { # Builds cmd for checking in API return `$cmd`; }
When I run it (and the data got to the DB), I get:
[{"_id":"123125125","files":6,"user":"abcasda","timestamp":1571685134, +"runtime":3,"run_name":"checking"}] check:1 Failed to report. Successfully reported.
Why does it enter in the if-statement if $content != "[]?

Replies are listed 'Best First'.
Re: Equality of strings
by huck (Parson) on Oct 21, 2019 at 19:40 UTC

    == is numeric equals, you want eq instead

      Oops, such a novice mistake. Thank you :)
Re: Equality of strings
by Corion (Pope) on Oct 21, 2019 at 19:41 UTC

    == is the operator for numerical comparisons, just as !=. See perlop.

    You likely want eq or ne.

Re: Equality of strings
by stevieb (Canon) on Oct 21, 2019 at 19:48 UTC

    First, when describing a problem, be very sure you haven't made a typo:

    Why does it enter in the if-statement if $content != "[]?

    You're missing a closing double-quote character at the end. These things are important for those who may try to troubleshoot your issue. Be clear, concise and correct in what you're actually trying to sort out.

    Next, $content == "[]"... you are testing a variable against a string of characters. perl recognizes == as the numerical comparison operator. If you want to compare to a string (which you are clearly implying here), you need to use the string comparison operator, eq. So replace == (numerical comparison) to eq (string comparison) and you should progress further.

    See Equality Operators for details.

Re: Equality of strings
by 1nickt (Abbot) on Oct 22, 2019 at 00:29 UTC

    Didn't you get a warning from this code?

    $ perl -Mstrict -wE 'my $str = "[]"; say $str == "[]" ? 1 : 0' Argument "[]" isn't numeric in numeric eq (==) at -e line 1. Argument "[]" isn't numeric in numeric eq (==) at -e line 1. 1
    $ perl -Mstrict -wE 'my $str = "42"; say $str == "[]" ? 1 : 0' Argument "[]" isn't numeric in numeric eq (==) at -e line 1. 0

    You're not seriously still running your code without warnings enabled, are you? While simultaneously trying, based on your last few questions, to increase your test coverage numbers?!


    The way forward always starts with a minimal test.
      You're not seriously still running your code without warnings enabled, are you? While simultaneously trying, based on your last few questions, to increase your test coverage numbers?!

      This is precisely the sort of "technically correct" yet unnecessarily abrasive comment that drives people away from communities. You're a smart person; surely you can find more constructive ways to offer feedback than getting all hot and bothered because someone is making a few mistakes along road of learning a new skill (not to mention giving this community a continuing reason to exist!) I'm asking you, gently, and as a fellow member of this community, to please do better.

        You should do a little research before scolding.

        I and many other monks have been providing several hundred answers to this Seeker's 100+ posts for about a year and a half. S/he's been the beneficiary of scores if not hundreds of hours of mentoring by some of the most skilled and experienced, and generous, Perl programmers there are. We've provided many dozens of example scripts and snippets, all showing use strict; use warnings;, and many recommendations to read and reread the fundamental Perl documentation.

        ovedpo15 suffering from using == in place of eq for string comparison and not employing Perl's protection-from-silly-mistakes features including the warnings pragma, after all this time, is obstinate and willful programmer negligence, disrespectful of those who take time to assist, and deserving of clarion criticism.

        You're not doing this monk any favours by shielding him/her from criticism from the peer group s/he has chosen to join, when s/he insists on not following the most basic advice, even while asking a new and more advanced question every few days. You're just enabling bad habits.


        The way forward always starts with a minimal test.