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

I am getting errors by my code, which I can see why, I wrote this a few years ago, is there a better way to do it?
sub Get_Page_Vars { my ($temp_vars,$type) = @_; my $sth = $dbh->prepare (qq{ SELECT `name`,`value`,`add_name` FROM + `page_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()) +{ defined($content); $content =~ s/(\$[a-zA-Z0-9\{\'\}_]+)/ eval($temp_vars{$1}) /g +e if $temp_vars{$1}; $content =~ s/(\$[a-zA-Z0-9\{\'\}_]+)/ $temp_vars{eval($1)} /g +e if $temp_vars{$1}; $content =~ s/\<templ_var ([a-zA-Z0-9\{\'\}_]+)\>/ eval ($vars +{$1}) /ge if $vars{$1}; $content =~ s/\<templ_var ([a-zA-Z0-9\{\'\}_]+)\>/ $vars{eval( +$1)} /ge if $vars{$1}; $content =~ s/\<templ_var ([a-zA-Z0-9\{\'\}_]+)\>/ eval ($temp +_vars{$1}) /ge if $temp_vars{$1}; $content =~ s/\<templ_var ([a-zA-Z0-9\{\'\}_]+)\>/ $temp_vars{ +eval($1)} /ge if $temp_vars{$1}; $content =~ s/\<templ_var ([a-zA-Z0-9\{\'\}_]+)\>/ eval ($1) / +ge if $1; $content =~ s/{{([a-zA-Z0-9\{\'\}_]+)}}/ eval($vars{$1}) /ge i +f $vars{$1}; $content =~ s/{{([a-zA-Z0-9\{\'\}_]+)}}/ $vars{eval($1)} /ge i +f $vars{$1}; $content =~ s/(\$[a-zA-Z0-9\{\'\}_]+)/ eval($vars{$1}) /ge if +$vars{$1}; $content =~ s/(\$[a-zA-Z0-9\{\'\}_]+)/ $vars{eval($1)} /ge if +$vars{$1}; $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); $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}; $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); }
errors:
[Tue Sep 25 08:12:24 2007] web.cgi: Useless use of private variable in + void context at /path/to/user/readin_pagevars.cgi line 35. [Tue Sep 25 08:12:24 2007] web.cgi: Useless use of defined operator in + void context at /path/to/user/readin_pagevars.cgi line 7. [Tue Sep 25 08:12:24 2007] web.cgi: Useless use of defined operator in + void context at /path/to/user/vars_e_subs.conf line 36. [Tue Sep 25 08:12:24 2007] web.cgi: Useless use of defined operator in + void context at /path/to/user/vars_e_subs.conf line 38. [Tue Sep 25 08:12:24 2007] web.cgi: Unquoted string \"yr\" may clash w +ith future reserved word at /path/to/user/vars_e_subs.conf line 103. [Tue Sep 25 08:12:24 2007] web.cgi: Useless use of defined operator in + void context at /path/to/user/vars_e_subs.conf line 344. [Tue Sep 25 08:12:24 2007] web.cgi: Useless use of defined operator in + void context at /path/to/user/vars_e_subs.conf line 346. [Tue Sep 25 08:12:24 2007] web.cgi: Useless use of defined operator in + void context at /path/to/user/vars_e_subs.conf line 348. [Tue Sep 25 08:12:24 2007] web.cgi: Useless use of defined operator in + void context at /path/to/user/vars_e_subs.conf line 350. [Tue Sep 25 08:12:25 2007] web.cgi: Compilation failed in require at w +eb.cgi line 69. [Tue Sep 25 08:12:27 2007] Sess.pm: Ambiguous call resolved as CORE::o +pen(), qualify as such or use & at /path/to/user/md/Sess.pm line 92. [Tue Sep 25 08:12:27 2007] Sess.pm: Ambiguous call resolved as CORE::o +pen(), qualify as such or use & at /path/to/user/md/Sess.pm line 116. + [Tue Sep 25 08:12:27 2007] Sess.pm: Ambiguous call resolved as CORE::o +pen(), qualify as such or use & at /path/to/user/md/Sess.pm line 126.
If I have variables in the database, is there a better way that will not give me those "Useless use of defined operator in void context at ..." errors?

I'm filling up my apache log with those errors, and I don't know a better way to do it. Any pointers would be most excellent!

Thank you,
John

Replies are listed 'Best First'.
Re: Reading Variables out of a database.
by ikegami (Patriarch) on Sep 25, 2007 at 17:22 UTC

    The errors are pretty clear and straightforward. You might want to add use diagnostics; to the top of your Perl files to longer descriptions of the warnings. At least until you're familiar with the jargon.

    Useless use of defined operator in void context at /path/to/user/readin_pagevars.cgi line 7.

    defined($content); doesn't do anything. What did you want it to do?

    Useless use of private variable in void context at /path/to/user/readin_pagevars.cgi line 35.

    eval{$syscode2}; doesn't do anything. I think you want eval($syscode2);. Don't confuse eval BLOCK with eval EXPR. They are two completly different functions despite their unfortunatly identical name.

    [ numerous other errors ]

    You didn't provide the code that generated these errors, so I can't tell you anything more than use diagnostics; would provide.

    And you really really really should be using an existing templating system.

Re: Reading Variables out of a database.
by suaveant (Parson) on Sep 25, 2007 at 14:56 UTC
    I would strongly suggest porting your code over to Template Toolkit, but if for some reason you can't, an example of the database data and templates you are using would help.

                    - Ant
                    - Some of my best work - (1 2 3)