I wanted to use variables in database 'text'(content for a website). However, since perl won't use the variables in a database, it would just show the variable (ie: $_co_name for Company Name) in the text.

So I had to make a way to replace those variables while it is reading them from the database into the hash that calls them.
Ok, so it seems like what you're really after here is a home grown template system, right? Is it possible to use an existing robust one like Template Toolkit or HTML::Template? Using TT as an example, you would have the string I work for [% co_name %], the best company there is! stored in the db. Then perl code would look like:
my $template = Template->new(); my $vars = {co_name=>"Foo-Co"}; my $my_template_string = 'I work for [% co_name %], the best company t +here is!'; my $string; $template->process(\$my_template_string, $vars, \$string); # stores o +utput in $string print $string; $template->process(\$my_template_string, $vars ); # dumps output to S +TDOUT

As for your code, a couple major things jump out at me.. the first is eval ($vars{$1}) vs $vars{eval($1)} .. why are both necessary? can you provide template examples for each of the substitutions? is the intent to do something like this?
$content =~ s/(\$[a-zA-Z0-9\{\'\}_]+)/ eval($vars{$1}) || $var +s{eval($1)} || eval($1) /ge;

The other (related) item is looking at these two lines:
$content =~ s/(\$[a-zA-Z0-9\{\'\}_]+)/ eval($temp_vars{$1}) /g +e; $content =~ s/(\$[a-zA-Z0-9\{\'\}_]+)/ $temp_vars{eval($1)} /g +e;
They have the exact same pattern, and the first is a global replacement, which means that the second can never match (unless the first replacement value included a template string itself)... this goes for pretty much all of the replacement sets...

In reply to Re: Help with 'Use of uninitialized value...' error in DB code by davidrw
in thread Help with 'Use of uninitialized value...' error in DB code by powerhouse

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.