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

I have been tasked with writing a basic package to map drives to NT and Windows 2000 servers. Based upon the domain that the server is located in, I simply use one of two different connection strings. To facilitate this, I simply perform 'nslookup' and call the appropriate syntax for Win32::NetResource::AddConnection. The problem that I am experiencing, is when I try to format a hash within a scalar to call AddConnection properly.

use Win32::NetResource qw(:DEFAULT AddConnection GetError); my $connex_string = null; my $win2k_uid = "uid"; my $win2k_pwd = "pwd"; my $persist = "/persistent:no"; if ($domain =~m/^domainOne$/i) { print "domainOne\n"; $net_resource = { "RemoteName" => "\\\\$object_md{MAP_SERVER}\\$argOne", "LocalName" => "$drive_letter", }; $connex_string = $net_resource; } elsif($domain =~m/^domainTwo$/i) { print "domainTwo\n"; $net_resource = { "RemoteName" => "\\\\$object_md{MAP_SERVER}\\$argOne", "LocalName" => $drive_letter, "Type" => "RESOURCETYPE_ANY", "Provider" => "", }; $connect_string = "$net_resource,$win2k_uid,$win2k_pwd,$persis +t"; } else { print "Undefined domain\n"; } if(AddConnection($connex_string)) { print "Connection work.\n"; } else { print "Connection did not work.\n"; }

This snippet of code gives me problems: $connect_string = "$net_resource,$win2k_uid,$win2k_pwd,$persist";

I know that it is the way that I am referencing the hash in the scalar.

This is the error that I am experiencing: "AddConnection: HASH reference required at C:/Perl/site/lib/Win32/NetResource.pm line 267."

I know how to reference the values of a hash with while and for loops. However, I just seem to have problems with hash references (i.e., \%hash, $$hash, %$hash, etc.). Can anyone offer some help? Also, in an effort to become a more competent programmer, is this the best way to accomplish the drive mappings to NT and Win 2K server?

Thanks in advance for any and all help!

TCF

Replies are listed 'Best First'.
Re: Hash Reference for Win32::NetResource::AddConnection
by shenme (Priest) on Sep 16, 2003 at 19:45 UTC
    Looking at my docs (might be outdated) it says, describing AddConnection:
      AddConnection( \%NETRESOURCE, $Password, $UserName, $Connection )
          Makes a connection to a network resource specified by %NETRESOURCE 
    
    so wouldn't you instead _not_ create your $connect_string but rather use the values directly?   You already have the hash reference in $net_resource.
    if( AddConnection($net_resource, $win2k_pwd, $win2k_uid, $Connection +) ) {
    Man, that's a strange order for the arguments!   And do you need to pre-declare $Connection?   And maybe the desired effect ala "/persistent:no" is simply accomplished through _not_ using Scope => RESOURCE_REMEMBERED, in your hash?   These are some things to try until others can give specific answers.
Re: Hash Reference for Win32::NetResource::AddConnection
by jdtoronto (Prior) on Sep 16, 2003 at 20:04 UTC
    By creating the connection string you are passing a single scalar to the AddConnection sub. You need to pass in the correct number of call parameters, in hte correct sequence, with the first being the HASHREF that you have already created.

    jdtoronto