Re: Cannot locate dynamically declared array
- From: Bob Walton <see.sig@xxxxxxxxxxxxxxxx>
- Date: Sun, 22 May 2005 03:18:27 GMT
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 .
- Follow-Ups:
- Re: Cannot locate dynamically declared array
- From: minjie
- Re: Cannot locate dynamically declared array
- References:
- Cannot locate dynamically declared array
- From: minjie
- Cannot locate dynamically declared array
- Prev by Date: Re: Cannot locate dynamically declared array
- Next by Date: FAQ 8.18 How can I do an atexit() or setjmp()/longjmp()? (Exception handling)
- Previous by thread: Re: Cannot locate dynamically declared array
- Next by thread: Re: Cannot locate dynamically declared array
- Index(es):
Relevant Pages
|