通常,我使用以下方法从文件加载解析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数据本身非常庞大,每个文件大约有10 or或更多。
*因此,我想要读取每个DN的文件,并对其前后进行比较。每个DN的解析都存储在$variable_before和$variable_after中。这就是为什么我实际上需要来自$变量的数据,因为'LDIF格式的数据‘来自于前一个进程的输出。*
我需要LDAP::LDIF使将LDIF字符串解析为perl hashref变得更容易。
我避免使用临时文件,因为"DN数据“真的很多,如果使用临时文件,处理速度会变慢。
发布于 2015-06-23 11:01:40
您可以将您拥有的数据附加到脚本的末尾,并从数据文件句柄( 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是您想要做的正确的模块吗?
发布于 2016-06-24 14:29:51
您可以打开一个标量参考文献:
Perl默认使用PerlIO构建。除非您对此进行了更改(例如使用配置-Uuseperlio构建Perl ),否则可以通过以下方式直接打开Perl标量的文件句柄:
open(my $fh, ">", \$variable) || ..
根据Net::LDAP::LDIF文档:
文件可以是文件的名称,也可以是已经打开的文件句柄。如果文件开始或结束时使用了一个\\,那么文件将被直接传递到打开。
所以,要回答你的问题:
open(my $string_fh, '<', $var_ldif) || die("failed to open: $?");
my $ldif = Net::LDAP::LDIF->new($string_fh, 'r', onerror => 'die');https://stackoverflow.com/questions/31000327
复制相似问题