通常,我使用以下方法从文件中加载解析 ldif:
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;
}
但我需要直接从变量字符串加载 LDIF 格式文件,而不是从文件加载。所以代码看起来像:
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;
}
那么,如何正确地做到这一点呢?
感谢并抱歉提出这个愚蠢的问题。 :) BR//
背景想法 我的目标是构建详细比较前后 LDIF 数据的脚本(从 dn 到属性值,一一比较)。 LDIF 数据本身非常巨大,每个文件大约 10GB 或更多。
*所以我想到了读取每个 DN 的文件,并比较之前和之后的情况。每个 DN 的解析存储在 $variable_before 和 $variable_after 中。这就是为什么我实际上需要来自 $ 变量的数据,因为“LDIF 格式数据”来自先前过程的输出。 *
我需要 LDAP::LDIF 来更轻松地将 LDIF 字符串解析为 Perl hashref。
我避免使用临时文件,因为“DN数据”确实很多,如果使用临时文件,处理速度会更慢。
最佳答案
您可以将您拥有的数据附加到脚本末尾并从 DATA 文件句柄读取(Net::LDAP::LDIF 文档规定第一个参数可以是文件名或文件句柄)
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
另一个解决方案是将 $var_ldif 的内容写入临时文件。
您确定 NET::LDAP::LDIF 是适合您想要执行的操作的模块吗?
关于perl Net::LDAP::LDIF 从变量字符串中读取 ldif,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31000327/