Using eval to iterate over a set of Perl variables 
Author Message
 Using eval to iterate over a set of Perl variables

(I'm sure Google should have been able to help me with this but
structuring a suitable query defeated me... a million hits or none.)

I'm inserting new records into a MYSQL database using Perl DBI.  There
are four{*filter*} fields, each one of which has its corresponding variable in
my program.  I want to execute the "bind_param" method for each one of
them.  If you're not familiar with DBI here's an example of what I might
code:
   $sth->bind_param( 1, $message_id );
   $sth->bind_param( 2, $from_name );
   ...
   $sth->bind_param( 14, $body );

Ugly.  So I have a list of those variable names, thus

and I have a loop with a counter, like this
   for ( my $i = 1; $i<=14; $i++ ) {
       eval{ $ins_sth->bind_param( $i, $variable_name_list[$i-1] ) };
   }

... but the database is filling up with fields containing the literal
name of my variables:

   mysql> SELECT message_id FROM mailitem;
   +-------------+
   | message_id  |
   +-------------+
   | $message_id |
   +-------------+

I know that it's to do with when the variable name "becomes" the
variable value, but I've tried multiple combinations of $, \$, and
nested 'eval' and can't hit it.  Can someone help me work out how to do
this?  Yes, I could have coded four{*filter*} statements and had it working by
now, but it's a matter of elegance!

--

Henry Law            Manchester, England



Thu, 09 Aug 2012 14:27:50 GMT  
 Using eval to iterate over a set of Perl variables

Quote:

> I'm inserting new records into a MYSQL database using Perl DBI.  There
> are four{*filter*} fields, each one of which has its corresponding variable in
> my program.  I want to execute the "bind_param" method for each one of
> them.  [...]

This is in the DBI documentation:

Heres a more fancy example that binds columns to the values inside a
hash (thanks to H.Merijn Brand):

              $sth->execute;
              my %row;

              while ($sth->fetch) {
                  print "$row{region}: $row{sales}\n";
              }

Let it inspire you to do your bind_params properly.

--
Ruud



Thu, 09 Aug 2012 15:08:30 GMT  
 Using eval to iterate over a set of Perl variables

Quote:


>> I'm inserting new records into a MYSQL database using Perl DBI.  There
>> are four{*filter*} fields, each one of which has its corresponding variable
>> in my program.  I want to execute the "bind_param" method for each one
>> of them.  [...]

> Let it inspire you to do your bind_params properly.

Inspired by the good doctor, to whom thanks, and also prompted by the
intellectual exercise of presenting my problem, I've solved it:

   for ( my $i = 1; $i<=$field_count; $i++ ) {
       $ins_sth->bind_param( $i, eval($variable_name_list[$i-1]));
   }

The "bind_param" method simply wants a value.

--

Henry Law            Manchester, England



Thu, 09 Aug 2012 15:13:16 GMT  
 Using eval to iterate over a set of Perl variables

Quote:



>>> I'm inserting new records into a MYSQL database using Perl DBI.  
>>> There are four{*filter*} fields, each one of which has its corresponding
>>> variable in my program.  I want to execute the "bind_param" method
>>> for each one of them.  [...]

>> Let it inspire you to do your bind_params properly.

> Inspired by the good doctor, to whom thanks, and also prompted by the
> intellectual exercise of presenting my problem, I've solved it:

>   for ( my $i = 1; $i<=$field_count; $i++ ) {
>       $ins_sth->bind_param( $i, eval($variable_name_list[$i-1]));
>   }

> The "bind_param" method simply wants a value.

Don't even consider eval. Best use a hash.

I would use the actual column names as keys, or use an id2name hash if
needed.

You won't need it anymore in a proper solution, but remember that you
can write
   for ( my $i = 1; $i<=$field_count; $i++ )
as
   for my $i ( 1 .. $field_count )

--
Ruud



Thu, 09 Aug 2012 16:04:38 GMT  
 Using eval to iterate over a set of Perl variables

Quote:

> I'm inserting new records into a MYSQL database using Perl DBI.  There
> are four{*filter*} fields, each one of which has its corresponding variable in
> my program.  I want to execute the "bind_param" method for each one of
> them.

> and I have a loop with a counter, like this
>    for ( my $i = 1; $i<=14; $i++ ) {
>        eval{ $ins_sth->bind_param( $i, $variable_name_list[$i-1] ) };
>    }

You have disguised symrefs as an eval.

Let's try and avoid that...

Quote:
> ... but the database is filling up with fields containing the literal
> name of my variables:

>    mysql> SELECT message_id FROM mailitem;
>    +-------------+
>    | message_id  |
>    +-------------+
>    | $message_id |
>    +-------------+

> I know that it's to do with when the variable name "becomes" the
> variable value, but I've tried multiple combinations of $, \$, and
> nested 'eval' and can't hit it.  Can someone help me work out how to do
> this?

# untested
my $p_num=1;
foreach my $var ( $message_id, $from_name ) {
    $ins_sth->bind_param( $p_num++, $var);

Quote:
}

Look Ma! No eval!

:-)

--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"



Thu, 09 Aug 2012 16:21:17 GMT  
 Using eval to iterate over a set of Perl variables
On Sun, 21 Feb 2010 13:27:50 +0000 in comp.lang.perl.misc, Henry Law

Quote:
>Ugly.  So I have a list of those variable names, thus


 - Stop using eval for this.
 - Put real references in your array.
 - Read perldoc -q "How can I use a variable as a variable name"


Thu, 09 Aug 2012 16:59:30 GMT  
 Using eval to iterate over a set of Perl variables

Quote:

>I'm inserting new records into a MYSQL database using Perl DBI.  There
>are four{*filter*} fields, each one of which has its corresponding variable in
>my program.  I want to execute the "bind_param" method for each one of
>them.  If you're not familiar with DBI here's an example of what I might
>code:
>   $sth->bind_param( 1, $message_id );
>   $sth->bind_param( 2, $from_name );
>   ...
>   $sth->bind_param( 14, $body );

>Ugly.  So I have a list of those variable names, thus


Whenever you feel the urge to use a string as a variable name you should
seriously consider using a hash instead.

        my %myvars;

Quote:
>and I have a loop with a counter, like this
>   for ( my $i = 1; $i<=14; $i++ ) {
>       eval{ $ins_sth->bind_param( $i, $variable_name_list[$i-1] ) };

        $ins_sth->bind_param( $i, $myvars{$variable_name_list[$i-1] })

jue



Thu, 09 Aug 2012 18:29:45 GMT  
 Using eval to iterate over a set of Perl variables

Quote:

> Whenever you feel the urge to use a string as a variable name you should
> seriously consider using a hash instead.

Jrgen, Tad and the Doctor all on my case ... I stand corrected and will
re-write it at once.  Thank you all.

--

Henry Law            Manchester, England



Thu, 09 Aug 2012 18:41:32 GMT  
 Using eval to iterate over a set of Perl variables


  >> Whenever you feel the urge to use a string as a variable name you should
  >> seriously consider using a hash instead.

  HL> Jrgen, Tad and the Doctor all on my case ... I stand corrected and
  HL> will re-write it at once.  Thank you all.

add me to that list. using a hash is always better than symrefs. my rule
is only use symrefs when you want to mung the symbol table. never use it
for data structures. a hash is meant for data and is actually faster
than symrefs as it doesn't do all the symbol table stuff that symrefs
need.

uri

--

-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------



Fri, 10 Aug 2012 04:45:45 GMT  
 Using eval to iterate over a set of Perl variables
Hi Henry,

   Due to the use of single quotes '', the perl variables don't get
the values of the variables. Example:

my $string = "Hello";

print '$string'; # this would print $string on the console/tty.
print "$string"; #this would print Hello on the console/tty.

Is that what you are looking at? Let me know if I mis-interpreted the
question.

--
Krishna


Quote:
> (I'm sure Google should have been able to help me with this but
> structuring a suitable query defeated me... a million hits or none.)

> I'm inserting new records into a MYSQL database using Perl DBI. ?There
> are four{*filter*} fields, each one of which has its corresponding variable in
> my program. ?I want to execute the "bind_param" method for each one of
> them. ?If you're not familiar with DBI here's an example of what I might
> code:
> ? ?$sth->bind_param( 1, $message_id );
> ? ?$sth->bind_param( 2, $from_name );
> ? ?...
> ? ?$sth->bind_param( 14, $body );

> Ugly. ?So I have a list of those variable names, thus

> and I have a loop with a counter, like this
> ? ?for ( my $i = 1; $i<=14; $i++ ) {
> ? ? ? ?eval{ $ins_sth->bind_param( $i, $variable_name_list[$i-1] ) };
> ? ?}

> ... but the database is filling up with fields containing the literal
> name of my variables:

> ? ?mysql> SELECT message_id FROM mailitem;
> ? ?+-------------+
> ? ?| message_id ?|
> ? ?+-------------+
> ? ?| $message_id |
> ? ?+-------------+

> I know that it's to do with when the variable name "becomes" the
> variable value, but I've tried multiple combinations of $, \$, and
> nested 'eval' and can't hit it. ?Can someone help me work out how to do
> this? ?Yes, I could have coded four{*filter*} statements and had it working by
> now, but it's a matter of elegance!

> --

> Henry Law ? ? ? ? ? ?Manchester, England



Fri, 10 Aug 2012 19:42:51 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Perl not setting pattern match variables when in an eval

2. Using Perl to set Unix environmental variables

3. Test for eval() without using eval() ??

4. set up hash using variables not yet defined

5. why does foreach iterate on an undef variable?

6. Iterating through multi-demensional hash using references

7. Patchlevel 33: eval doesn't set $@ for syntax errors

8. use of eval and strict does not correctly set $@

9. Perl setting parents environmant variables

10. PERL script for set DISPLAY variable ?

11. Setting perl variables from C

12. Why can't I set env variable in Perl (NT)

 

 
Powered by phpBB® Forum Software