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

how to use unc paths in perl. I have a shared folder in comp1. \\comp1\newfolder. in comp1, i go to command prompt and give pushd \\comp1\newfolder. it works. but when i am doing the same in perl script system ('pushd \\comp1\newfolder'); the system complains. "the system cannot find the path specified."

Replies are listed 'Best First'.
Re: perl and unc paths
by ikegami (Patriarch) on Jul 16, 2006 at 05:25 UTC
    \\ in string literals only adds \ to the string being built. In other words, you need to double up \ chars (and add a \ in front of ' chars) in your string literal:
    system('pushd \\\\comp1\\newfolder');

    But that's not going to do what you want. system will create a new shell for pushd to run in, and pushd will only affect that shell. (It will also affect any child that shall will later spawn, but it spawns none as is.)

    Use chdir to change the working directory of the Perl script (and any children it later spawns). The current directory will get automatically restored when the script ends. If that's too late, use Cwd to find out which directory is the current working directory, and use chdir to restore it when you wish to do so.

      Except 'chdir \\\\comp1\\newfolder doesnt work!'
        On Windows that is... pushd and popd would work, except you can't do system("pushd \\\\unc_path1\\folder") because it spawns a new shell and then exits with system()