First thing is that ldap_bind() is returning in the next-to-last statement, so $ldap->bind is never actually called.

The better alternative to sticking values in global variables is to pass needed parameters to each sub and return what each sub produces. The mechanism is simple. Perl subs receive their arguments in the @_ array, and they return either the arguments of the first return statement they encounter, or else the value of the last statement evaluated.

Here is a minimal modification to your code to follow those principles,

#!/usr/bin/perl -w use strict; use warnings; use diagnostics; use Net::LDAP; ldap_search( qw/Neil Hunt/); sub ldap_search { my ($givenname, $sn) = @_; my ($ldap, $mesg) = ldap_bind(); my $base="ou=People,dc=btester,dc=com,dc=au"; $mesg = $ldap->search ( base => $base, filter => "(&(cn=$givenname $sn))"); if ($mesg->count == 0) { print "Hi!\n"; } else { print "Content-type: text/html\n\n"; print "<title>Already Exists</title>"; print "$givenname $sn already exists"; print "<p>"; print "Go <a href=\"../addemp.html\">Back</a>"; } 1; } sub ldap_bind{ my $ldap_server="argyle"; my $user="cn=Directory Manager"; my $pass="<password>"; my $ldap = Net::LDAP->new( $ldap_server ) or die "$@"; my $mesg = $ldap->bind( "cn=Directory Manager", password => "secretsecret" ); return ($ldap, $mesg); }
Doing that is likely to point up places to improve your code. I was tempted to make $ldap_server, $user, $pass arguments to ldap_bind, but it seemed just as likely that the sub was a desired specialization and that no fixing was needed.

Your code (and my modified version) really needs some error checking code added. You don't know whether some of your LDAP operations have succeeded or not.

After Compline,
Zaxo


In reply to Re: Passing parameters between subroutines by Zaxo
in thread Passing parameters between subroutines by neilh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.