Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

use of useless void in context

by Anonymous Monk
on Feb 07, 2009 at 17:29 UTC ( [id://742139]=perlquestion: print w/replies, xml ) Need Help??

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

I am getting an error around this line...
$content =~ s/(\$[a-zA-Z0-9\{\'\}_]+)/ eval($1) /ge if $1;
I think that $1 is not set outside of the s/// marks, so will this work:
$content =~ s/(\$[a-zA-Z0-9\{\'\}_]+)/ if $1(eval($1)) /ge;
I am looking to replace merge fields within a database record.

Thank you.

Replies are listed 'Best First'.
Re: use of useless void in context
by moritz (Cardinal) on Feb 07, 2009 at 17:35 UTC
    I think that $1 is not set outside of the s/// marks

    That's correct.

    $content =~ s/(\$[a-zA-Z0-9\{\'\}_]+)/ if $1(eval($1)) /ge;

    That won't work. At the very least it's spelled if (...) { ... } (but I guess it won't work when an expression is expected).

    That said, why do you want that if at all? evaling an empty string isn't an error, so I don't see why you want to avoid it.

Re: use of useless void in context
by JavaFan (Canon) on Feb 07, 2009 at 17:36 UTC
    $content =~ s/(\$[a-zA-Z0-9\{\'\}_]+)/ eval($1) /ge if $1;
    The '$1' after the if would refer to the $1 set by the last successful match. Also, if you want to eval the content of $1, no need to do both eval($1) and /e. Just /e should do.
    $content =~ s/(\$[a-zA-Z0-9\{\'\}_]+)/ if $1(eval($1)) /ge;
    That doesn't make any sense.

    Since you don't tell us what you try to do, I won't bother offering suggestions.

      I agree. From what I can tell, the OP could use a backreference instead ( \1, \2, etc ), but I can't quite puzzle out what the OP is trying to accomplish.
        Considering the OP is using all his backreferences outside of the pattern itself, I don't see where the OP should use a \1 instead of a $1.
Re: use of useless void in context
by Anonymous Monk on Feb 07, 2009 at 17:49 UTC
    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.
      \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

      I think you would be better off with a template system like HTML::Template, instead of rolling a crippled one on your own.
      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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://742139]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-03-29 01:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found