[aklug] Re: slightly OT: Perl Net::LDAP

From: David M. Syzdek <david@syzdek.net>
Date: Tue May 24 2016 - 10:47:41 AKDT

Todor,

I agree, with Royce. Talking down to someone who is attempting to help you
is unprofessional and to Royce's defense based upon your question is does
appear that you are not very familiar with some of the basics of using
function calls within perl modules.

Often the first few parameters to a module's function are set parameters
which are required, but may accept additional parameters which are optional
or more complex than simple scalars. So the LDAP example is doing
something similar to the following:

Here is my script to illustrate:

syzdek@sojourner$ cat test.pl
#!/usr/bin/perl -Tw
use Data::Dumper;
sub mock_up_add($%)
{
   my $dn = shift;
   my $param = {@_};
   printf("dn: %s\n", $dn);
   printf("%s\n", Dumper ($param));
}

mock_up_add( "cn=Barbara Jensen, o=University of Michigan, c=US",
   attrs => [
      'cn' => ['Barbara Jensen', 'Barbs Jensen'],
      'sn' => 'Jensen',
      'mail' => 'b.jensen@umich.edu',
      'objectclass' => ['top', 'person',
         'organizationalPerson',
         'inetOrgPerson' ],
   ]
);
# end of script
syzdek@sojourner$ ./test.pl
dn: cn=Barbara Jensen, o=University of Michigan, c=US
$VAR1 = {
          'attrs' => [
                       'cn',
                       [
                         'Barbara Jensen',
                         'Barbs Jensen'
                       ],
                       'sn',
                       'Jensen',
                       'mail',
                       'b.jensen@umich.edu',
                       'objectclass',
                       [
                         'top',
                         'person',
                         'organizationalPerson',
                         'inetOrgPerson'
                       ]
                     ]
        };
syzdek@sojourner$

Additionally, perl modules are often implemented as blessed objects so
there would be an additional shift statement for the module itself:

sub mock_up_add($%)
{
   my $self = shift;
   my $dn = shift;
   my $param = {@_};
   ...

--David M. Syzdek

On Tue, May 24, 2016 at 7:45 AM, Royce Williams <royce@tycho.org> wrote:

> On Tue, May 24, 2016 at 7:37 AM, Todor Fassl <fassl.tod@gmail.com> wrote:
> >
> > On 05/20/2016 01:10 PM, Royce Williams wrote:
> >>
> >> On Fri, May 20, 2016 at 9:49 AM, Todor Fassl <fassl.tod@gmail.com>
> wrote:
> >>>
> >>> Essentially, my question is, in perl, what is the difference between
> >>> saying:
> >>>
> >>> $array = ['one' => 1, 'two' => 2];
> >>>
> >>> and
> >>>
> >>> $array = {'one' => 1, 'two' => 2};
> >>>
> >>> I am trying to write an ldap add function in perl. Something in the
> >>> documentation puzzles me:
> >>> http://search.cpan.org/~gbarr/perl-ldap-0.33/lib/Net/LDAP.pod
> >>>
> >>> That page shows the second parameter to the add function being a
> anonymous
> >>> array ref created by using brackets, '[ ... ]'.
> >>>
> >>> But then it appears to use string keys. Doesn't that make it a hash or
> an
> >>> associative array? Therefore, shouldn't it be instantiated with
> braces, "{
> >>> ... }"?
> >>>
> >>> --
> >>> Tod
> >>
> >>
> >> Braces mean a hash reference.
> >>
> >> http://stackoverflow.com/a/11839874
> >
> >
> > Dude, isn't it obvious from my question that I know that braces mean a
> hash reference?
>
> No. Your original message began with what appeared to be a summary of
> your question that seemed to indicate that you did not. I deal with
> high volumes of email, and I answered as best as I could with the
> information that I had absorbed -- admittedly, by reading too quickly,
> but nevertheless in good faith.
>
> Also, please drop the "dude, isn't it obvious". We try to be more
> polite than this in this forum. It's not the way to treat people who
> are trying to help you.
>
> > My question is how can the Net::LDAP module use brackets to instantiate
> a normal array reference and then, apparently, use it as a hash reference?
> I wrote some code to check the validity of my question:
> >
> > #!/usr/bin/perl
> > use Data::Dumper;
> > $junk = {'apple' => 1, 'banana' => 6, 'cherry' => 99};
> > print "TYPE=" . ref($junk) . "\n" . Dumper ($junk);
> >
> > As expected, this gives the following output:
> >
> > TYPE=HASH
> > $VAR1 = {
> > 'banana' => 6,
> > 'cherry' => 99,
> > 'apple' => 1
> > };
> >
> > So far so good. Same code except the braces for the hash reference are
> changed to brackets:
> >
> > #!/usr/bin/perl
> > use Data::Dumper;
> > $junk = ['apple' => 1, 'banana' => 6, 'cherry' => 99];
> > print "TYPE=" . ref($junk) . "\n" . Dumper ($junk);
> >
> > This displays the following:
> >
> > TYPE=ARRAY
> > $VAR1 = [
> > 'apple',
> > 1,
> > 'banana',
> > 6,
> > 'cherry',
> > 99
> > ];
> >
> > I suspect that in perl, if you treat a normal array reference as a hash
> reference, it just deals with it. But that seems really sloppy. I can't be
> messing around with my ldap database.
>
>
> I don't have the combo of cycles and competence to delve into this one
> -- maybe someone else can field it.
>
> Royce
> ---------
> To unsubscribe, send email to <aklug-request@aklug.org>
> with 'unsubscribe' in the message body.
>
>

-- 
"I'm religious but not spiritual."
        --Cardinal Francis George, O.M.I.
---------
To unsubscribe, send email to <aklug-request@aklug.org>
with 'unsubscribe' in the message body.
Received on Tue May 24 10:48:06 2016

This archive was generated by hypermail 2.1.8 : Tue May 24 2016 - 10:48:06 AKDT