perl Net::LDAP::LDIF read ldif from variable string

1.1k views Asked by At

Usually, I load parse ldif from file using following method:

use Net::LDAP::LDIF;
use Net::LDAP::Entry;
use Data::Dumper;

my $ldif = Net::LDAP::LDIF->new( "filename.ldif", "r") or die "file not exits\n";
while( not $ldif->eof ( ) ) {
    $entry = $ldif->read_entry ( );
    print Dumper $entry;
}

but instead of load from file, I need load the LDIF format file directly from variable string. so the code will look like :

use Net::LDAP::LDIF;
use Net::LDAP::Entry;
use Data::Dumper;

my $var_ldif = "dn: cn=Sheri Smith,ou=people,dc=example,dc=com
objectclass: inetOrgPerson
cn: Sheri Smith
sn: smith
uid: ssmith
userpassword: sSmitH
carlicense: HERCAR 125
homephone: 555-111-2225";

my $ldif = Net::LDAP::LDIF->new( $var_ldif, "r") or die "file not exits\n";
while( not $ldif->eof ( ) ) {
    $entry = $ldif->read_entry ( );
    print Dumper $entry;
}

so, how to do it corectly?

thanks and sorry for this stupid question. :) BR//

BACKGROUND IDEA My goal is to build script that compare LDIF data before and after in detail (from dn until attribute values, one by one). the LDIF data itself is really huge, about 10GB or more per file.

*So I come with idea to read the file per-DN, and compare it before and after. parsing of each DN is stored in $variable_before and $variable_after. that is why I actually need data from $ variable, because the 'LDIF format data' is come from output from previous process. *

I need LDAP::LDIF to make easier to parse LDIF string into perl hashref.

I avoid using temporary file because the "DN data" is really much, will slower in processing if using temporary file.

2

There are 2 answers

1
Georgi Rangelov On

You can append the data you have to the end of the script and read from the DATA filehandle ( Net::LDAP::LDIF documentation states the first parameter can be a filename or a filehandle )

use Net::LDAP::LDIF;
use Net::LDAP::Entry;
use Data::Dumper;

my $ldif = Net::LDAP::LDIF->new( *DATA, "r") or die "file not exits\n";
while( not $ldif->eof ( ) ) {
    $entry = $ldif->read_entry ( );
    print Dumper $entry;
}

__DATA__
dn: cn=Sheri Smith,ou=people,dc=example,dc=com
objectclass: inetOrgPerson
cn: Sheri Smith
sn: smith
uid: ssmith
userpassword: sSmitH
carlicense: HERCAR 125
homephone: 555-111-2225

Another solution is to write the contents of the $var_ldif to a temporary file.

Are you sure NET::LDAP::LDIF is the right module for what you want to do ?

0
Lucas On

You can open a SCALAR ref:

Perl is built using PerlIO by default. Unless you've changed this (such as building Perl with Configure -Uuseperlio ), you can open filehandles directly to Perl scalars via:

open(my $fh, ">", \$variable) || ..

And per the Net::LDAP::LDIF documentation:

FILE may be the name of a file or an already open filehandle. If FILE begins or ends with a | then FILE will be passed directly to open.

So, to answer your question:

open(my $string_fh, '<', $var_ldif) || die("failed to open: $?");
my $ldif = Net::LDAP::LDIF->new($string_fh, 'r', onerror => 'die');