<div class="gmail_quote">On Fri, Feb 24, 2012 at 10:36 AM, Bill Brush <span dir="ltr"><<a href="mailto:bbrush@gmail.com">bbrush@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hello everyone.  I just recently started down the path of learning<br>
Perl, and I'm working on my first script (program?  which is the<br>
correct term for Perl?).<br>
<br>
Anyway, I'm basing it on the example here:<br>
<a href="http://www.developer.com/open/article.php/10930_3106601_3/Searching-Active-Directory-with-Perl.htm" target="_blank">http://www.developer.com/open/article.php/10930_3106601_3/Searching-Active-Directory-with-Perl.htm</a><br>

<br>
Getting down to the details of the code, I want to throw out some<br>
lines and how I interpret them to make sure I'm reading them right.<br>
<br>
This line seems to be the money line of the example:<br>
<br>
my $results = $ad->search(base=>$base,filter=>$filter,attrs=>$attrs);<br>
<br>
The way I read that is:<br>
<br>
my $results  =  (the output of this command will be stored in $results)<br>
<br>
$ad->search   (Use the search method in the previously created object $ad)<br>
<br>
(base=>$base,filter=>$filter,attrs=>$attrs)  (The search method has 3<br>
inputs required [base,filter, attrs] which are stored in the 3<br>
variables.  Base corresponds to the LDAP context where the search will<br>
be performed, filter gives the criteria of the objects selected, and<br>
attrs gives the attributes to be returned.  Presumably these could<br>
have been written out explicitly rather than stored in variables, but<br>
this is infinitely more readable and flexible.)<br>
<br>
So am I reading that command correctly?<br></blockquote><div><br>Yes. <br> </div><blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Another question about the example script is minor, but I haven't<br>
found the answer elsewhere.  The author uses the operator .= (dot<br>
equal).  What does that do?<br></blockquote><div> </div><div>$x = 'x';<br>$x .= 'X'; <br><br>is short had for<br><br>$x = $x . 'X';<br><br>both store 'xX' after execution.<br> </div><blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">

A final question is more of a code style and efficiency question.  The<br>
author uses a FOR-NEXT loop to cycle through the alphabet.  Wouldn't a<br>
While (letter<z) or Until (letter =z) loop structure be more concise?<br>
I'm trying to understand if there's an advantage to the author's<br>
method that I'm not grasping.<br></blockquote><div><br>I would propose rather than incrementing a letter index you could just use <br>'a'..'z' range operator.<br><br>This is how I would code the example.<br>
<br>#!/usr/bin/perl<br>use Modern::Perl;<br>use Net::LDAP;<br><br># Connect and bind<br>my $ad = Net::LDAP->new("<a href="http://ad.wjgilmore.com">ad.wjgilmore.com</a>") || die "Could not connect!";<br>
$ad->bind( '<a href="mailto:ad-web@ad.wjgilmore.com">ad-web@ad.wjgilmore.com</a>', password => 'secret' );<br><br># build the alphabetical toc<br>my $toc = "\n\n" . join( ' ', a .. z ) . " \n";<br>
<br># Perform LDAP queries, build directory pages<br>my $base = 'OU=People,OU=staff,DC=ad,DC=wjgilmore,DC=com';<br>for my $letter ( a .. z ) {<br><br>    # Filter on the staff membership and<br>    # first letter of samaccountname attribute<br>
    my $filter = "(&(memberof=CN=staff,OU=groups,DC=ad,DC=wjgilmore,DC=com)(samaccountname=$letter*))";<br><br>    # Which attributes should be returned?<br>    my $attrs = "sn, givenname, mail";<br>
<br>    # Execute the search<br>    my $results = $ad->search( base => $base, filter => $filter, attrs => $attrs );<br>    <br>    # Check for errors!<br>    $results->code && die $results->error;<br>
    <br>    # Build the directory<br>    my $directory;<br>    for my $entry ( $results->entries ) {<br>        $directory .= sprintf '%s %s (%s)%s', $entry->get_value('givenname'), $entry->get_value('sn'),<br>
            $entry->get_value('mail'), "\n";<br>    }<br><br>    # Write the file<br>    open( FILE, '>', "/www/wjgilmore/directory/$letter.html" );<br>    say FILE $toc, $directory;<br>
    close FILE;<br>}<br><br># Unbind from the server<br>$ad->unbind;<br></div></div># END<br><br>-- <br>Ted Katseres<br>      ||=O=||<br>