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

From: Todor Fassl <fassl.tod@gmail.com>
Date: Tue May 24 2016 - 07:37:13 AKDT

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? 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.
---------
To unsubscribe, send email to <aklug-request@aklug.org>
with 'unsubscribe' in the message body.
Received on Tue May 24 07:37:39 2016

This archive was generated by hypermail 2.1.8 : Tue May 24 2016 - 07:37:39 AKDT