Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Posted Value "eq" not working

by packetstormer (Monk)
on Sep 12, 2011 at 20:14 UTC ( #925560=perlquestion: print w/replies, xml ) Need Help??
packetstormer has asked for the wisdom of the Perl Monks concerning the following question:


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 = ''; 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=""> <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 (Pope) 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'$)/  );


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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://925560]
Approved by ww
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2017-08-22 22:36 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (344 votes). Check out past polls.