Perl warning: "Found = in conditional, should be ==", but there is no equal sign on the line

Doing the following in Perl v5.12.3 on macOS 10.7.2:

#!/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 7 slots, weekdays have only 4 (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 the warning "Found = in conditional, there should be == on addtimes.pl line 16", but there is no equal sign on this line. In addition, the cycle starts with $d == 9 . What am I missing?

Line 16:

 if (($d+4) % 7 < 2 || ($t > 3)) { 

Thanks.

+4
source share
2 answers

The problem is in your elsif

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

Since the if run on line 16, and elsif is part of this statement, where the error came from. This is an unfortunate limitation of the Perl compiler.

In an unrelated note, it is much better to avoid C-style loops if you can:

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

Isn't it prettier? :)

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

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

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

Source: https://habr.com/ru/post/1379047/


All Articles