Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Accessing Windows Path from Perl script

by kulls (Hermit)
on Aug 13, 2010 at 13:42 UTC ( #854910=perlquestion: print w/ replies, xml ) Need Help??
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

Comment on Accessing Windows Path from Perl script
Download Code
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 (Priest) 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 (Canon) 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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://854910]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2014-12-18 01:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (41 votes), past polls