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

hi monks,

I intend to transfer a series of powershell scripts for creating database into perl script. but I'm stuck in replace of backslash.

take a look below commands
part of conf file ########################################### # Diagnostics and Statistics ########################################### background_dump_dest=%ORACLE_BASE%\admin\%ORACLE_SID%\bdump core_dump_dest=%ORACLE_BASE%\admin\%ORACLE_SID%\cdump #timed_statistics=TRUE user_dump_dest=%ORACLE_BASE%\admin\%ORACLE_SID%\udump command: in perl: `psed -e 's/\\/\//gp' init10CONVERGE.ora > init10CONVERGE.ora_for_unix +`; in shell: psed -e 's/\\/\//gp' init.ora > init.ora_for_unix`;
I'm surprised to find out almost the same command has different outcome. it doesn't work in perl, but does in shell.

Any suggestions? I've tried single quote, double quote, q{} etc, but seems not very helpful.





I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

Replies are listed 'Best First'.
Re: what difference does a command run in shell and in perl
by BrowserUk (Patriarch) on May 22, 2011 at 05:34 UTC

    The problem is that the backslashes are interpolated inside perl as a part of the back-quotes `` expression with the result that what gets passed to the shell look like this:

    psed -e 's/\///gp' init10CONVERGE.ora > init10CONVERGE.ora_for_unix

    Which isn't going to do what you intended. To avoid that, double escape the regex:

    `psed -e 's/\\\\/\\//gp' init10CONVERGE.ora > init10CONVERGE.ora_for_u +nix`;

    That said, as you are redirecting the output to a file, why are you using back-quotes? system would be more appropriate.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      ah~~ I see! Thanks very much! As back-quotes thing, it is just for test. I'll declare variables like $command, @pars etc and pass to system function.




      I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

Re: what difference does a command run in shell and in perl
by rgcosma (Beadle) on May 22, 2011 at 10:42 UTC
    Given the format of file you are reading, Config::IniFiles might do a better job at giving you the key / value pairs that you can then dump in another file.
    It can handle multi-line values that your simple replace breaks, see Parameter Files in the Oracle DB ref
Re: what difference does a command run in shell and in perl
by Gulliver (Monk) on May 22, 2011 at 14:53 UTC

    BrowserUK already pointed out your particular problem with backticks. One thing I would suggest is you can use characters other than '/' for matching and substitution, etc. I'm a big fan of doing this when it makes things easier to follow. Anytime '/' or '\' is involved is a good time for {} or other characters.

    ########################################################## # change back slashes to forward sub slashfwd { my $retval = $_[0]; $retval =~ s{\\} {/}g; return $retval; }