On Tue, May 24, 2016 at 03:58:20PM -0500, Todor Fassl wrote:
> The second parameter to the Net::LDAP function is a hash reference. One of
> the keys for that hash can be "attrs". This allows you to specify the
> attributes you want to add. In the examples in the documentation on the
> CSPAN web site, it shows that key being set to an anonymous array reference
> -- not a *hash* reference. Yet, the example uses hash syntax.
>
> I don't know how to put it more clearly unless you look at the documentation
> itself. Here's a segment of the example:
>
> --- begin quote ---
> $result = $ldap->add( 'cn=Barbara Jensen, o=University of Michigan,c=US',
> attrs => [
> 'cn' => ['Barbara Jensen', 'Barbs Jensen'],
> 'sn' => 'Jensen',
> 'mail' => 'b.jensen@umich.edu',
> 'objectclass' => ['top', 'person',
> 'organizationalPerson',
> 'inetOrgPerson' ],
> ]
> );
> --- end quote ---
>
> Here the key "attrs" is set to an anonymous array reference (not a hash
> reference) and then it looks like they try to use the strings 'cn', 'sn',
> 'mail', and 'objectclass' as keys. From the code example that I put into my
> second message, it looks like what that code would do is to set the key
> "attrs" to a reference to an array with 8 elements, not a hash with 4
> key/value pairs.
>
> I am asking if anyone can explain that.
>
> I'd chalk it up to just a typo in the documentation except I've found
> several other tutorials with the same syntax. Also, the replace and delete
> functions are similarly confusing.
This isn't a typo, and there are no hash references involved in the code
snippet you showed.
There are two things occurring with the $ldap->add call. First, the
method is being passed a list of positional arguments, and key-value
pairs. This is somewhat atypical for a calling convention, but it can
make sense when one parameter is required.
It may be simpler to break it down to what different calls look like
from the function's perspective.
add('cn=Barb', attrs => ['sn' => 'Jensen']);
As mentioned in another response, => is effectively just a comma, though
it will quote the left hand operand. This is most likely the key
confusion you're having; => doesn't mean you're building a hash. What
this means is the function has been effectively called as:
add('cn=Barb', 'attrs', ['sn', 'Jensen']);
Which means add() is called with @_ assigned like so:
('cn=Barb', 'attrs', ['sn', 'Jensen'])
One means of handling this argument list, which is something like what
Net::LDAP is doing, is to assign things to a hash in order to access
arguments by name.
sub add {
my($dn, %kv) = @_;
...
}
Now, a call such as:
add('cn=Barb', 'attrs', ['sn', 'Jensen']);
Results in add() assigning the variables thusly:
$dn = 'cn=Barb';
$kv{'attrs'} = ['sn', 'Jensen'];
Finally, how Net::LDAP handles 'attrs' specifically requires special
mention. There's a reason it's an arrayref, and not a hashref; this is
because LDAP attributes can have multiple values, and with LDAP this is
usually represented as specifying the attribute more than once. So, the
two calls:
$ldap->add(
'cn=Barb',
attrs => [
cn => 'Barbara Jensen',
cn => 'Barbs Jensen',
],
);
And:
$ldap->add(
'cn=Barb',
attrs => [
cn => ['Barbara Jensen', 'Barbs Jensen']
],
);
Are treated by Net::LDAP as effectively identical. If the 'attrs'
argument were passed as a hashref then you would be required to use the
second form of call, as the behaviour of the hash would overwrite keys
of the same name on assignment.
$href = { cn => 'Barbara Jensen', cn => 'Barbs Jensen' };
Results in:
$href->{'cn'} = 'Barbs Jensen';
Does that clear things up for you?
Also, I understand your confusion and frustration. However, none of the
responses on this list were intended as malicious, degrading, or
anything other than honestly helpful. This is a small community of
local folks, not a large anonymous newsgroup where the instant response
is to flame you. Please take the responses in that light.
-- Michael Fowler www.shoebox.net --------- To unsubscribe, send email to <aklug-request@aklug.org> with 'unsubscribe' in the message body.Received on Fri May 27 12:01:47 2016
This archive was generated by hypermail 2.1.8 : Fri May 27 2016 - 12:01:48 AKDT