help with callbacks (?)

D

dan

hi,

Firstly, I'm not sure if this post is about callbacks, but here goes.

I have two modules, in moduleA I have

$self->{_coderef} = undef; # and later on...
$self->{_coderef}->sayhello if defined $self->{_coderef};

moduleB contains the subroutine

sub sayhello {
print 'hello';
}

Now I can write a script that says

my $testA = new moduleA;
my $testB = new moduleB;
$testA->{_coderef} = $testB;

Now the code

$self->{_coderef}->sayhello if defined $self->{_coderef};

prints 'hello'.

OK so far so good, but now I wonder if there is a way I can change
moduleA so that it does not contain the string 'sayhello'. Instead the
script passes the subroutine name to moduleA. I have tried various things
but so far no luck.

Any ideas? Sorry no actual code.

dan
 
S

sln

hi,

Firstly, I'm not sure if this post is about callbacks, but here goes.

I have two modules, in moduleA I have

$self->{_coderef} = undef; # and later on...
$self->{_coderef}->sayhello if defined $self->{_coderef};

moduleB contains the subroutine

sub sayhello {
print 'hello';
}

Now I can write a script that says

my $testA = new moduleA;
my $testB = new moduleB;
$testA->{_coderef} = $testB;

Now the code

$self->{_coderef}->sayhello if defined $self->{_coderef};

prints 'hello'.

OK so far so good, but now I wonder if there is a way I can change
moduleA so that it does not contain the string 'sayhello'. Instead the
script passes the subroutine name to moduleA. I have tried various things
but so far no luck.

Any ideas? Sorry no actual code.

dan

From ModuleA:
$self->{_coderef}->sayhello

is really:

$testB->sayhello()


So Dumper $self->{_coderef} would probably reveal an
instance object $testB, of class type ModuleB.

Then the actual call would be ModuleB::sayhello($testB)

This is a runtime where $self->{_coderef} should contain a
instance of that type class.

Maybe it could be done in an eval, I don't know. There is
probably a way.

Did you try something like this?
$call = "\$self->{_coderef}->$methodname";
eval $call;

Not sure.
 
J

Jim Gibson

dan said:
hi,

Firstly, I'm not sure if this post is about callbacks, but here goes.

I have two modules, in moduleA I have

$self->{_coderef} = undef; # and later on...
$self->{_coderef}->sayhello if defined $self->{_coderef};

moduleB contains the subroutine

sub sayhello {
print 'hello';
}

Now I can write a script that says

my $testA = new moduleA;
my $testB = new moduleB;
$testA->{_coderef} = $testB;

Now the code

$self->{_coderef}->sayhello if defined $self->{_coderef};

prints 'hello'.

OK so far so good, but now I wonder if there is a way I can change
moduleA so that it does not contain the string 'sayhello'. Instead the
script passes the subroutine name to moduleA. I have tried various things
but so far no luck.

Any ideas? Sorry no actual code.

You might be looking for "AUTOLOAD". See 'perldoc perlsub' and search
for "Autoloading".
 
S

sln

hi,

Firstly, I'm not sure if this post is about callbacks, but here goes.

I have two modules, in moduleA I have

$self->{_coderef} = undef; # and later on...
$self->{_coderef}->sayhello if defined $self->{_coderef};

moduleB contains the subroutine

sub sayhello {
print 'hello';
}

Now I can write a script that says

my $testA = new moduleA;
my $testB = new moduleB;
$testA->{_coderef} = $testB;

Now the code

$self->{_coderef}->sayhello if defined $self->{_coderef};

prints 'hello'.

OK so far so good, but now I wonder if there is a way I can change
moduleA so that it does not contain the string 'sayhello'. Instead the
script passes the subroutine name to moduleA. I have tried various things
but so far no luck.

Any ideas? Sorry no actual code.

dan

If you know what your doing, you might be able to finnesse that with
typeglobs. Read perlmod.

sln
 
X

xhoster

dan said:
hi,

Firstly, I'm not sure if this post is about callbacks, but here goes.

I have two modules, in moduleA I have

$self->{_coderef} = undef; # and later on...
$self->{_coderef}->sayhello if defined $self->{_coderef};

I guess that could be considered a callback. What you have is a method
invocation, the method being invoked on an object stored in a hash under
the key "_coderef" (which is rather confusing, because it is an object ref,
not a coderef.)

Now I can write a script that says

my $testA = new moduleA;
my $testB = new moduleB;
$testA->{_coderef} = $testB;

Now the code

$self->{_coderef}->sayhello if defined $self->{_coderef};

prints 'hello'.

OK so far so good, but now I wonder if there is a way I can change
moduleA so that it does not contain the string 'sayhello'. Instead the
script passes the subroutine name to moduleA.

my $testA = new moduleA;
my $testB = new moduleB;
$testA->{_object} = $testB;
$testA->{_method_name} = "sayhello";

my $x=$self->{_method_name};
$self->{_object}->$x if defined $self->{_object};

I don't know how to get rid if the intermediate variable ($x), you need a
simple scalar to use it as a method name, or it can't parse properly.

Another way would to use a real code ref, which is a closure over
the desired object and the desired method to apply to that object:

$testA->{_coderef} = sub {$testB->sayhello};

Now _coderef really is a code ref.

$self->{_coderef}->() if defined $self->{_coderef};

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,211
Messages
2,571,100
Members
47,695
Latest member
KayleneBee

Latest Threads

Top