On 05/14/2017 02:00 PM, lee wrote:
Uri Guttman <u...@stemsystems.com> writes:

On 05/13/2017 09:08 PM, lee wrote:
Hi,

would you say this acceptable or something that should be forbidden?


my $sth_cmds = $dbh->prepare_cached($sql);
my @params;
push(@params, undef) foreach(0..12);
$sth_cmds->execute();
$sth_cmds->bind_columns(eval join(', ', map('\$params[' . $_ . ']', 
0..$#params)));


I haven't used 'eval' before, and this seems inherently dangerous.
you are correct in that string eval is very dangerous. my rule is you
never use it until you learn when not to use it! :)
How do you learn that unless you use it?

you have to learn what is used for and wait until you find a proper use. as i said in another post, eval EXPR is the solution of last resort. it isn't needed in almost any code. beginners pretty much can ignore it. as you become more proficient in perl, you will run into cases where it is useful. but please ignore it until then for your code's safety. it is too easy to do something very nasty with it and it is very hard to debug.

if you really want to see live cases of eval EXPR, check out my modules Template::Simple and Sort::Maker. both generate perl code for efficiency and then compile it so you can run it later multiple times. that is one important use when you are going for top speed.
you likely want to use the dbi calls that return a hash for each row
you select. i always forget the name but it is something like
fetchall_hashrows. you can look it up easily. it is generally the
nicest way to get rows from dbi IMO.
That depends on what you want to do: IIRC, I was using this to prepare
data for Chart::Bars.  That requires (references to) arrays containing
the data you want to have plotted in the chart, each array representing
one set of data.  Since the data is in columns in the database, you need
to transform the columns into suitable rows:


database:
val1a, val2a, val3a, ...
val1b, val2b, val3b, ...
...


Chart::Bars:
val1a, val1b, val1c, ...
val2a, val2b, val2c, ...
...


That isn't too clear in the documentation and can be rather confusing
until you get the idea.

You would need something like a "multi hash" for this, i. e. a hash in
which each key refers to multiple elements in the form of a row (like an
array) rather than the usual 1 to 1 hash that has each key referring to
a single value (like a reference to such an array).  That still leaves
you with having to do the transformation and no advantage to using
hashes that I could see.
hashes of hashes are standard perl data structures. there is no need for eval to build them up.


The fetchall_something functions return, IIRC, a reference to an array
containg references to arrays of the values you want to fetch.  That
always gives me trouble with dereferencing to finally get the values.
you can get an array of hashes, where each hash is one row.

learning dereferencing will do you much more good than using eval EXPR. references are key to deeper data structures in perl and are used all over the place. it is a core skill you should focus on as it will solve your current problem and many to come.

if you need help with references, this list is the place to ask.


They are potentially dangerous because you might fetch huge numbers of
rows and run into memory issues which could bring down the server.  I'm
avoiding them unless they have a significant advantage and when I can be
sure not to fetch too many rows, or when I would have all the data in
the program anyway.

you can get a row at a time i am sure with each row being a hash.

uri

--
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