zdzieblo has asked for the wisdom of the Perl Monks concerning the following question:

hi, i'm not sure if understand perls eval() correctly... i've got the following problem.

$sth->execute(__variable_number_of_arguments_here); $string = '$a,$b,$c'; $sth->execute(eval($string));

is above gonna work? thank you

Replies are listed 'Best First'.
Re: eval dilema
by moritz (Cardinal) on Jul 14, 2008 at 09:54 UTC
    Why not just use $sth->execute($a, $b, $c)? If $string comes from somewhere else, consider passing a list instead of a string.

    Your eval might work if all of $a, $b, $c are in scope, but in general it's a very bad idea to do things like this. Is it an XY Problem?

      thanks for the quick reply, well the thing is that i never know how does the string gonna look like as it depends from user user input..

      is there a way of converting string to a list? (hope is not a stupid question)

      regarding xy thing.. dunno, i used to program a bit in c and that's how i was sorting similar problems there .. eval

        C does not have eval, so you must be thinking of some other programming language.

        Passing user input unfiltered to your database is a very bad idea. I think it's better if you describe to us the problem you're trying to solve with this approach.

        If you want to "convert a string to a list", let's assume that the string is a string of comma-separated items. Then split could be what you're looking for. Except that you won't be able to have any items that contain a comma as the value in your list. But maybe you should explain where you get the string from in the first place.

        the thing is that i never know how does the string gonna look like as it depends from user user input..

        If you don't know how the input looks, you're screwed. Processing natural language is very, very hard and usually works only partially.

        is there a way of converting string to a list?

        Many ways, the easiest involving split and a regular expression.

        Anyway, it might help if you'd describe us what problem you are trying to solve, and in which way you are trying to solve it - maybe there's a better way.