use warnings; use strict; sub work { my ($x, $y, $callback) = @_; if (ref $callback ne 'CODE'){ die "callback param needs to be a code reference"; } $callback->($x, $y); } # with a ref to an existing sub sub this { my ($x, $y) = @_; print "$x $y\n"; } work(3, 4, \&this); # with an explicit code ref my $cref = sub { print "$_\n" for @_; }; work(2, 2, $cref); # or inline work(2, 3, sub { print "$_[0], $_[1]\n"; }) #### my $regex = qr/$self->{regex}/; while(<$input>){ my $result = /$regex/; $self->{callback_ref}($result,$line,$output); }