I will try to explain it clearer. Currently you can read utf8 atributes out
of the box by using the "raW" option in the constructor:

    $ldap = Net::LDAP->new(
                              $server, 
                              port      =>  $port,
                              raw       =>  qr/not_utf_attrib/,
                          );

so you can look for entries, and all the attributes will be converted
automatically from utf8 (except "not_utf_attrib" attribute):

    $mesg = $ldap->search(
                             base    =>  "c=US",
                             filter  =>  "(&(sn=Barr) (o=Texas Instruments))",
                         );
    # Treat here entry attributes in $mesg without worrying on utf8 values.

But if you create an entry, you will have to manually convert all attributes 
into utf8 (except "not_utf_attrib" attribute):

    for my $c (keys %$attrs)
    {
        utf8::encode($attrs->{$c})
            if (not ref $attrs->{$c}  and  $c ne 'not_utf_attrib');
    }

    $mesg = $ldap->add( $dn, attrs => [ @$attrs ] );

This manual conversion should be unnecesary, since Net::LDAP already knows the
attributes that must be treated as utf8 and the attributes that mustn't (with
the "raw" option), so they could be converted automatically by Net::LDAP.

So I'm proposing to add an option (raw_for_writing) to the constructor, that 
will indicate Net::LDAP to automatically convert attributes into utf8 when 
creating/modifing entries:

    $ldap = Net::LDAP->new(
                              $server,
                              port              =>  $port,
                              raw               =>  qr/not_utf_attrib/,
                              raw_for_writing   =>  1,
                          );

Thank you


( Off topic: There is a delay, after a posted mail appears in the web page
http://www.nntp.perl.org/group/perl.ldap/2015/08.html . That is why I sent my
first post twice, I thought that subscription was required, since the mail
didn't appear in the web page )


25.08.2015, 20:42, "pe rl" <pe...@yandex.com>:
> Hi, we are using an old version of Net::LDAP (0.39) in an old perl
> installation (5.10.1). Recently we have changed the ldap server, and now it
> uses utf8 in the entry attributes, so we are getting problems with reading and
> writting attributes with Net::LDAP.
>
> To solve it, I have read the documentation of Net::LDAP 0.39 ( at
> https://metacpan.org/pod/release/GBARR/perl-ldap-0.39/lib/Net/LDAP.pod ), and
> I see there is an option ("raw") in the constructor to indicate attributes
> that should be treated as utf8. I have tested it, and it works por reading
> from the ldap server (attribute strings are marked as utf8, so they a treated
> correctly by our programs), but it doesn't work for writting (our latin1
> strings are not being converted automatically into utf8 before being sent).
>
> So it looks like the "raw" option works for reading but not for writting. Is
> there any quick way to use the "raw" regex also for writting? The alternative
> would be to review all of our code and manually encode all the values to utf8
> before passing them to Net::LDAP, but it would mean a lot of work. It would be
> better if we could change the Net::LDAP library itself to convert
> automatically attributes into utf8, same as for reading. For example, a new
> option "raw_for_writing" could be added to the constructor:
>
>       $ldap = Net::LDAP->new(
>                                 $server,
>                                 port => $port,
>                                 raw => qr/(?i:^jpegPhoto|;binary)/,
>                                 raw_for_writing => 1,
>                             )
>
> I see that the automatic conversion for reading is done at the "decode"
> function of "Entry.pm":
>
>     sub decode {
>
>       ...
>
>       if (CHECK_UTF8 && $arg{raw}) {
>         $result->{objectName} = Encode::decode_utf8($result->{objectName})
>           if ('dn' !~ /$arg{raw}/);
>
>       ...
>
>         foreach my $elem (@{$self->{asn}{attributes}}) {
>           map { $_ = Encode::decode_utf8($_) } @{$elem->{vals}}
>             if ($elem->{type} !~ /$arg{raw}/);
>         }
>       }
>
> And I see that there is an "encode" function in "Entry.pm", that doesn't do
> the magic:
>
>     sub encode {
>       $LDAPEntry->encode( shift->{asn} );
>     }
>
> Would it be sufficient to add some similar code to the "Entry::encode"
> function in order to automatically encode attributes to utf8 before being 
> sent?
>
> Any suggestion to reduce the amount of code to be changed in our programs?
>
> Thank you

Reply via email to