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 this
my ($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
thanks in advance
-theleftsock
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|