| LDIF(3pm) | User Contributed Perl Documentation | LDIF(3pm) |
Mozilla::LDAP::LDIF - read or write LDIF (LDAP Data Interchange Format)
use Mozilla::LDAP::LDIF
qw(set_Entry get_LDIF put_LDIF unpack_LDIF pack_LDIF
sort_attributes references enlist_values delist_values
read_v1 read_v0 read_file_URL_or_name);
$ldif = Mozilla::LDAP::LDIF->new(*FILEHANDLE, \&read_reference, $comments);
@record = get $ldif;
@records = get $ldif ($maximum_number);
$entry = set_Entry (\entry, \@record);
$entry = readOneEntry $ldif;
@entries = readEntries $ldif ($maximum_number);
$ldif = Mozilla::LDAP::LDIF->new(*FILEHANDLE, $options);
$success = put $ldif (@record);
$success = put $ldif (\@record, \object ...);
$success = writeOneEntry $ldif (\entry);
$success = writeEntries $ldif (\entry, \entry ...);
@record = get_LDIF (*FILEHANDLE, $eof, \&read_reference, $comments);
@record = get_LDIF; # *STDIN
$success = put_LDIF (*FILEHANDLE, $options, @record);
$success = put_LDIF (*FILEHANDLE, $options, \@record, \object ...);
@record = unpack_LDIF ($string, \&read_reference, $comments);
$string = pack_LDIF ($options, @record);
$string = pack_LDIF ($options, \@record, \object ...);
@record = enlist_values (@record);
@record = delist_values (@record);
@record = sort_attributes (@record);
$DN = LDIF_get_DN (@record); # alias get_DN
@DNS = LDIF_get_DN (\@record, \object ...); # alias get_DN
$offset = next_attribute (\@record, $offset, @options);
@references = references (@record);
@references = references (\@record, \object ...);
$success = read_v1 (\$url); # alias read_file_URL
$success = read_v0 (\$name); # alias read_file_name
$success = read_file_URL_or_name (\$url_or_name);
MIME::Base64, Exporter, Carp
Put the LDIF.pm file into a subdirectory named Mozilla/LDAP, in one of the directories named in @INC. site_perl is a good choice.
Nothing (unless you request it).
LDIF version 1 is defined by <draft-good-ldap-ldif-03>. An LDIF record like this:
DN: cn=Foo Bar, o=ITU
cn: Foo Bar
Sn: Bar
objectClass: person
objectClass: organizatio
nalPerson
jpegPhoto:< file:foobar.jpg
# comment
corresponds (in this module) to a Perl array like this:
(DN => "cn=Foo Bar, o=ITU",
cn => "Foo Bar",
Sn => "Bar",
objectClass => [ "person", "organizationalPerson" ],
jpegPhoto => \"file:foobar.jpg",
'# comment', undef
)
URLs or file names are read by a separate function. This module provides functions to read a file name (LDIF version 0) or a file URL that names a local file (minimal LDIF version 1), or either. You can supply a similar function to read other forms of URL.
Most output and utility methods in this module accept a parameter list that is either an LDIF array (the first item is a string, usually "dn"), or a list of references, with each reference pointing to either an LDIF array or an object from which this module can get LDIF arrays by calling the object's getLDIFrecords method. This module calls $object->getLDIFrecords(), expecting it to return a list of references to LDIF arrays. getLDIFrecords may return references to the object's own data, although it should not return references to anything that will be modified as a side-effect of another call to getLDIFrecords(), on any object.
If \&read_reference is defined, call it when reading each reference to another data source, with ${$_[$[]} equal to the reference. The function should copy the referent (for example, the contents of the named file) into $_[$[].
Ignore LDIF comment lines, unless $comments eq "comments".
If *FILEHANDLE is not defined, read from *STDIN.
If $eof is passed, set it true if the end of the given file was encountered; otherwise set it false. This function may set $eof false and also return a record (if the record was terminated by the end of file).
If \&read_reference is defined, call it when reading each reference to another data source, with ${$_[$[]} equal to the reference. The function should copy the referent (for example, the contents of the named file) into $_[$[].
Ignore LDIF comment lines, unless $comments eq "comments".
If \&read_reference is defined, call it when reading each reference to another data source, with ${$_[$[]} equal to the reference. The function should copy the referent (for example, the contents of the named file) into $_[$[].
Ignore LDIF comment lines, unless $comments eq "comments".
This implements LDIF version 1, although it doesn't support URLs that refer to anything but a local file (e.g. HTTP or FTP URLs).
This implements LDIF version 0.
Default: 0 (output is not broken into continuation lines).
Default: "^[:< ]|[^ -\x7E]"
For example:
pack_LDIF ([encode=>"^ |[^ -\xFD]"], @record)
returns a string in which UTF-8 strings are not encoded (unless they begin with a space or contain control characters) and lines are not continued. Such a string may be easier to view or edit than standard LDIF, although it's more prone to be garbled when sent in email or processed by software designed for ASCII. It can be parsed without loss of information (by unpack_LDIF).
Default: "\n" (the usual line separator, for output text).
enlist_values (givenName => "Joe", givenname => "Joey", GivenName => "Joseph")
returns
(givenName => ["Joe", "Joey", "Joseph"])
If the parameter list is a single record, return a single record; otherwise return a list of references to records.
delist_values (givenName => ["Joe", "Joey", "Joseph"])
returns
(givenName => "Joe", givenName => "Joey", givenName => "Joseph")
If the parameter list is a single record, return a single record; otherwise return a list of references to records.
In either case, the expression may be a string, which is simply evaluated (using eval), or a reference to a subroutine, which is called with $_ as its only parameter. The value returned by eval or the subroutine is taken as the result of evaluation.
If no options are given, the offset of the next attribute is returned.
Option expressions can modify the record, since they are passed an alias to an element of the record. An option can selectively prevent the evaluation of subsequent options: options are evaluated in the order they appear in the @options list, and if an option evaluates to FALSE, subsequent options are not evaluated.
use Mozilla::LDAP::LDIF qw(read_file_URL_or_name);
$in = Mozilla::LDAP::LDIF->new(*STDIN, \&read_file_URL_or_name);
$out = Mozilla::LDAP::LDIF->new(*STDOUT, 78);
@records = get $in (undef); # read to end of file (^D)
put $out (@records);
use Mozilla::LDAP::Conn();
$conn = Mozilla::LDAP::Conn->new(...);
while ($entry = readOneEntry $in) {
add $conn ($entry);
}
use Mozilla::LDAP::LDIF qw(get_LDIF put_LDIF
references read_v1 next_attribute sort_attributes);
while (@record = get_LDIF (*STDIN, $eof)) {
# Resolve all the file URLs:
foreach my $r (references (@record)) {
read_v1 ($$r);
}
# Capitalize all the attribute names:
for ($r = undef; defined ($r = next_attribute (\@record, $r)); ) {
$record[$r] = ucfirst $record[$r];
}
# Capitalize all the title values:
next_attribute (\@record, undef,
type => '"title" eq lc $_',
value => '$_ = ucfirst; 0');
# Sort the attributes and output the record, 78 characters per line:
put_LDIF (*STDOUT, 78, sort_attributes (@record));
last if $eof;
}
This bug arises when handling standard LDIF received 'raw' via the Internet (via HTTP, for example). There's no problem with an input file that has been converted (as generic text) from standard Internet line separators to $/ (that is, the usual line separator for the local platform).
John Kristian <kristian@netscape.com>
Thanks to Leif Hedstrom, from whose code I took ideas; and to the users who took the trouble to correct my mistakes. But I accept all blame.
Mozilla::LDAP::Entry, Mozilla::LDAP::Conn, and of course Perl.
| 2024-03-31 | perl v5.38.2 |