Re: Cannot locate dynamically declared array



minjie@xxxxxxxxxx wrote:
....
I'm trying to do the following:  to retrive all table names from a
database and display them on a web application, and when a user selects
one table, display all its fields and the corresponding values on the
web. I'm doing it in Perl (ver 5.8.0). I can get the table names and
field names from the database alright. But when I dynamically composed
the field names as elements of an array, such as the following:

my $var_declaration = "my @" . $tablename . " = (";
foreach $fieldname (@fieldnames)
{
    $var_declaration .= "'" . $fieldname . "', ";
}
..... # some processing to get rid of the last comma and add the
enclosing ")"

It is not necessary to get rid of the last comma in Perl. A construction like: @a=(1,2,3,); is fine.



and then I do the following: eval $var_declaration; print $var_declaration;

the print statement did print the array as follows:
my @USER = ('NAME', 'ADDRESS1', 'ADDRESS2', 'CITY', 'STATE',
'ZIPCODE');

I thought the eval will actually declare @USER array. But when I
actually tried to use @USER in the following code, Perl found no
elements at all. I did a size on @USER and I got a 1(one) in return.
Does that mean eval cannot be used to dynamically declare an array? Is
there another way to do it, or is it not doable at all?

The problem is that you are creating a lexical variable in an eval(). The lexical variable thus created goes out of scope as soon as eval() returns, and is destroyed. You could instead create a package global variable my simply removing the "my" (and also the use strict;). But you would be far better off to dispense with the idea of storing this stuff in variables named by the tables of your database, and simply store it in a hash (a hash of arrays in this case). That's what hashes were made for. If you called the hash "tables", then something on the order of:


use Data::Dumper;
use warnings;
use strict;
my @fieldnames=('f1','f2','f3');
chomp @fieldnames;
my $tablename='table1';
my %tables;
for my $fieldname(@fieldnames){
   push @{$tables{$tablename}},$fieldname;
}
print Dumper(\%tables);

should work nicely. And without all the nastiness of building strings containing Perl code, along with the worries about such things as: What if a table name is the same as another variable in the program? What if a table name contains a character that is not legal in a Perl variable name? What if the table name were set by a hacker to something like ';rm -rf ?

....
--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl
.



Relevant Pages

  • Re: MFC MDI for binary db
    ... i think the 'right' method to do this is to have an array of views. ... be used to display different sections of the same document in copies of the ... > structure represent one record of a database. ... > selected record. ...
    (microsoft.public.vc.mfc)
  • Re: INFORMIX 4GL MASTER DETAIL
    ... Are you using a DISPLAY ARRAY or an INPUT ARRAY? ... up the data for the database and contact names, ... SCROLL UP or SCROLL DOWN to navigate them. ...
    (comp.databases.informix)
  • Re: splitting / unpacking line into array
    ... Using your array to unpack into may ... > I'm looking a Perl solutions right now mainly because I'm trying to learn ... I do not have any SQL database and am not really good ...
    (perl.beginners)
  • [SOLVED] Re: Specify an array in $sth->execute() ?
    ... In your case below you want to bind a single parameter to an array. ... I didn't developped in perl for a while... ... Using 'hash slice' syntax ... #SELECT FROM database 1 and INSERT INTO database 2... ...
    (perl.dbi.users)
  • Cannot locate dynamically declared array
    ... database and display them on a web application, ... field names from the database alright. ... the field names as elements of an array, ... Does that mean eval cannot be used to dynamically declare an array? ...
    (comp.lang.perl.misc)