in reply to How to parse perl variable defined in config file

Maybe you want to use a proper templating system to replace variable names?

If you need to do this yourself, follow these steps:

  1. Read the variable values
  2. Read the statement
  3. Replace the variable values in the statement
  4. Execute the statement

For step three, I suggest the following data structure:

my %variables= ( schema => 'myschema', name => 'my_name', ); my $statement= 'select * from $schema.app_table where col1 = $name'; $statement =~ s/\$(\w+)/$variables{ $1 } || '$'.$1/ge;

Note that you will either need to add proper quotes around $name in the SQL statement or add additional logic to recognize when a variable is used as a column value and then transform the logic to use SQL placeholders.

Replies are listed 'Best First'.
Re^2: How to parse perl variable defined in config file
by udvk009 (Novice) on Jun 03, 2015 at 14:32 UTC

    Thanks for the quick suggestion!