I made some code, that is causing these errors:
Use of uninitialized value in eval "string" at /path_to/req/readin_pag +evars.cgi line 10, <STDIN> line 47. Use of uninitialized value in eval "string" at /path_to/req/readin_pag +evars.cgi line 10, <STDIN> line 47. Use of uninitialized value in eval "string" at /path_to/req/readin_pag +evars.cgi line 10, <STDIN> line 47. Use of uninitialized value in substitution iterator at /path_to/req/re +adin_pagevars.cgi line 10, <STDIN> line 47. Use of uninitialized value in eval "string" at /path_to/req/readin_pag +evars.cgi line 10, <STDIN> line 47. Use of uninitialized value in eval "string" at /path_to/req/readin_pag +evars.cgi line 10, <STDIN> line 47.


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.

Can you please critique my code and tell me what could be causing those errors?

Here is my code:
sub Get_Page_Vars { my ($temp_vars,$type) = @_; my ($code1, $code2, $_do_code, $syscode1, $syscode2); $sth = $dbh->prepare (qq{ SELECT `name`,`value`,`add_name` FROM `p +age_vars` WHERE `type` = ? OR `type2` = ? ORDER BY add_name,id,d}); $sth->execute($type,$type); while(my ($db_name,$content,$_add_name) = $sth->fetchrow_array()) +{ $content =~ s/(\$[a-zA-Z0-9\{\'\}_]+)/ eval($temp_vars{$1}) /g +e; $content =~ s/(\$[a-zA-Z0-9\{\'\}_]+)/ $temp_vars{eval($1)} /g +e; $content =~ s/\<templ_var ([a-zA-Z0-9\{\'\}_]+)\>/ eval ($vars +{$1}) /ge; $content =~ s/\<templ_var ([a-zA-Z0-9\{\'\}_]+)\>/ $vars{eval( +$1)} /ge; $content =~ s/\<templ_var ([a-zA-Z0-9\{\'\}_]+)\>/ eval ($temp +_vars{$1}) /ge; $content =~ s/\<templ_var ([a-zA-Z0-9\{\'\}_]+)\>/ $temp_vars{ +eval($1)} /ge; $content =~ s/\<templ_var ([a-zA-Z0-9\{\'\}_]+)\>/ eval ($1) / +ge; $content =~ s/{{([a-zA-Z0-9\{\'\}_]+)}}/ eval($vars{$1}) /ge; $content =~ s/{{([a-zA-Z0-9\{\'\}_]+)}}/ $vars{eval($1)} /ge; $content =~ s/(\$[a-zA-Z0-9\{\'\}_]+)/ eval($vars{$1}) /ge; $content =~ s/(\$[a-zA-Z0-9\{\'\}_]+)/ $vars{eval($1)} /ge; $content =~ s/(\$[a-zA-Z0-9\{\'\}_]+)/ eval($1) /ge; if ($content && ($content =~ /<code>/i && $content =~ /<\/code +>/i)) { while($content =~ /<code>/i && $content =~ /<\/code>/i) { my ($code1,$code2,$_do_code); ($content,$code1) = split /<code>/, $content, 2; ($code2,$ocontent) = split /<\/code>/, $code1, 2; $_do_code = eval($code2) if $_do_code && $_do_code ne +""; $content = $content . $_do_code . $ocontent; } } if ($content && ($content =~ /<syscode>/i && $content =~ /<\/s +yscode>/i)) { while($content =~ /<syscode>/i && $content =~ /<\/syscode> +/i) { my ($syscode1,$syscode2); ($content,$syscode1) = split /<syscode>/i, $content, 2 +; ($syscode2,$ocontent) = split /<\/syscode>/i, $syscode +1, 2; eval($syscode2) if $syscode2 && $syscode2 ne ""; $content = $content . $ocontent; } } if ($_add_name) { $content = qq~<!--Start $db_name-->$content<!--End $db_nam +e-->~; } $temp_vars{$db_name} = $content; } $sth->finish(); return(%temp_vars); }

I would appreciate any and all positive criticism.

Thank you very much.
thx,
Richard

Retitled by g0n from 'Positive Criticism Requested, please.'.


In reply to 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.