I'm writing some LDAP programs and have started out by cutting & pasting from POD examples. I've generalized the following code somewhat but think the result can be improved. I have to test whether $entry->get('var') is defined or the program dies with the following message:
Can't use an undefined value as an ARRAY reference at ldap.test2.pl li +ne xx.
So the question is, is there an idiom that would simplify or at least make the following code (particulary within the for loop) easier to read? BTW, the code below runs with no errors or warnings and the results appear to be correct.
use strict; use warnings; use Net::LDAP; use Net::LDAP::Util qw / ldap_error_text /; use Net::LDAP::Schema; use Net::LDAP::Entry; use Net::LDAP::Search; use Data::Dumper; ## snip my @vars = qw( cn mail ); ## this will have about 30 fields eventuall +y my %uids; for my $entry ($searchresult->entries) { my $uid = ''; $uid = ${$entry->get('uid')}[0] if defined $entry->get('uid'); if ($uid) { for (@vars) { $uids{$uid}{$_} = ''; $uids{$uid}{$_} = ${$entry->get($_)}[0] if defined $entry->get($_); } } else { print "*** No uid for the following entry:\n"; print Dumper(\$entry); } }
Thanks for any and all feedback.

In reply to About BLOCK variables by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.