in reply to use of useless void in context

I created a table in our mysql database for page variables, so the pages can load dynamically. I then call certain variables in text that is in a database record. Since I cannot use variables directly in mysql, I put them as merge fields, such as: ##variable## or something. There are several different types of variables, which were created over time, so I have accounted for each of them. Here is the subroutine I wrote to put these into a hash for storage while the system is running. We then later replace each of those merge fields:
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()) +{ if($content =~ /(\$[a-zA-Z0-9\{\'\}_]+)/) { $content =~ s/(\$[a-zA-Z0-9\{\'\}_]+)/eval($temp_vars{$1}) +/ge; $content =~ s/(\$[a-zA-Z0-9\{\'\}_]+)/$temp_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 =~ /\<templ_var ([a-zA-Z0-9\{\'\}_]+)\>/) { $content =~ s/\<templ_var ([a-zA-Z0-9\{\'\}_]+)\>/eval($va +rs{$1})/ge; $content =~ s/\<templ_var ([a-zA-Z0-9\{\'\}_]+)\>/$vars{ev +al($1)}/ge; $content =~ s/\<templ_var ([a-zA-Z0-9\{\'\}_]+)\>/eval($te +mp_vars{$1})/ge; $content =~ s/\<templ_var ([a-zA-Z0-9\{\'\}_]+)\>/$temp_va +rs{eval($1)}/ge; $content =~ s/\<templ_var ([a-zA-Z0-9\{\'\}_]+)\>/eval($1) +/ge; } if($content =~ /{{([a-zA-Z0-9\{\'\}_]+)}}/) { $content =~ s/{{([a-zA-Z0-9\{\'\}_]+)}}/eval($vars{$1})/ge +; $content =~ s/{{([a-zA-Z0-9\{\'\}_]+)}}/$vars{eval($1)}/ge +; } $content =~ s|<br>|<br />|g; 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); }
I had to go add a bunch of stuff because there were soooo many useless void errors. I know there are modules already that will do this more effieiently, however, I wrote this years ago, have not had time to upgrade the system to a better one. that is on my to-do list for the future though.

I certainly would love any feedback, negative or positive.

thank you.

Replies are listed 'Best First'.
Re^2: use of useless void in context
by toolic (Bishop) on Feb 07, 2009 at 18:33 UTC
    \w is a lot less typing than a-zA-Z0-9_

    You can cut down on back-whackin' by choosing a different delimiter: split m{</syscode>}i, $syscode, 2;

    Update: johngg++. Added m. I often botch untested code.

      split {</syscode>}i, $syscode, 2;

      According to the documentation, "If "/" is the delimiter then the initial m is optional" so once you choose a different delimiter I think you need the m. Therefore the code should be

      split m{</syscode>}i, $syscode, 2;

      I hope this is of interest.

      Cheers,

      JohnGG

        Since split always treats its first argument as a regular expression anyway you could also use a string:

        split '(?i:</syscode>)', $syscode, 2;
Re^2: use of useless void in context
by moritz (Cardinal) on Feb 07, 2009 at 17:50 UTC
    I think you would be better off with a template system like HTML::Template, instead of rolling a crippled one on your own.
Re^2: use of useless void in context
by missingthepoint (Friar) on Feb 08, 2009 at 06:01 UTC
    I certainly would love any feedback, negative or positive.

    That sub's not very readable. Perhaps:

    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()) + { { local $_ = $content; my $ident_key = qr/[\w{}']+/; my $phash_pat = qr/[$]$ident_key/; if (/$phash_pat/) { s/($phash_pat)/ eval($temp_vars{$1}) /ge; s/($phash_pat)/ $temp_vars{eval($1)} /ge; s/($phash_pat)/ eval($vars{$1}) /ge; s/($phash_pat)/ $vars{eval($1)} /ge; s/($phash_pat)/ eval($1) /ge; } if (/\<templ_var ($ident_key)\>/) { s/\<templ_var ($ident_key)\>/ eval($vars{$1}) /g +e; s/\<templ_var ($ident_key)\>/ $vars{eval($1)} /g +e; s/\<templ_var ($ident_key)\>/ eval($temp_vars{$1}) /g +e; s/\<templ_var ($ident_key)\>/ $temp_vars{eval($1)} /g +e; s/\<templ_var ($ident_key)\>/ eval($1) /g +e; } if (/{{$ident_key}}/) { s/{{($ident_key)}}/ eval($vars{$1}) /ge; s/{{($ident_key)}}/ $vars{eval($1)} /ge; } s|<br>|<br />|gi; $content = $_; } 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); }

    Less visual noise.


    Life is denied by lack of attention,
    whether it be to cleaning windows
    or trying to write a masterpiece...
    -- Nadia Boulanger