Uri Guttman <u...@stemsystems.com> writes:

> On 05/12/2016 08:04 PM, lee wrote:
>> ... I appreciate perl for:
>>
>>
>> $dbh->do("INSERT INTO $T_ENTRIES (" .
>>       join(', ', map($dbh->quote_identifier($_), $cgi->param)) . ') VALUES 
>> (' .
>>       join(', ', map($dbh->quote($_), map($cgi->param($_), $cgi->param))) . 
>> ')')
>>    if(scalar($cgi->param) == 111);
>>
> not bad but i have a few improvements that you may like.
>
> i would not call $cgi->param so often. easy enough to use arrays.
>
> my @cgi_params = $cgi->param() ;
> my @cgi_values = $cgi->param( @cgi_params ) ;

Oh, I didn't know you can do
'my @cgi_values = $cgi->param(@cgi_params);', thanks!

How does that play out?

my @cgi_values = $cgi->param($cgi->param);

And hence:

$dbh->do("INSERT INTO $T_ENTRIES (" .
         join(', ', map($dbh->quote_identifier($_), $cgi->param)) . ') VALUES 
(' .
         join(', ', map($dbh->quote($_), $cgi->param($cgi->param))) . ')')
   if(scalar($cgi->param) == 111);

?

You could make that:


if (scalar($cgi->param) == 111) {
  my $i = join(', ', map($dbh->quote_identifier($_), $cgi->param));
  my $v = join(', ', map($dbh->quote($_), $cgi->param($cgi->param)));

  $dbh->do("INSERT INTO $T_ENTRIES ($i) VALUES ($v)");
}


Would that work?

> i like to build up the sql parts outside of the call and to use ?
> placeholders which are quoted for you.
>
>     my $holders = join ',', ('?') x @cgi_params ;
>
> and i like here docs for sql so i can see the sql and not need all
> those quotes and noise. also assigning the sql  to a scalar so i can
> print it out for debugging
>
>     my $sql = <<SQL ;
> INSERT INTO $T_ENTRIES ( $holders ) VALUES ( $holders )
> SQL
>
> my $sth = $dbh->prepare( $sql ) ;
> $sth->execute( @cgi_params, @cgi_values ) ;

Don't you need (properly quoted) identifiers rather than place holders
where the identifiers go?


if (scalar($cgi->param) == 111) {
  my @cgi_params = $cgi->param();
  my @cgi_values = $cgi->param( @cgi_params );
  @cgi_params = map($dbh->quote_identifier($_), @cgi_params);

  unless (scalar(@cgi_params) != scalar(@cgi_values)) {
    my $identifiers = join(', ', @cgi_params);
    my $holders = join(', ', ('?') x scalar(@cgi_values));

    my $sql = <<SQL;
INSERT INTO $T_ENTRIES ( $identifiers ) VALUES ( $holders )
SQL

    my $sth = $dbh->prepare( $sql );
    $sth->execute(@cgi_values);
    $sth->finish();
  } else {
    print $cgi->p('ERROR: perhaps do something') . "/n";
  }
}


Which version is better for performance?

> it may look longer but it is easier to read, debug and reuse this
> way. it can be made into a sub with other options (selecting or where
> clauses, etc.).
>
> i didn't add in the if condition but that can be put in front of this code.

I guess I just happened to finally learn the 'map()' function and
immediately found it extremely useful and elegant :)

Are there other programming languages having the same, or an equivalent?

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to