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

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

Hello

This is a simple problem and I am sure it is staring me in the face. The page below loads and reads an XML value by get. The value will always be either "true" of "false". If I print the value to screen I can see clearly that it only every equals one of those 2 values, however, my if clause isn't working? That is, even though when $get is equal to false the text doesn't print.
#!/usr/bin/perl use strict; use LWP::Simple; use XML::Simple; use CGI; print "Content-type: text/html\n\n"; my $q = CGI->new; my $post = 'http://192.168.1.45/web/powerstate'; my $power = $q->param('power'); if($power eq "toggle") { get("$post"."?newstate=0"); } print "<br />"; my $get = get($post); chomp $get; # THIS PART IS FAILING if($get eq "false") { print "Box is currently on"; } print $get; print <<HTML; <form method="post" action="power.pl"> <input type="hidden" name="power" value="toggle"> <input type="submit" value="Change Power State"> </form> </body> HTML

Replies are listed 'Best First'.
Re: Posted Value "ne" not working
by Corion (Patriarch) on Sep 12, 2011 at 20:17 UTC

    So, who do you trust more, Perl or yourself?

    Have you looked at what $get contains? Maybe it contains whitespace at the end, maybe something is different from what you expect.

    Also, is the CGI surrounding your code necessary to reproduce the problem? Can you run the program without a webserver in between and still reproduce the failure?

Re: Posted Value "eq" not working
by Caio (Acolyte) on Sep 12, 2011 at 21:06 UTC
    I might not be half as knowledgeable as some people here. but I'd sugest to change the:

    eq "something"
    into a
    =~ m/something/i

    so you'd still look for the desired value, independing of case sensitivity or white spaces as Corion mentioned above. Altough I don't know if that'd take longer to run or not.
Re: Posted Value "eq" not working
by onelesd (Pilgrim) on Sep 12, 2011 at 23:35 UTC
    Doing something like this will help you debug.
    # THIS PART IS FAILING if($get eq "false") { print "Box is currently on"; } else { print "=>$get<" ; }

      perhaps you are expecting the string f+a+l+s+e where you should have 0 or ''

      maybe something like this...

      if(/(^[Ff]alse$|^0$|^''$|^'0'$)/  );

      or

      unless($get){print ...}
the Perl debugger is your friend...
by shriken (Priest) on Sep 13, 2011 at 14:33 UTC
    $ perl -d yourProgram.pl