Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Problems Passing an object to a function that was passed to a module

by Oliver.Doerr (Initiate)
on May 03, 2021 at 08:33 UTC ( #11131967=perlquestion: print w/replies, xml ) Need Help??

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


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?



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 (Curate) 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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11131967]
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2021-05-11 05:35 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (113 votes). Check out past polls.