Jonathan Mast <[EMAIL PROTECTED]> asked:
> ok, how do I do that?
Braindead minimal example:
#!/usr/bin/perl -w
use strict;
package Sample::Callback;
# constructor takes a function reference as argument
sub new {
my( $class, $callback ) = @_;
my $self = { 'cb' => $callback };
bless $self, $class;
return $self;
}
# the single method of this class invokes the callback
# syntax note: $coderef->() to dereference a coderef.
sub invoke_callback {
my( $self, @args ) = @_;
$self->{'cb'}->( @args );
}
1;
package main;
# named function to pass into the constructor
sub callback_func {
my @args = @_;
print "callback args: " , join( ', ', @args );
}
# illustrates taking a reference from a function name
my $obj1 = Sample::Callback->new( \&callback_func );
$obj1->invoke_callback( qw(Hello World!) );
# same thing with an anonymous coderef
# syntax note: my $coderef = sub { code; here; };
my $obj2 = Sample::Callback->new( sub { print reverse @_, "\n" } );
$obj2->invoke_callback( qw(Hello World!) );
__END__
HTH,
Thomas
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/