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

In reply to Evaling Strings in Subs for Hashref Variable Names to Values by theleftsock

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.