Dumping Net::LDAP::Entry to a string

Heiko Jansen <[email protected]>
Newsgroups gmane.comp.lang.perl.modules.ldap
Organization hbz NRW
Message-ID <[email protected]>
Hi there,

using Net::LDAP I had a problem when I wanted to hand of a stringified version 
of a single result record (object of type Net::LDAP::Entry): it seems like 
it's only possible to dump a record directly to a file handle 
(Net::LDAP::Entry->dump)?!

So in case anyone else finds this useful (and in case I didn't miss something) 
I've appended a small patch to Net/LDAP/Entry.pm (and Net/LDAP/Entry.pod) 
which provides a dumpstr() method to return a stringified record.

Heiko
dumpstr.patch (text/x-patch, 1.5 KB)
--- Entry.pm.orig	2010-02-04 18:01:33.000000000 +0100
+++ Entry.pm	2010-02-04 18:09:15.000000000 +0100
@@ -17,7 +17,7 @@
 }
 
 
-$VERSION = "0.24";
+$VERSION = "0.25";
 
 sub new {
   my $self = shift;
@@ -295,12 +295,25 @@
 
 sub dump {
   my $self = shift;
+
   no strict 'refs'; # select may return a GLOB name
   my $fh = @_ ? shift : select;
 
+  print $fh $self->dumpstr();
+
+  return;
+}
+
+
+sub dumpstr {
+  my $self = shift;
+
   my $asn = $self->{asn};
-  print $fh "-" x 72,"\n";
-  print $fh "dn:",$asn->{objectName},"\n\n" if $asn->{objectName};
+
+  my $string = '';
+
+  $string .= "-" x 72 . "\n";
+  $string .= "dn:" . $asn->{objectName} . "\n\n" if $asn->{objectName};
 
   my($attr,$val);
   my $l = 0;
@@ -313,15 +326,17 @@
 
   foreach $attr (@{$asn->{attributes}}) {
     $val = $attr->{vals};
-    printf $fh "%${l}s: ", $attr->{type};
+    $string .= sprintf "%${l}s: ", $attr->{type};
     my($i,$v);
     $i = 0;
     foreach $v (@$val) {
-      print $fh $spc if $i++;
-      print $fh $v;
+      $string .= $spc if $i++;
+      $string .= $v;
     }
-    print $fh "\n";
+    $string .= "\n";
   }
+
+  return $string;
 }
 
 sub attributes {
--- Entry.pod.orig	2010-02-04 18:03:28.000000000 +0100
+++ Entry.pod	2010-02-04 18:04:40.000000000 +0100
@@ -228,6 +228,12 @@
 
 If C<FILEHANDLE> is omitted C<STDOUT> is used by default.
 
+=item dumpstr ( )
+
+Dump the entry to a string.
+
+This method is intended for debugging purposes and does not
+treat binary attributes specially.
 
 =item exists ( ATTR )
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.