Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl warning: "Found = in conditional, should be ==", but there's no equals sign on the line

Tags:

warnings

perl

I am running the following in Perl v5.12.3 on Mac OS X v10.7.2 (Lion):

#!/usr/local/bin/perl

use strict;
use warnings;
use DBI;

my $db = DBI->connect("dbi:SQLite:testdrive.db") or die "Cannot connect: $DBI::errstr";

my @times = ("13:00","14:30","16:00","17:30","19:00","20:30","22:00");

my $counter = 1;

for (my $d = 1; $d < 12; $d++) {
    for (my $t = 0; $t < 7; $t++) {
        # Weekend days have seven slots, weekdays
        # have only four (barring second friday)
        if (($d+4) % 7 < 2 || ($t > 3)) {
            $db->do("INSERT INTO tbl_timeslot VALUES ($counter, '$times[$t]', $d);");
            $counter++;
        # Add 4:00 slot for second Friday
        } elsif (($d = 9) && ($t = 3)) {
            $db->do("INSERT INTO tbl_timeslot VALUES ($counter, '$times[$t]', $d);");
            $counter++;
        }
    }
}

$db->disconnect;

I get a "Found = in conditional, should be == at addtimes.pl line 16" warning, but there's no equal sign on that line. Also, the loop seems to start at $d == 9. What am I missing?

Line 16:

if (($d+4) % 7 < 2 || ($t > 3)) {
like image 671
Ellipsoid Avatar asked Oct 31 '11 22:10

Ellipsoid


2 Answers

The problem is in your elsif

} elsif (($d = 9) && ($t = 3)) {
             ^-----------^--------- should be ==

Because the if statement started on line 16, and the elsif is part of that statement, that's where the error got reported from. This is an unfortunate limitation of the Perl compiler.

On an unrelated note, it's much nicer to avoid C-style loops when you can:

for my $d ( 1 .. 11 ) { 
    ...
    for my $t ( 0 .. 6 ) { 
        ...
    }
}

Isn't that prettier? :)

like image 135
friedo Avatar answered Nov 19 '22 14:11

friedo


} elsif (($d = 9) && ($t = 3)) {

This line will assign 9 to $d and 3 to $t. As the warning says, you probably want this instead:

} elsif (($d == 9) && ($t == 3)) {
like image 39
K-ballo Avatar answered Nov 19 '22 13:11

K-ballo