Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reference subroutine as parameter

2 views
Skip to first unread message

Trevor Vallender

unread,
Dec 30, 2009, 9:54:23 AM12/30/09
to begi...@perl.org
Hi,

I have just moved a piece of code into a separate module. However, the
subroutine in the new module needs to reference a subroutine in the
script it's called from. Is there any way around this?

Basically, I am using AnyEvent::XMPP, and want the setup code in the
module. This code sets up which subroutines to call when certain events
occur, and all these will be in the calling script.

Can I pass the names of subroutines in the calling script to the
subroutine in the module somehow?

Thanks, and sorry if my question is a little jumbled, I'm having
problems explaining it clearly. Hopefully you understand what I mean.

Thank you,
Trevor Vallender

Trevor Vallender

unread,
Dec 30, 2009, 10:33:45 AM12/30/09
to John W. Krahn, Perl Beginners
On Wed, 2009-12-30 at 07:23 -0800, John W. Krahn wrote:
> Trevor Vallender wrote:
> > Hi,
>
> Hello,

>
> > I have just moved a piece of code into a separate module. However, the
> > subroutine in the new module needs to reference a subroutine in the
> > script it's called from. Is there any way around this?
> >
> > Basically, I am using AnyEvent::XMPP, and want the setup code in the
> > module. This code sets up which subroutines to call when certain events
> > occur, and all these will be in the calling script.
> >
> > Can I pass the names of subroutines in the calling script to the
> > subroutine in the module somehow?
>
> No, but you can pass a reference to a subroutine and then dereference it
> in the module. For example:
>
> $ perl -le'
> sub test { print "@_" }
> sub in_module {
> my $sub_ref = shift;
> $sub_ref->( "one", 2, "three", 4 );
> }
> in_module( \&test );
> '
> one 2 three 4
>
>
That works perfectly, exactly what I needed. Thank you!
>
>
> John
> --
> The programmer is fighting against the two most
> destructive forces in the universe: entropy and
> human stupidity. -- Damian Conway
>
Trevor.

Trevor Vallender

unread,
Dec 30, 2009, 10:25:35 AM12/30/09
to Jeremiah Foster, begi...@perl.org
On Wed, 2009-12-30 at 16:13 +0100, Jeremiah Foster wrote:
> Code snippets are always useful for clarifying your question.
>

I have a piece of code which sets subroutines to be called on certain
events:

$cl->reg_cb(
session_ready=>sub { sessionReady(@_); },
disconnect=>sub { disconnect(@_); },
error=>sub { error(@_); },
message=>sub { messageReceived(@_); }
);

I want this in a module. However, I want sessionReady() et al to be in
the calling code. Is there a way I can either have the module be okay
with this, and know that these subroutines will be present in the
calling code, or a way I can pass the subroutines to the module with
something like

setup(&sessionReady, &disconnect, &error, &messageReceived);

?

> Jeremiah
>

Trevor.

John W. Krahn

unread,
Dec 30, 2009, 10:23:28 AM12/30/09
to Perl Beginners
Trevor Vallender wrote:
> Hi,

Hello,

> I have just moved a piece of code into a separate module. However, the


> subroutine in the new module needs to reference a subroutine in the
> script it's called from. Is there any way around this?
>
> Basically, I am using AnyEvent::XMPP, and want the setup code in the
> module. This code sets up which subroutines to call when certain events
> occur, and all these will be in the calling script.
>
> Can I pass the names of subroutines in the calling script to the
> subroutine in the module somehow?

No, but you can pass a reference to a subroutine and then dereference it

in the module. For example:

$ perl -le'
sub test { print "@_" }
sub in_module {
my $sub_ref = shift;
$sub_ref->( "one", 2, "three", 4 );
}
in_module( \&test );
'
one 2 three 4

Jeremiah Foster

unread,
Dec 30, 2009, 10:13:25 AM12/30/09
to Trevor Vallender, begi...@perl.org

On Dec 30, 2009, at 15:54, Trevor Vallender wrote:

Code snippets are always useful for clarifying your question.

Jeremiah

0 new messages