G'day,
I'm having problems with two subroutines using the same variables, and I'm not sure how to proceed.
When I declare the variables globally, it all works, but I have been told this is not the Right Way.
The code is as follows:
#!/usr/bin/perl -w
use strict;
use warnings;
use diagnostics;
use Net::LDAP;
my $givenname="Neil";
my $sn="Hunt";
my ($ldap,$mesg);
&ldap_search;
sub ldap_search {
&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>";
}
}
sub ldap_bind{
my $ldap_server="argyle";
my $user="cn=Directory Manager";
my $pass="<password>";
return $ldap = Net::LDAP->new( $ldap_server ) or die "$@";
return $mesg = $ldap->bind( "cn=Directory Manager",
password => "secretsecret"
);
}
This code is part of a larger programme which is for adding and updating users in LDAP. The ldap_bind() sub needs to be called multiple times depending on the situation the script is in.
Update:
Thanks Zaxo. I applied the changes you suggested. Not only does my code work nicely, I learnt so much more about how a subroutine works.
Thanks!
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.