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

Hi Monks, I have a perl script written for Windows2000/XP which invokes the system function to call another perl script wrapped in a batch:
$outdir = "\\machine\share\dir"; chdir($outdir) #==1 system($proc_that_runs_batch);
When I print Cwd::cwd() from the perl script in the batch, it returns c:\windows????. However,
$outdir = "h:\dir"; chdir($outdir) #==1 system($proc_that_runs_batch);
will print "h:\dir" in the batch perl script. Can anyone clue in me in as to what's going on? The batch, by the way, was generated by pl2bat.bat. thanks, Michael

Replies are listed 'Best First'.
Re: perl & UNC paths
by bart (Canon) on May 05, 2004 at 21:41 UTC
    Double your backslashes. If perl is able to work with UNC paths, that should fix it. Because now, your "UNC path" isn't an UNC path.

    If it can't... Well, map the drive or directory to a letter.

      you can also set it up as follows:

      use File::Spec; my $dir=File::Spec->catdir('//server','share','folder','subfolder');

      Note that the \\ has been reversed from normal, this does work, we do this all the time with our scripts on Windows. Also, you can do the following:

      chdir $dir or die "Cannot change to $dir. $!";

      We do this quite frequently with our Windows Perl scripts. You cannot do this from the commandline (if I remember correctly).

Re: perl & UNC paths
by samtregar (Abbot) on May 05, 2004 at 21:13 UTC
    You need to check that chdir() is actually succeeding. Try this:

      chdir($outdir) or die "Unable to chdir to '$outdir': $!\n";

    Most likely your first chdir() is failing.

    -sam

Re: perl & UNC paths
by Corion (Patriarch) on May 05, 2004 at 21:22 UTC

    In addition to what samtregar has to say, I believe that it's not possible to change the "current directory" to an UNC path, as the Windows concept of "current directory" is tied to a volume/drive letter.

      To clarify, Windows processes do not have a single "current directory" like Unix processes. Instead, there is a "current drive" and for each drive, there is a "current directory" on that drive.

      Your current drive cannot legally be a UNC share, and a UNC share has no current directory concept. (Though I think I've seen some error-handler circumstances where Windows has confused itself into a state that looks close.)

      --
      [ e d @ h a l l e y . c c ]