I would tend to use the second option. I think explicitly passing in args, having as few globals as possible, and having to write fewer subroutines would be a good thing. But its not the only options. You could go OO and hide all the parameters in, e.g. a hashref and just call the subs as methods, like
$dir_server->modify_directory. Or you could use closures to write the subs with some fixed parameters for each directory server, and leave the rest as args to pass in.
Looking at the docs, the new() method is the only one that returns undef and
returns an error message in $@; for the rest, you have
to examine the returned Message object. An OO example:
package DirServer;
sub new {
my $class = shift;
my ($dir, $dir_manager, $pass) = @_;
my $ldap = Net::LDAP->new($dir) or die "Couldn't connect: $@\n";
my $result = $ldap->bind($dir_manager, password=>$pass);
$result->code && die "Couldn't bind".$result->error;
bless \$ldap, $class;
}
sub modify_directory{
my $ldap = ${shift()};
my ($action_to_be_taken, $entry_to_be_adjusted,
$attribute_to_be_adjusted, $value_to_adjust_with) = @_;
my $result = $ldap -> modify($entry_to_be_adjusted,
$action_to_be_taken =>
{$attribute_to_be_adjusted => $entry_to_be_adjusted});
$result->code && die "Ya screwed up".$result->error;
}
# Then...
package main;
my $dir_server = DirServer->new(@args);
$dir_server->modify_directory(@more_args);
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.