Oliver.Doerr has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm searching for a part of wisdom, that I was not able to find in the Internet. So it is to good hidden for me;-)

So what I'm trying to build is a perl module that does a lot of work for various perl scripts for me. However there is one little kind of code (a simple parser), that has to be provided by the calling script. So I have code like this inside the script as parser...

sub parseGroupDescription { my $groupObj=shift; # my problem $groupObj is always undef / $g +roupObj is Net::LDAP:Entry my $groupName=get_attribute($groupObj,"erldapservicegroup"); my $groupDesc=get_attribute($groupObj,"description"); my $response; $response->{"name"}=$groupName; return $response; } # sub parseGroupDescription
I'm calling the method inside my main script using
my $data=$zbdbObj->fetchData($ldapConn,$serviceFilter,$groupFilter,sub + {parseGroupDescription});
The perl module has code like
sub fetchData { my $selfObj=shift; my $ldapConn=shift; my $serviceFilter=shift; my $groupFilter=shift; my $parseFunction=shift; ... foreach my $group ($allGroups->entries) { print get_attribute($group,"erldapservicegroup")." "; my $response=&$parseFunction->($group); # $group is a Net:: +LDAP:Entry object and $parseFunction a reference on the function of t +he main script my $groupName=$response->{"name"}; $serviceDataObj->{"groups"}->{$groupName}=$groupName; } # foreach my $group ($allGroups->entries) } # sub fetchData

I could see that my parser is called within this construct, however I was not able to find a way to pass the value of $groupObj to the parser.

Any ideas?

Regards

Oliver

PS: This is a simplyfied version of my code

Replies are listed 'Best First'.
Re: Problems Passing an object to a function that was passed to a module
by haj (Vicar) on May 03, 2021 at 08:44 UTC

    Your construct is creating an anonymous sub {parseGroupDescription} which gets into the way - it doesn't pass the value it receives to parseGroupDescription.

    A quick guess: Just replace sub {parseGroupDescription} by a direct reference to your named sub:

    my $data=$zbdbObj->fetchData($ldapConn,$serviceFilter,$groupFilter,\&parseGroupDescription)
      This hint solved the problem. Thanks a lot Oliver