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

I am trying to use the win32::fileop module to map a drive. Here is the actual code below:
$drive = Map "$sh",{user=>"$us",pass=>"$pa",domain=>"$do"};
The variables above contain the following values:
$sh = '\\\\machine1\\d\$' $us = 'username' $pa = 'password' $do = 'domain'
I keep getting the following error:
"The specified device name is invalid."
However, if I were to cut and paste the exact values above and insert them into the Map statement, the process works beautifully. I have tried various combinations of single and double quotes, escaping each and then without escaping, with and without the quotes. How do I pass values/options to the Map function through variables?

Thanks

Replies are listed 'Best First'.
Re: Mapping a drive with Win32::Fileop
by ikegami (Patriarch) on May 11, 2008 at 20:08 UTC

    $sh = '\\\\machine1\\d\$';
    should be
    $sh = '\\\\machine1\\d$';
    or
    $sh = "\\\\machine1\\d\$";

    $\ = "\n"; print '\\\\machine1\\d\$'; # \\machine1\d\$ print '\\\\machine1\\d$'; # \\machine1\d$ print "\\\\machine1\\d\$"; # \\machine1\d$

    Update:

    Some elaboration:

    In single quotes strings, you need to escape the delimiter (single quotes) and backslashes followed by another backslash. Lone backslashes may optionally be escaped.

    In double quotes strings, you must escape $ (unless you want to interpolate), @ (unless you want to interpolate), the delimiter (double quotes) and backslashes. Other non-alphanumeric characters may optionally be escaped.

    Update:

    By the way,
    {user=>"$us",pass=>"$pa",domain=>"$do"}
    is better written as
    {user=>$us,pass=>$pa,domain=>$do}
    Putting quotes around variables needlessly makes a string copy of them.

Re: Mapping a drive with Win32::Fileop
by pc88mxer (Vicar) on May 11, 2008 at 23:53 UTC
    There's gotta be a better way to avoid backslash-hell. How about:
    my $b = "\\"; $sh = "$b${b}machine1${b}d\$";
    or is there another perl mechanism that can ease the pain of dealing with backslashes and other meta-characters in strings?

    Of course, the best way it avoid using them, but what if you are forced to?

      He can get away with only one extra slash if he uses single quotes: '\\\machine\d$'. But that's just confusing the issue. Remembering how to do it these ways is harder than learning how it works.

      Alternatively, he could use '//machine/d$' since Windows accepts either slashes. Tools don't necessarily accept either slash (especially unquoted), but the system does.

      ( my $b= '//machine1/d$' ) =~ tr-/-\\-;

      - tye