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

Ok, I have a script, in which I read from a text file, such as this:
open(FILE, "/home/user/template/$filename.txt"); while (<FILE>) { s/(\$[a-zA-Z0-9\{\'\}_]+)/ eval ($1) /ge; $template{'content'} .= $_; } close(FILE);
This reads every $string in the text file, and replaces it with the perl value of $string since it normally would not, because of it being in a text file and not read as a string by Perl.
It then puts that "value" into a hash.

That is my understanding, as limited as it is.

Here is my question.

What if that is in a database, and instead of it having $string it has {{string}} and I normally do a s/{{string}}/$string/g;

I'm getting a error doing it that way in a subroutine, because not every database value has the same {{stringname}} so I get a lot of "useless switch" errors.

So how do I get the database record to be in a value of <FILE> so that I can do it like my other one, except like this:
while(my ($db_name,$content,$_add_name) = $sth->fetchrow_array()) { s/\<templ_var ([a-zA-Z0-9\{\'\}_]+)\>/ eval ($1) /ge; s/{{([a-zA-Z0-9\{\'\}_]+)}}/ eval ($1) /ge; $_content = $_; if ($_add_name) { $_content = qq~<!--Start $db_name-->$_content<!--End $db_name- +->~; } $temp_vars{$db_name} = $_content; }

I hope I made this clear enough.
I would appreciate any tips or advice.
Thank you.

Richard

Replies are listed 'Best First'.
Re: <FILE> Questions
by Chady (Priest) on Nov 10, 2003 at 14:42 UTC

    There's a lot of wierd stuff going on in here, I'm not sure I got this right, but let's try.

    In your second example, you use $_content where I see a $content read from the db. then you perform your substitution on $_ where you should have done it -- I assume -- on $content, $content =~ s/{{([a-zA-Z0-9\{\'\}_]+)}}/ eval ($1) /ge;

    OTOH, I think it's safer/better(?) to use a hash instead of evaluating strings. So instead of doing s/{{([a-zA-Z0-9\{\'\}_]+)}}/ eval ($1) /ge; you would do s/{{([a-zA-Z0-9\{\'\}_]+)}}/$hash{$1}/g; - but I don't know the context of this, so I can't judge.

    does this actually run? what errors does it produce?

    Update: I re-read your question, namely this part:

    What if that is in a database, and instead of it having $string it has {{string}} and I normally do a s/{{string}}/$string/g;
    That would work if you do it like so:
    s/{{([^}]+)}}/${$1}/g;
    unless you're using strict refs.

    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/
Re: <FILE> Questions
by talexb (Chancellor) on Nov 10, 2003 at 15:44 UTC

    In addition to the previous post, I don't see where you are saving or outputting the result of your variable translation in either of your examples .. did you leave that out for simplicity's sake?

    --t. alex
    Life is short: get busy!
      Thank you so much!!!

      Yeah, I saved it for ease, I guess I should have put the whole subroutine in there.

      I'm saving it after the 2nd while statement, inside the first one(that gets stuff from the db) into a hash like this:$template{$dbname} = $_content;

      I'm taking it out of the database, with $content. I'm putting it in the $_content from the while <> statement.

      I got it to work like this:
      The Whole Subroutine that is getting data from the database, settings and everything:
      sub Get_Page_Vars { my ($temp_vars,$type) = @_; $sth = $dbh->prepare (qq{ SELECT `name`,`value`,`add_name` FROM `p +age_vars` WHERE (`type` OR `type2`) = ? }); $sth->execute($type); # I know I should have done this different : +o) while(my ($db_name,$content,$_add_name) = $sth->fetchrow_array()) +{ $content =~ s/\<templ_var ([a-zA-Z0-9\{\'\}_]+)\>/ eval ($vars +{$1}) /ge if $vars{$1}; $content =~ s/{{([a-zA-Z0-9\{\'\}_]+)}}/ eval ($vars{$1}) /ge +if $vars{$1}; if ($_add_name) { $content = qq~<!--Start $db_name-->$content<!--End $db_nam +e-->~; } $temp_vars{$db_name} = $content; } $sth->finish(); return(%temp_vars); }


      I don't know why I did not think of the things you suggested, I thought about doing it, but could not call to mind how to try it. Thank you for your advice, tips and comments. I really appreciate it!

      thx,
      Richard
Re: <FILE> Questions
by iburrell (Chaplain) on Nov 10, 2003 at 22:12 UTC
    Other people have covered the problem of the substitutions. If you want to use same substutions on files and database records, I would suggest splitting the manipulations into a subroutine.
    while (my $line = <FILE>) { $content .= substitute_parameters($line); } while (($content) = $sth->fetchrow_array()) { $content = substitute_parameters($content); } sub substitute_parameters { my ($content) = @_; $content =~ s/{{([a-zA-Z0-9\{\'\}_]+)}}/$hash{$1}/ge; return $content; }
      I do have one more question...

      Say in the database, I have one that is named say, uhm, "_domain".

      Is there a way to use a similar function to make $_domain equal to that value, IF I do not make perl do that by telling it $_domain = $vars{_domain};

      For instance, say I have in my database, like 10 different "variables" that I have named. is there a way to make those "named variables" a perl string?

      I hope I am making this clear enough. I would not know the syntax to even try it. Is it possible?

      Thank you.
      Richard