In article <[EMAIL PROTECTED]>,
 [EMAIL PROTECTED] (Tim Musson) writes:
>Hey all,
>
>   I am using the perl-ldap module (to access MS AD, not that that
>   matters to this question though :-).
>
>   My problem is I don't understand part of the example code - it all
>   works just fine, but using the "as_struct" method, I need to just
>   display the "cn" attribute (I need to write the attributes in a
>   specific order, so have to be able to write just one of the returned
>   list - I can change what is returned with no problem).
>
>   The code I am looking at is here
>   < 
> http://search.cpan.org/~gbarr/perl-ldap/lib/Net/LDAP/Examples.pod#PROCESSING_Displaying_SEARCH_Results
>  >
>   and the specific part is
>
>foreach $attrName (@arrayOfAttrs) {
>
>        # skip any binary data: yuck!
>        next if ( $attrName =~ /;binary$/ );
>
>        # get the attribute value (pointer) using the
>        # attribute name as the hash
>        my $attrVal =  @$valref{$attrName} ;
>        print "\t $attrName: @$attrVal \n";
>}
>
>   I was thinking (and the documents say) these are references, but I
>   have had a hard time getting my head around references...
>
>   I have tried adding this
>       print @$valref{cn};
>   but all it prints is
>       ARRAY(0x25d9ec4)

That's because you missed out the next line, where the value from (the
equivalent of) @$valref{cn} is dereferenced again with @$attrVal.

I don't know why the dereferencing is done in the example with 
@$valref{$attrName}; that's needlessly complicated (single element slice
of hash via reference?!).  It'd make more sense to use -> :

    print $valref->{cn}->[0];

(Assuming you have only one cn, as most people do.)

In fact, you can get it right from the search result.
$result->as_struct returns a reference to a hash keyed by DN, with values being
references to hashes keyed by attribute name, whose values are references to
arrays of attribute values.  So:

my $href = $result->as_struct;
for my $dn (keys %$href) {
  print "cn of $dn is $href->{$dn}{cn}[0]\n";
}

(That's taking advantage of the fact that you can leave out -> between
closing and opening {} or [].)

-- 
Peter Scott
http://www.perldebugged.com



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to