From: David Harmon on
On Sun, 21 Feb 2010 13:27:50 +0000 in comp.lang.perl.misc, Henry Law
<news(a)lawshouse.org> wrote,
>Ugly. So I have a list of those variable names, thus
> my @variable_name_list = ( '$message_id', '$from_name', ...

- Stop using eval for this.
- Put real references in your array.
- Read perldoc -q "How can I use a variable as a variable name"
From: J�rgen Exner on
Henry Law <news(a)lawshouse.org> wrote:
>I'm inserting new records into a MYSQL database using Perl DBI. There
>are fourteen 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
> my @variable_name_list = ( '$message_id', '$from_name', ...

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

my %myvars;

>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
From: Henry Law on
J�rgen Exner wrote:
> Whenever you feel the urge to use a string as a variable name you should
> seriously consider using a hash instead.

J�rgen, 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
From: Uri Guttman on
>>>>> "HL" == Henry Law <news(a)lawshouse.org> writes:

HL> J�rgen Exner wrote:
>> Whenever you feel the urge to use a string as a variable name you should
>> seriously consider using a hash instead.

HL> J�rgen, 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

--
Uri Guttman ------ uri(a)stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
From: krish on
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


On Feb 21, 6:27 pm, Henry Law <n...(a)lawshouse.org> wrote:
> (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 fourteen 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
>    my @variable_name_list = ( '$message_id', '$from_name', ...
> 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 fourteen statements and had it working by
> now, but it's a matter of elegance!
>
> --
>
> Henry Law            Manchester, England