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

Failing to warn about "Found = in conditional"

by Ineffectual (Scribe)
on Apr 17, 2012 at 01:31 UTC ( [id://965418]=perlquestion: print w/replies, xml ) Need Help??

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

Hello monks,

Is there a way to turn off some warnings? For some reason on a Mac perl 5.10.0 the following code will die with a : "Found = in conditional, should be == at line 21." However, on my linux computer with perl 5.12.3, it does not die or offer any warnings whatsoever. I'd like to figure out why this is happening and how to correct it on my linux box so that the warnings always happen.
use warnings FATAL => 'all'; use strict; my $VERSION = '0.01'; my $program = $0; my $system = `hostname`; my $count = 1; print STDERR "Version $VERSION of $program, running on $system\n"; test_subroutine(); exit; sub test_subroutine { my $subroutine = (caller(0))[3]; if ($count < 2 ) { print "one\n"; } elsif ($count = 2) { # ERROR HERE print "HA!\n"; } else { print "three!\n"; } }
If count is 1 then it prints one, but if it's anything over 1 then it prints HA!.
Update: I am indeed asking why perl is not warning me about the elsif error on my linux box, but is warning me on my Mac box.

Replies are listed 'Best First'.
Re: Failing to warn about "Found = in conditional"
by ww (Archbishop) on Apr 17, 2012 at 02:18 UTC

    Yes, you can "turn off some warnings" as you ask in your first sentence, but it appears that you really want your nix box to issue one. Why it fails to do so, I can't say, but the same behavior occurs with AS Perl 5.014 sub 2 on a Win 7 box.

    That seems odd, since the single equals sign is an assignment; the message you cite correctly suggests using == in the conditional. The rest is merely an expansion on dasgar's observation:

    FWIW, with alternate code,

    my @count = qw/1 2 3 4/; while ( my ( $iter, $value ) = each @count ){ if ($value < 2 ) { print "$iter: one\n"; } elsif ($value == 2) { # NO ERROR HERE, NOW say "$iter: HA!\n"; } else { print "$iter: three!\n"; } }

    one receives the expected values,

    0: one 1: HA! 2: three! 3: three! # This is a very special case where 4 == three :-)

    and restoring the single equals sign in the conditional

    my @count = qw/1 2 3 4/; while ( my ( $iter, $value ) = each @count ){ if ($value < 2 ) { print "$iter: one\n"; } elsif ($value = 2) { # LOGICAL ERROR HERE # assignment rather than test of equality # but no 'plaints from Perl print "$iter: HA!\n"; } else { print "$iter: three!\n"; } }

    ... also produces the expected (but incorrect, if I understand you correctly) sequence of output,

    0: one 1: HA! 2: HA! 3: HA!

    I can only guess the Perl version on your Mac is slightly better at noting the problem.

    Update: Corrected typos and bad markup... but also noted that you say the Mac version cites an error at line 36. It's line 21 in the code you posted... and even allowing for a hashbang and a blank line, that doesn't match well with "36." Is there something else in the code on either machine that you didn't show us that might explain the mystery?

      FWIW, on my Win 7 box AS Perl 5.8.9 build 827 and Strawberries 5.10.1.5, 5.12.3.0 and 5.14.2.1 all warn about assignment in a conditional.

Re: Failing to warn about "Found = in conditional"
by JavaFan (Canon) on Apr 17, 2012 at 11:16 UTC
    This doesn't seem a Mac vs Linux issue. I do get the warning on a Linux box using 5.10.0, but no warning using 5.10.1.

    I don't see this change documented in perl5101delta, nor in perl5120delta, so I can only assume this to be a bug. Specially since the warning is still issued if we replace the elsif with an if.

    I suggest you use perlbug to report this; it's unlikely to get fixed for the imminent 5.16 release, but it ought to be addressed in the 5.17 series leading up to 5.18.

      I don't see this change documented in perl5101delta, nor in perl5120delta,

      Didn't they make a change relatively recently that made the error reporting for if/elsif/else cascades more accurate? (Ie. attributing errors to the line number of the end of the nearest block rather than the end of the entire cascade.)

      Could that be the source of the change?


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

      Thanks for your input and understanding of the issue. I've used perlbug to report this.
        I haven't seen it on p5p yet. What's the bug ID?
Re: Failing to warn about "Found = in conditional"
by Anonymous Monk on Apr 17, 2012 at 03:56 UTC
    I have the same problem on Fedora. As a work-around, I used Cond::Expr. This test script correctly exposes the assignment.
    #!/usr/bin/perl use strict 'refs'; use warnings 'all'; use Cond::Expr; use Devel::SimpleTrace; my $answer = 1; my %args = ( this => 'that', (($answer = 1)) ? (answer => $answer) : ($answer) ? (wrong_answer => 1) : (no_answer => 1), );
Re: Failing to warn about "Found = in conditional"
by ikegami (Patriarch) on Apr 17, 2012 at 04:19 UTC

    If the warning isn't clear enough (even though it tells you exactly what to do in this case), you can use diagnostics to get more info.

    >perl -Mdiagnostics -e"my $x; if ($x = 2) {}" Found = in conditional, should be == at -e line 1 (#1) (W syntax) You said if ($foo = 123) when you meant if ($foo == 123) (or something like that).

    Ok, so it doesn't add much in this case.

Re: Failing to warn about "Found = in conditional"
by dasgar (Priest) on Apr 17, 2012 at 01:53 UTC

    Perhaps it would be easier to fix your code?

    If you change:

    } elsif ($count = 2) { # ERROR HERE

    to be:

    } elsif ($count == 2) { # ERROR HERE

    That just might fix the issue, which is what the error message that you listed seems to be indicating.

    Of course, that's just a suggestion.

      I think the OP knows what the warning means, the question is why the warning is missing. He probably spent a fair amount of time finding this error and wants to prevent it from happening again.

      -- Time flies when you don't know what you're doing

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://965418]
Approved by ww
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2024-04-19 18:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found