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

Hello Perl Gaints,
I have a file qry.txt. It has list of queries. I have written it with the variable itself. That is Eg:

select * from emp where emp=$name; select * from sal where code=$code; ...

Now i am reading this file ,get the query one by one, assign it to a variable $qryvar and then exceute it one after the other. The problem is that, the variables $name and $code are not getting parsed.The values are not assigned to them. They stay as the same $name $code. How to bring the variable's value in the query now?

How to accomplish this?
Best Regards,
rad

20050111 Janitored by Corion: Moved from PMD to SoPW, added formatting

Replies are listed 'Best First'.
Re: Parse $ Variable in text file
by insaniac (Friar) on Jan 11, 2005 at 10:46 UTC
    so basically, you'll do something like this (if you haven't read the perldoc the AnoMonk gave us):

    perl -e '$name='kak';$q=do {open(F,"/tmp/file");<F>}; $q=~ s/\$(\w+)/$ +{$1}/g;; print $q' gives: select * from kak cat /tmp/file select * from $name

    In my example, the /tmp/file only contains one line, that's why I assigned it to a scalar and not an array.
    Hope this helps...

    --
    to ask a question is a moment of shame
    to remain ignorant is a lifelong shame

        More specifically it doesn't work with lexical (my) variables - you will need to use package variables instead.

        /J\

        ok thanks!... then someone needs to update perldoc -q "expand variable" because i just quickly copied and pasted it ;-)

        i didn't even test the code.. since it looked so obvious. but, once more!, ppl slap me around the ears saying: "use strict" and try out your code !
        one day i'll learn... one day ;-)

        --
        to ask a question is a moment of shame
        to remain ignorant is a lifelong shame
Re: Parse $ Variable in text file
by Anonymous Monk on Jan 11, 2005 at 10:06 UTC
Re: Parse $ Variable in text file
by Joost (Canon) on Jan 11, 2005 at 20:58 UTC
    If you are willing to change the syntax of your txt file to
    select * from emp where emp={$name}; select * from sal where code={$code};

    Text::Template should be able to do the job easily. Other template systems will work as well but they usually have yet other syntaxes.

    In this specific case, it might be easier to just use $dbh->selectall_hashref(eval $line); if you can trust the input of the textfile and the values in the variables.

Re: Parse $ Variable in text file
by ZlR (Chaplain) on Jan 12, 2005 at 15:22 UTC
    Hello,

    I used the following line to achieve this goal :

    $ValCmd = eval qq("$ValCmdEx") ;
    This expands all the variables contained in the string ValCmdEx .

    These vars should all be correctly set before this command, or $ValCmd will be undef.
    So when I do this, I usually test afterwards that I get a coherent result

    I use this method because it's fairly straightforward and use strict compliant, but I have to admit that I’m not 100% sure of what it does exactly. Also, i'm not certain using eval is a good thing.

    ZlR