>   I am assigning a variable dynamically and using it in a query to
> fetch values from my database. Here is the pseudo code...
> 
> ###########
> my $string ;
> my $q1 = "select * from bugs where target_milestone = ? and date
> between ? and ?;
> my $q2 = "select * from bugs where $string and target_milestone = ?
> and date between ? and ?;
> 
> foreach my $q ($q1,$q2){
>   $stmt = $dbh->prepare($q1) or die ""; #####$dbh already has a valid
> handle
>   $stmt->execute()  or die "";
> 
>   $string = "bug_id not in (1,2,3)"; ####  1,2,3  are eg. values which
> i get after process $q1 and i #use it in $q2
> }
> 
> ##################################
> 
> howver it happens that $string is not used in $q2 i.e. $q2 is executed
> without taking into account the newly assigned value to $string and
> instead using '';
> 
> Is it that perl compiles the statement statement beforehand and never
> really substitutes it when foreach loop advances to use $q2.

No it doesn't compile the statement. The catch is that string 
interpolation (the change from $varname to the value of the $varname 
in doublequoted strings) happens at the place you specify the 
doublequoted literal, not every time you used it:

#!perl
$x = 'original';
$string = "The x='$x'.";
$x = 'modified';
print $string;

If you want to change the value of the $q2 after it was assigned you 
have to put a mark there and replace that mark once you have the 
value you want to replace there. Something like


my $string ;
my $q1 = "select * from bugs where target_milestone = ? and date 
between ? and ?;
my $q2 = "select * from bugs where ### and target_milestone = ? and 
date between ? and ?;


foreach my $q_template ($q1,$q2){
  (my $q = $q_template) =~ s/###/$string/g;

  $stmt = $dbh->prepare($q1) or die ""; 
   #####$dbh already has a valid handle
  $stmt->execute()  or die "";

  $string = "bug_id not in (1,2,3)"; 
  ####  1,2,3  are eg. values which i get after process $q1 
  #     and i use it in $q2
}

HTH, Jenda

===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
        -- Terry Pratchett in Sourcery


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to