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

hi, i have written a small script to know the name of current working directory on windows machine. the script is like this
use strict; use warnings; use Win32; my $V_CurrDir; unless ($V_CurrDir = ( Win32::GetFullPathName( Win32::GetLongPathName +( $0 ) ) )[0]) { die "$!"; } print "$V_CurrDir";
when i run this script from the command prompt it is not working..while i run this script from perlwiz IDE it is working..any clue why it is not working..and i make sure that libwin32 module is installed..iam using activestateperl distribution..please help me..

Janitored by Arunbear - changed title from 'problem with win32 module' and added code tags

Replies are listed 'Best First'.
Re: Current working directory on win32
by EverLast (Scribe) on Oct 14, 2004 at 12:49 UTC

    It seems there's a problem with GetFullPathName - it bombs out here too...

    But why the fuss with Win32-specificness? How about:

    use strict; use warnings; use Cwd; print getcwd();

    ---Lars

Re: Current working directory on win32
by gellyfish (Monsignor) on Oct 14, 2004 at 12:50 UTC

    You will probably see why it is not working when you run it from the command line by printing out $0 at some point int your program.

    /J\

Re: Current working directory on win32
by pernod (Chaplain) on Oct 14, 2004 at 12:47 UTC

    Umm .. If you only need the working directory, I would suggest using FindBin instead, as it is platform independent.

    use strict; use warnings; use FindBin; print $FindBin::Bin, "\n"; # prints c:\dev\temp\monks\

    A quick glance at the POD for Win32 shows that Win32::GetLongPathName in list context returns the path and the file as two different components. It seems to me that you are trying to do this:

    use Win32; my( $path, $filename ) = Win32::GetFullPathName( $0 ); my $longpath = Win32::GetFullPathName( $path ); print $longpath; # prints c:\dev\temp\monks\

    Here I find the path and filename component and stuff them into variables. Then I attempt to translate $path to a long path with the second call to Win32::GetFullPathName. This works on my machine with Activestate Perl 5.8 and Windows XP.

    Hope this helps.

    pernod
    --
    Mischief. Mayhem. Soap.

      > i have written a small script to know the name of current working directory

      The original poster seems to want cwd, at least that's what he states. FindBin "Locate's directory of original perl script".

      ---Lars

        Ah! I think I see your nuance, although I would argue that it is of a somewhat academic character in this case. Isn't "Locates directory of original perl script" in this case similar to Cwd's "get pathname of current working directory"?

        After all, the end results are (on my box) the same. TIMTOWDI, you know ;)

        use FindBin; use Win32; use Cwd; my( $path, $filename ) = Win32::GetFullPathName( $0 ); my $longpath = Win32::GetFullPathName( $path ); print "FindBin: $FindBin::Bin\n"; print "Win32: $longpath\n"; print "Cwd: ", cwd(), "\n";

        The result:

        c:/dev/temp/monks $ perl wdir.pl FindBin: C:/dev/temp/monks Win32: c:\dev\temp\monks\ Cwd: c:/dev/temp/monks

        Seems to me that FindBin and Cwd does more or less the same job here?

        pernod
        --
        Mischief. Mayhem. Soap.

Re: Current working directory on win32
by Grygonos (Chaplain) on Oct 14, 2004 at 15:05 UTC

    Unless I'm really missing something can't you just  my $pwd = `pwd`; I know that doesn't work on network drives.. but on the local machine it should work just fine shouldn't it ?

      It won't work on Windows as specified in the subject header, unless the OP has installed cygwin or similar.


      --
      Regards,
      Helgi Briem
      hbriem AT simnet DOT is

        On Windows you could do $pwd = `CD` infact

        /J\

Re: Current working directory on win32
by SamCG (Hermit) on Oct 15, 2004 at 19:54 UTC
    Ummm. . . can't you --
    #!perl use Cwd; $cwd = cwd(); print $cwd;

    ??? It works on my WinXP machine, and is referenced in Llama, so I assume the module's standard.