jasonl has asked for the wisdom of the Perl Monks concerning the following question:

Using Net::LDAP, I'm running into a problem trying to deal with multiple instances of an attribute. It appears that they get stored in a hash as an array of arrays; in the initial hash ($valref), I can dump the values like this:

@pktClassList = @$valref{packetclassifierlist}; print "class = "; foreach my $i (@pktClassList) { foreach my $j (@$i) { print "$j\n"; } }

Unfortunately I need to get @pktClassList to another sub as a member of a different hash. I'm assigning it to the hash in a switch statement like this:

... case /packetclassifierlist/ { $tlvHash{pktClassList} = "@pktClassLi +st" } ...

I then pass $tlvHash to my new sub as "newSub($tlv,%tlvHash);". In newSub I'm doing this:

... my ($tlv, %tlvHash) = @_; ... my $classArray = $tlvHash{pktClassList}; foreach my $i (@$classArray) { print "i = $i\n"; foreach my $j (@$i) { print "j = $j\n"; } }

I've tried various permutations of @, $, and @$ around classArray and tlvHash, but if the code executes at all, the best I get is "Can't use string ("ARRAY(0xa3f6e4)") as an ARRAY ref while "strict refs" in use...." I'm really trying to stick with packing this into %tlvHash instead of passing it as a separate arg, because multiple functions are calling newSub and they won't all have this attribute.

Replies are listed 'Best First'.
Re: problems dereferencing an array of arrays as a hash value
by gmargo (Hermit) on Nov 23, 2009 at 16:54 UTC

    How about a new array reference, like this?

    case /packetclassifierlist/ { $tlvHash{pktClassList} = [ @pktClassL +ist ] }
      Thanks! That did it, but I'm not quite clear why. Can you explain a little what's going on there?

        The brackets [] create a reference to an anonymous array. With an array inside the brackets like [ @pktClassList ], the contents of the @pktClassList array are copied into the new anonymous array.

        There is a different way to get the same effect, but which avoids the second copy. If the @pktClassList array is a local variable in a subroutine, which gets used only once before going out of scope, then you could have used the @pktClassList array directly like this:

        case /packetclassifierlist/ { $tlvHash{pktClassList} = \@pktClassLi +st }

        The key is the scope of the original @pktClassList array - when the variable goes out of scope, it becomes an anonymous array by virtue of being referenced by the %tlvHash hash. Quite unlike the C language.

        See perlref for more info on references. Anonymous arrays are addressed early in the document.

Re: problems dereferencing an array of arrays as a hash value
by toolic (Bishop) on Nov 23, 2009 at 16:54 UTC
    I'm having a hard time understanding your question, but a few things look suspicious:

    { $tlvHash{pktClassList} = "@pktClassList" }
    Do you really want to inject spaces between array elements here? If not, get rid of the quotes.

    Try to eliminate the switch/case statement for debugging purposes, as Switch has many unusual side-effects.

    Show your data using Data::Dumper. That might clarify things:

    use Data::Dumper; print Dumper(\%tlvHash);
      If he gets rid of quotes, he gets number of elements :)