in reply to substituting a regular expression in a file

Rule #1 of using system: avoid the shell. Unless you can't. Here is a case of both.

You can avoid the shell locally. Do so. You can't avoid the shell remotely, so we'll deal with that.

Rule #3 (I think) of regexps: avoid leaning toothpick syndrome (LTS). Too many escapes can get confusing. Change your delimiter if you have to. In your case, you're just escaping everything - no need. (Funnily enough, you missed the extra \ on "\." in "command\.package" - but by avoiding the extra shell, we shouldn't need to worry about it. As much.)

system('rsh', $sdpType, qq[perl -pi -e 's/^(command\.package)=(\S+)/"\$1=$version"/gei' + $ENV{JBOSS_HOME}/install/version.properties]);
You may still need to double what few escapes we have for the remote shell. And may need to double again for the local perl. But the $1 shouldn't need extra escaping. That said, you probably also want to simplify this just a wee bit more:
system('rsh', $sdpType, qq[perl -pi -e 's/(?<=^\Qcommand.package=\E)\S+/$version/i' $EN +V{JBOSS_HOME}/install/version.properties]);
By using a zero-width positive look-behind assertion (see perlre, we can get rid of your eval modifier. By using \Q and \E (those \'s may need doubling), we no longer worry about special characters (admittedly, there's only one - but it's a useful habit to be in). And your g modifier was never really needed since we anchored at one end anyway.

(Warning: untested code - use at own risk.)

Replies are listed 'Best First'.
Re^2: substituting a regular expression in a file (\\)
by tye (Sage) on Sep 06, 2006 at 17:43 UTC

    To check one's work, just go one step at a time.

    Inside of qq[...], \. is just . and \S is just S. So rsh will get the following argument:

    perl -pi -e 's/^(command.package)=(S+)/"$1=..."/gei' $ENV{JBOSS_HOME}/ +install/version.properties

    which is clearly wrong.

    I'd replace \. with [.] since that survives this type of situations more easily. So the remaining trick is how to get \S to the remote Perl. \\S would give \S to rsh which would give '...\S...' to the remote shell which would work.

    The /e isn't need on this s/// so drop that and drop the double quotes as well:

    system( 'rsh', $sdpType, q[perl -pi -e 's/^(command[.]package)=(\\S+)/$1=] . $version . q[/gi' $ENV{JBOSS_HOME}/install/version.properties] +, );

    Note that qq[\\S] eq q[\\S], and I avoid using things like q[\S] since it tends to lead people into thinking that q[\\mach\share\dir\] works.

    This sends the following argument to rsh:

    perl -pi -e 's/^(command[.]package)=(\S+)/$1=.../gi' $ENV{JBOSS_HOME}/ +install/version.properties

    which gets sent to the remote shell producing the following arguments

    perl -pi -e s/^(command[.]package)=(\S+)/$1=.../gi .../install/version.properties

    which should work.

    Note that if $version ends up containing any single quote characters, then you are in trouble again.

    Inserting the 'echo' command at different points can help you while you figure out how quoting works at the different stages:

    system( 'echo', 'rsh', $sdpType, q[perl -pi -e 's/^(command[.]package)=(\\S+)/$1=] . $version . q[/gi' $ENV{JBOSS_HOME}/install/version.properties] +, ); #then system( 'rsh', $sdpType, q[echo perl -pi -e 's/^(command[.]package)=(\\S+)/$1=] . $version . q[/gi' $ENV{JBOSS_HOME}/install/version.properties] +, );

    - tye