Dynamic method for determing single or multi-value attributes

--_000_F9EBEB8DE1C1184C9BB2BD64BD33A40809EAB1DSMMail01dsmnet_
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

Fellow LDAPers,

I have dozens of Net::LDAP, Entry, LDIF, Schema etc based scripts (Open Sou=
rce rocks!).

However, I keep finding myself in the same situation wishing I had a more d=
ynamic approach towards handling multi-valued attributes.

I suppose I could ALWAYS use array references even for attrs that I know ar=
e single valued: ie "$rarray =3D $entr->get_value->($some_specific_attr, as=
ref =3D> 1)".
Where the "{asref =3D> 1}" option forces an array ref for the returned valu=
e even if its only single valued.

But then I really don't want to deal with $$rarray[0], or if (scalar @$arra=
y =3D=3D 1) etc??

A little bit poking around the Net::LDAP::Schema seems to indicate that
  if (! exists $schema->attribute->('some_specifc_attr')->{single-value}) {
     # it's multi-valued - ie; the 'single-value' is set to '1' but I don't=
 see any set to zero, rather the key just doesn't exist if its multi-valued=
..
  else
   #its single valued.

But the schema is really big and I'd have to somehow cache/stash the small =
parts of this monster that I'd need throughout the lifetime of the script;-=
)

Today I typically have static constants files that are like:

our @multi_value_attrs =3D qw[mail, memberof, etc] and check this every tim=
e I fetch or process attributes.

if (some_specific_attr in @multi_value_attrs) {
   @answers =3D $entr->get_value('some_specific_attr'); #array
} else {
  $answer =3D $entr->get_value('some_specific_attr');  #scalar
}

Its very inelegant...

I see many posts out there explaining the whole "fetch multi-valued attribu=
tes as an array and single valued attributes as scalars" but no simple way =
of knowing dynamically which to expect.

Does anyone have an inexpensive function like:
$entr->is_multi_valued('some_specific_attr')?

Or maybe it's just my approach that is flawed?

Thanks all!
--Dan

--_000_F9EBEB8DE1C1184C9BB2BD64BD33A40809EAB1DSMMail01dsmnet_
Content-Type: text/html; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

<html xmlns:v=3D"urn:schemas-microsoft-com:vml" xmlns:o=3D"urn:schemas-micr=
osoft-com:office:office" xmlns:w=3D"urn:schemas-microsoft-com:office:word" =
xmlns:m=3D"http://schemas.microsoft.com/office/2004/12/omml" xmlns=3D"http:=
//www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dus-ascii"=
>
<meta name=3D"Generator" content=3D"Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
	{font-family:Calibri;
	panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin:0in;
	margin-bottom:.0001pt;
	font-size:11.0pt;
	font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
	{mso-style-priority:99;
	color:blue;
	text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
	{mso-style-priority:99;
	color:purple;
	text-decoration:underline;}
span.EmailStyle17
	{mso-style-type:personal-compose;
	font-family:"Calibri","sans-serif";
	color:windowtext;}
..MsoChpDefault
	{mso-style-type:export-only;
	font-family:"Calibri","sans-serif";}
@page WordSection1
	{size:8.5in 11.0in;
	margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
	{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext=3D"edit" spidmax=3D"1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext=3D"edit">
<o:idmap v:ext=3D"edit" data=3D"1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang=3D"EN-US" link=3D"blue" vlink=3D"purple">
<div class=3D"WordSection1">
<p class=3D"MsoNormal">Fellow LDAPers,<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">I have dozens of Net::LDAP, Entry, LDIF, Schema etc =
based scripts (Open Source rocks!).<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">However, I keep finding myself in the same situation=
 wishing I had a more dynamic approach towards handling multi-valued attrib=
utes.<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">I suppose I could ALWAYS use array references even f=
or attrs that I know are single valued: ie &#8220;$rarray =3D $entr-&gt;get=
_value-&gt;($some_specific_attr,
<b><span style=3D"color:red">asref =3D&gt; 1</span></b>)&#8221;.<o:p></o:p>=
</p>
<p class=3D"MsoNormal">Where the &#8220;{asref =3D&gt; 1}&#8221; option for=
ces an array ref for the returned value even if its only single valued.<o:p=
></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">But then I really don&#8217;t want to deal with $$ra=
rray[0], or if (scalar @$array =3D=3D 1) etc??<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">A little bit poking around the Net::LDAP::Schema see=
ms to indicate that
<o:p></o:p></p>
<p class=3D"MsoNormal">&nbsp;&nbsp;if (! exists $schema-&gt;attribute-&gt;(=
&#8216;some_specifc_attr&#8217;)-&gt;{single-value}) {<o:p></o:p></p>
<p class=3D"MsoNormal">&nbsp;&nbsp;&nbsp;&nbsp; # it&#8217;s multi-valued &=
#8211; ie; the &#8216;single-value&#8217; is set to &#8216;1&#8217; but I d=
on&#8217;t see any set to zero, rather the key just doesn&#8217;t exist if =
its multi-valued.<o:p></o:p></p>
<p class=3D"MsoNormal">&nbsp; else<o:p></o:p></p>
<p class=3D"MsoNormal">&nbsp;&nbsp; #its single valued.<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">But the schema is really big and I&#8217;d have to s=
omehow cache/stash the small parts of this monster that I&#8217;d need thro=
ughout the lifetime of the script;-)<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">Today I typically have static constants files that a=
re like:<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">our @multi_value_attrs =3D qw[mail, memberof, etc] a=
nd check this every time I fetch or process attributes.<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">if (some_specific_attr in @multi_value_attrs) {<o:p>=
</o:p></p>
<p class=3D"MsoNormal">&nbsp;&nbsp; @answers =3D $entr-&gt;get_value(&#8216=
;some_specific_attr&#8217;); #array<o:p></o:p></p>
<p class=3D"MsoNormal">} else {<o:p></o:p></p>
<p class=3D"MsoNormal">&nbsp; $answer =3D $entr-&gt;get_value(&#8216;some_s=
pecific_attr&#8217;);&nbsp; #scalar<o:p></o:p></p>
<p class=3D"MsoNormal">}<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">Its very inelegant&#8230;<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">I see many posts out there explaining the whole &#82=
20;fetch multi-valued attributes as an array and single valued attributes a=
s scalars&#8221; but no simple way of knowing dynamically which to expect.<=
o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">Does anyone have an inexpensive function like:<o:p><=
/o:p></p>
<p class=3D"MsoNormal">$entr-&gt;is_multi_valued(&#8216;some_specific_attr&=
#8217;)?<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">Or maybe it&#8217;s just my approach that is flawed?=
<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">Thanks all!<o:p></o:p></p>
<p class=3D"MsoNormal">--Dan<o:p></o:p></p>
</div>
</body>
</html>

--_000_F9EBEB8DE1C1184C9BB2BD64BD33A40809EAB1DSMMail01dsmnet_--
0
DCutler
10/11/2013 8:38:56 PM
perl.ldap 1268 articles. 0 followers. Follow

0 Replies
359 Views

Similar Articles

[PageSpeed] 10
Get it on Google Play
Get it on Apple App Store

Reply: