Документация
HTML CSS PHP PERL другое

11.5 Incorporating Modules Effectively, Part 1

 
Previous Table of Contents Next

11.5 Incorporating Modules Effectively, Part 1

Let's break out CGI.pm and while we have it, we'll use its header() method in place of the hand-carved string that's been there so far.

While we're editing those lines we'll change the Perl 4-only || and && operators to their more natural or and and cousins, which look better where the intent is to change the flow of control. These changes only affect the first few lines of the program, which now look like this:


use CGI qw(param header);

use Net::LDAP;



print header;

my $rdn = 'ou=People, dc=wp, dc=emerald, dc=city, dc=oz';



my $filter;

foreach my $input (param)

{

  my $value = param($input);

  $filter and do_error("Cannot lookup by >1 attribute... pick one only");

  $filter = "($input=$value)";

}

$filter or do_error("Need an attribute to search on");

Now let's do some serious cleaning up and move as many constant values as possible into named symbols at the head of the program. The main program now looks like this:

Example 11.8. dirsearch.cgi, Version 6

1  #!/usr/bin/perl

2  use strict;

3  use warnings;

4

5  use CGI qw(param header);

6  use Net::LDAP;

7

8  print header;

9  my %SEARCH_OPTS =

10     (base  => 'ou=People, dc=wp, dc=emerald, dc=city, dc=oz',

11      scope => 'sub');

12 my $LDAP_SERVER = "whitepages";

13 my @LDAP_OPTS = ($LDAP_SERVER, timeout => 10);

14 my @ATTRS = qw(username location haircolor

15                telephone email fax name);

16

17 my $filter;

18 foreach my $input (param)

19 {

20   my $value = param($input);

21   $filter and do_error("Cannot lookup by >1 attribute... pick one only");

22   $filter = "($input=$value)";

23 }

24 $filter or do_error("Need an attribute to search on");

25

26 my $ldap = Net::LDAP->new(@LDAP_OPTS)

27   or do_email_error("Can't connect to $LDAP_SERVER: $@");

28 my $mesg = $ldap->bind;

29 $mesg->code and do_email_error("Bind error: "

30                                . $mesg->error);

31 my $res = $ldap->search(%SEARCH_OPTS, filter => $filter);

32 $res->code and do_email_error("Search failure: "

33                               . $res->error);

34

35 $res->count or do_error("No match for $filter");

36

37 print "<HTML><HEAD><TITLE>Search Results</TITLE></HEAD>\n";

38 print "<BODY><H1>Results of search for $filter</H1>\n";

39 print "<TABLE BORDER=\"1\"><TR>\n";

40 print map "<TH>$_</TH>", @ATTRS;

41 print "</TR>\n";

42 foreach my $ent ($res->entries)

43 {

44   print "<TR>", map "<TD>" . $ent->get_value($_)

45                            . "</TD>", @ATTRS;

46 }

47 print "</TR></TABLE></BODY></HTML>\n";

48

49 $ldap->unbind;

50

51 sub do_email_error

52 {

53   my $mess = shift;

54   do_email($mess);

55   do_error($mess);

56 }

57

58 sub do_error

59 {

60   my $mess = shift;

61   return unless $mess;

62   print "<HTML><HEAD><TITLE>Error</TITLE></HEAD><BODY>\n";

63   print "<H1>Error</H1><P>$mess</P>\n";

64   print "</BODY></HTML>\n";

65   exit;

66 }

67

68 sub do_email

69 {

70   my $mess = shift;

71   open MAIL, "|/usr/lib/sendmail -oi -t";

72   print MAIL "To: me\@here\n";

73   print MAIL "Subject: $0: LDAP lookup problem\n";

74   print MAIL "\n";

75   print MAIL "Error in LDAP processing in $0:\n\n";

76   print MAIL "$mess\n";

77   close MAIL;

78 }

    Previous Table of Contents Next
    © 2000- NIV