theleftsock has asked for the wisdom of the Perl Monks concerning the following question:
Good Morning. I poked around, and found some .cgi discussions about this, but did not find an exact answer to my question. I thought an eval{$string} would do it, but lets get to it, shall we?
I have a file, we'll call sql_string.sql that has a sql string in it, written to have some variable names in it. I want to read in that string, then send it to a sub along with HASHREF that contains the necessary variables. Then I want to swap out those var names for the var values. Maybe I need to explicitly deference the HASHREF? Any help would be great. BTW, I am not sure if there is better pragma for this. I am going to be doing a lot of switching on some SQL for various uses. i don't want to inline the SQL, and I want to be able assign values to various pieces for multiple use, so additional style comments are always welcome.
Sort of like thisthanks in advance -theleftsockmy ($sql_var_hr); $$sql_var_hr{col_name} = 'column_name'; my $sql = get_sql($sql_han, $sql_var_hr); sub get_sql { my ($file_han, $vars) = @_; #vars is a hashref containing any swit +ch variables for the select my ($iline, $sql); print "vars: $$vars{col_name}\n"; while ($iline = <DATA>) { if ($iline =~ /^#/) { next; } else { $sql = $sql.$iline; } } print "pre_eval: $sql\n"; eval{$sql}; print "post_eval: $sql\n"; return $sql; } #the following are just attempts __DATA__ select $vars{col_name} from schema.table select $$vars{col_name} from schema.table select ${$var{col_name}} from schema.table
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Evaling Strings in Subs for Hashref Variable Names to Values
by choroba (Cardinal) on Nov 12, 2012 at 16:21 UTC | |
by theleftsock (Beadle) on Nov 12, 2012 at 17:25 UTC | |
by CountZero (Bishop) on Nov 12, 2012 at 18:03 UTC | |
by theleftsock (Beadle) on Nov 13, 2012 at 14:38 UTC |