http://qs321.pair.com?node_id=854910

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

Hi,
I'm unable to detect the correct windows path in the below code.
Can anyone suggest me how many '\' that I need to add it in the path to make it works ?
Is there any better modules to tackle the path issues in windows ?
#!C:\perl\bin\perl use strict; use warnings; my %typehash = ( 'tomcat' => '\\d\$\\logs\\tomcat', 'tomcatweb' => '\\d\$\\logs\\web', 'apache' => '\\d\$\\logs\\Apache' ); sub getLog { my ($a,$type)=@_; my $server=$a; my $dir = "\\\\$server".$typehash{$type}; print "\n $dir "; chomp(my $meh = `dir /b /O:D $dir`); print $meh; } &getLog("test.control.com",'tomcat');

-Raja

Replies are listed 'Best First'.
Re: Accessing Windows Path from Perl script
by BioLion (Curate) on Aug 13, 2010 at 14:17 UTC

    Firstly you need to check out perldoc: quotelike operators and see the difference between "$string\\path" and '$string\\path'.

    Also windows paths are notoriously awkward, so maybe a path name handler module would be useful, rather than hard coding them? Check out Win32 and File::Basename among others...

    Hope this helps, let us know how you get on!

    Update: Fixed links...

    Just a something something...
Re: Accessing Windows Path from Perl script
by VinsWorldcom (Prior) on Aug 13, 2010 at 14:22 UTC

    I think your problem is the interpolation and non-interpolation with double versus single quotes. You don't need to escape your backslashes and dollar sign in your %typehash as you're using single quotes.

    This worked (printed the right path) for me:

    #!C:\perl\bin\perl use strict; use warnings; my %typehash = ( 'tomcat' => '\d$\logs\tomcat', 'tomcatweb' => '\d$\logs\web', 'apache' => '\d$\logs\Apache' ); sub getLog { my ($a,$type)=@_; my $server=$a; my $dir = "\\\\$server".$typehash{$type}; print "\n $dir "; chomp(my $meh = `dir /b /O:D $dir`); print $meh; } &getLog("test.control.com",'tomcat');

    And executed:

    {Z} \\vmware-host\Shared Folders > perl script.pl \\test.control.com\d$\logs\tomcat The network path was not found.
Re: Accessing Windows Path from Perl script
by roboticus (Chancellor) on Aug 13, 2010 at 14:58 UTC

    kulls:

    Normally perl will work fine using forward slashes instead of backslashes--even in Windows. So you can save yourself tons of headaches by using them:

    my $dir = "//$server/d/logs/tomcat";

    The only time I have problems with it is when I'm executing another program from perl. Then you may have problems with the shell handing special characters, the external program not wanting forward slashes, etc. But I rarely execute other programs from perl, so it hasn't been a problem for me.

    ...roboticus

Re: Accessing Windows Path from Perl script
by jonadab (Parson) on Aug 13, 2010 at 19:26 UTC