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

My sub is sometimes not excuted. Sorry, I am new to perl, I have removed non significant code that... could be significant :
#!/usr/bin/perl # use strict; use CGI; use DBI; #-----------------------------Variables communes my %IndexClient; my @row; #.... #---------------------------------------- # FillListe( $Indx, $Ref, $Ville,$Tel,$TypeClient) my $dbgfill=0; sub FillListe { $dbgfill++; my $Indx = $IndexClient{$_[0]}; #... } #---------- Sortie des en tetes print "Content-type: text/html\n"; print "\n"; #... my $ligcnt; while ( @row = $cursor->fetchrow ) { FillListe($NumClient,$Ref,$row[2], $row[1],@row[3]); $NumeroClient[$IndexClient{$NumClient}]=-1; $ligcnt++; } $cursor->finish; print "<P> $ligcnt $dbgfill</P>";
My print sometimes work, othertime gives me : 187 0 as if I do not go inside FillListe Help, please Pierre Couderc

Replies are listed 'Best First'.
Re: sub not executed
by tall_man (Parson) on Feb 10, 2003 at 19:44 UTC
    It looks to me like FillListe will be called each time in the loop. You can confirm it by doing a Devel::TraceCalls like this:
    perl -d:TraceCalls=Subs,FillListe script.pl
    What seems more likely is that the contents of $ligcnt or $dbgfill are getting corrupted. It is dangerous to use the contents of @_ directly, because any change you make to them will directly change the parameter variables. You might use something like this instead:
    sub FillListe { my ($numclient, $ref, @params) = @_; $dbgfill++; ... }
    Another thing that could happen is that you might have let the original $dbgfill go out of scope and have created another one. FillListe will hold on to the reference to the original $dbgfill, no matter what (it acts like a closure on any scoped variables it uses).
Re: sub not executed
by dws (Chancellor) on Feb 10, 2003 at 20:18 UTC
    I have removed non significant code that... could be significant

    I'd bet that the root cause of the problem is code that you removed. In particular, in the query that you're constructing and executing. It's perfectly reasonable for an SQL query to execute without error, yet still return no rows. If that happens, your sub wouldn't be called.

    Start there. Put in enough debugging code to examine the query that you're producing, then try running that query by hand using whatever command-line client your database provides (e.g., "mysql" if you're using MySQL).

      Consider also enabling tracing on your DBI statement handles (perldoc DBI, look for the trace() method). Setting that to a value of 2 usually will produce a fairly detailed transcript of what's going to the database and what's coming back from it (which can help further localize where your problem is).

      Thank you. Anyway, this is not my problem as my variable $ligcnt shows that many records are fetched. Moreover many times, it works correctly and FillListe is called. It is only when you truly need the result that FillListe is not called (but I think this is Murphy's law).