in reply to use qr// variable in right side of s/// operator

You need a double evaluation there. The first /e puts the contents of $str_regex into the replacement, and the second /e evaluates the contents as Perl code. Notice I double quote around $str_regex: one to quote it, and one for the Perl code I'll evaluate later.

$old_db = "emdb1"; $new_db = "kuku"; $string = "AUTHORIZATION,libctl62-m.sl,CONTROLM,emdb1,b02ed6600206 +ca161d165355c67b72073b6123d6838f1f68203b78cb9c4c63c9,emdb2,,;" ; $str_regexp = '"$1$2$3$new_db,"' ; $string =~ s/([^,]*,)([^,]*,)([^,]*,)([^,]*,)/$str_regexp/ee; print "$string\n";

Also, the right hand side is just a double quoted string. It's not a regexp in any way.

Lastly, if you're manipulating anything more complex regularly, you might want to use one of the comma separated value modules on CPAN rather than doing it yourself.

Good luck :)

--
brian d foy <brian@stonehenge.com>
Subscribe to The Perl Review

Replies are listed 'Best First'.
Re^2: use qr// variable in right side of s/// operator
by ikegami (Patriarch) on Feb 15, 2006 at 16:52 UTC
    Keep in mind that since the second 'e' causes the content of $str_regexp to be executed as Perl code, it would be dangerous to accept $str_regexp from a remote source such as a web page form. For example, consider what would happen if someone passed "system('rm -rf /')".

      Well, all the /e's evaluate the replacement string as Perl code. :)

      --
      brian d foy <brian@stonehenge.com>
      Subscribe to The Perl Review

        The first 'e' causes $str_regexp to be evaluated as Perl code. In other words, it uses the value of $str_regexp as the replacement string. That is compiled along with the rest of the program. There's no risk to this.

        The second 'e' causes the content of $str_regexp to be evaluated as Perl code. Since that could have come from anywhere, this is very dangerous.