• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • Last Modified:

how to write a subroutine that takes a subroutine as a parameter, executes it, but prints out the name

sub dispatch($doIt, $sub) {
  if (!doIt) { return; }
  if ($debuglevel > 1) {
# RIGHT HERE, how do I print the name of the subroutine?
  }
  &$sub(); # execute the subroutine
}



dispatch(1, \&copyIn);
0
dbkruger
Asked:
dbkruger
  • 2
1 Solution
 
ozoCommented:
You can't in general, the sub you pass may not have a name
dispatch(1, sub{print "annonymous sub"})
but you could also pass it a name
dispatch(1,"dispatch")
0
 
ozoCommented:
sub dispatch($doIt, $sub) { is not the proper syntax for subroutine prototypes
see
perldoc perlsub
0
 
dbkrugerAuthor Commented:
sorry, I was tired:

sub dispatch {
  my ($doIt, $sub) = @_;

the question still stands.
0
 
mjcoyneCommented:
How about the built-in function "caller" (see http://perldoc.perl.org/functions/caller.html)?  Or a module on CPAN, like the DB module (http://search.cpan.org/dist/perl/lib/DB.pm) or Hook::LexWrap (http://search.cpan.org/dist/Hook-LexWrap/lib/Hook/LexWrap.pm)?
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now