--On Tuesday, July 06, 2004 10:19 AM +0100 Graham Barr <[EMAIL PROTECTED]> wrote:

On 5 Jul 2004, at 21:52, Quanah Gibson-Mount wrote:
I'm curious if anyone would be interested in a

$mesg->as_hash;

function.  We currently have this implemented in an overlay perl
module we use (Stanford::Directory) to make using returned results
simpler for our client base.

Essentially, this allows you to do things like:

$uid = ($entry->uid)[0];
$displayName = ($entry->displayName);

Where "uid" is a multi-valued attribute, so uid[0] is the first value
of the uid attribute.

I think this generally makes for easily readable and writeable output.

I would be interested in seeing what you did. But I suspect as_hash is not the right name. as_hash is probably a better name for the as_struct method that is already there.

In fact I suspect that it can be done by blessing the result of as_struct
into a package which has only an AUTOLOAD method. Although that would not
be very efficient

Hm, my current method isn't pretty, and not likely efficient either. ;)

my @entries;
$status = $ld->search(base=>$self->basedn, scope=>$self->scope, filter=>$_, attrs=>[EMAIL PROTECTED]);
my @entrys;
my $ent;
@entrys=$status->all_entries;
foreach $ent (@entrys) {
my $entry = Stanford::Directory::Entry->new();
foreach my $attr ($ent->attributes()) {
my $key = lc($attr);
$entry->$key($ent->get_value($attr));
}
push (@entries, $entry);
}


And then here is Stanford::Directory::Entry

package Stanford::Directory::Entry;

use strict;
use vars qw($AUTOLOAD);

# new (constructor) - just bless the hash

sub new {
   my $class = shift;
   my $self = {};
   bless $self, $class;
   return $self;
}

# AUTOLOAD - used to get and set attributes

sub AUTOLOAD {
   my $self = shift;

   my $name = $AUTOLOAD;
   $name =~ s/.*:://;               # strip fully-qualified portion
   return if $name =~ /DESTROY/;    # don't do DESTROY
   $name = lc($name);

   push(@{$self->{$name}},@_) if (@_);

   return (exists $self->{$name}) ? @{$self->{$name}} : ();
}

--
Quanah Gibson-Mount
Principal Software Developer
ITSS/Shared Services
Stanford University
GnuPG Public Key: http://www.stanford.edu/~quanah/pgp.html

Reply via email to