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);
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.