Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Useless use of a constant in void context error 1

Status
Not open for further replies.

ssmith001

Programmer
Oct 6, 2005
40
US
I have the following code that I keep getting this error on: Useless use of a constant in void context at ...

I've highlighted the lines it has a problem with. What exactly does this mean and how do I get rid of the error/warning?

Code:
my @promocodes = qw/code1 code2 code3 code4 code5/;
my @bind_params;

if ( @promocodes )
{
 my $col_beg = "c.p_model";
 my $col_end = "ind";

 for (my $i = 1; $i < 21; $i++)
 {
  my $num = sprintf '%02s', $i;
  my $newcol = $col_beg . $num . $col_end;

  if ($i == 1)
  {
   [COLOR=red]$qry .= " AND \(\($newcol IN (" . join( ', ', ('?') x @promocodes ) . ')',"\n";[/color]
  }
  elsif ($i > 1 && ($i < 20))
  {
  [COLOR=red]$qry .= " OR $newcol IN (" . join( ', ', ('?') x @promocodes ) . ')',"\n";[/color]
  }
  else
  {
  [COLOR=red]$qry .= " OR $newcol IN (" . join( ', ', ('?') x @promocodes ) . ')))',"\n";[/color]
  }

  foreach my $element ( @promocodes )
  {
   push @bind_params, $element;
  }
 }
}

print $qry;
[/code/
 
whit is this supposed to do:

join( ', ', ('?') x @promocodes )

- Kevin, perl coder unexceptional!
 
It is supposed to build a SQL statement "where" clause that looks something like this:

AND ((c.p_model01ind IN (?, ?, ?, ?) OR c.p_model02ind IN (?, ?, ?, ?)...etc
 
Try something like this (check the code good for correct syntax in the return value of $qry):

Code:
my @promocodes = qw/code1 code2 code3 code4 code5/;
my @bind_params;
my $qry;
if ( @promocodes )
{
 my $col_beg = "c.p_model";
 my $col_end = "ind";
 my $place_holders = join( ', ', ('?') x @promocodes );
 for (my $i = 1; $i < 21; $i++)
 {
  my $num = sprintf '%02s', $i;
  my $newcol = $col_beg . $num . $col_end;

  if ($i == 1)
  {
   $qry .= " AND (($newcol IN $place_holders)\n";
  }
  elsif ($i > 1 && ($i < 20))
  {
  $qry .= " OR $newcol IN ($place_holders)\n";
  }
  else
  {
  $qry .= " OR $newcol IN ($place_holders)))\n";
  }

  foreach my $element ( @promocodes )
  {
   push @bind_params, $element;
  }
 }
}

print $qry;

I'm not really sure why perl throws that warning message out when you put the join() function in the code like you did.

- Kevin, perl coder unexceptional!
 
I know you have a solution, but just to explain the reason why you got that error so you don't make the same mistake again:
Code:
$qry .= " AND \(\($newcol IN (" . join( ', ', ('?') x @promocodes ) . ')', "\n";
In that code, you basically have 4 strings that you're trying to append to the end of $qry: the AND, the join, the closing bracket and the newline.

You're using the "dot" (concatenation) operator to join the first three together and to join them to $qry. But then you're using the comma operator to try to add the newline. This will actually create a two-element list, the first of which is appended to $qry. The second (the newline) doesn't have anywhere to go and so it's in a "void context", where it has no use, hence the warning. Here's a minimal case to illustrate:
Code:
#!/usr/bin/perl -w
use strict;

my $string = 1, 2;
print $string,"\n";
 
Thanks for the explanation. That makes sense and I don't think I would have ever understood why. Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top