Is it possible to pass a subroutine to another subroutine?

Thread Solved

Join Date: Feb 2008
Posts: 32
Reputation: klactose is an unknown quantity at this point 
Solved Threads: 1
klactose klactose is offline Offline
Light Poster

Is it possible to pass a subroutine to another subroutine?

 
0
  #1
Sep 15th, 2008
Hello,

I am writing some testing scripts. At the end of each test I may have to return my environment back to it's default state using various subroutines in various modules. I realize that using the END block I can run these subroutines, however I have hundreds of these test to write and there is various formatting, etc, that is repetitious... so I want to include a single function in my END block that will accept other functions as parameters and then be able to run them from within itself.

A simple example would be:
  1. sub function_runner {
  2. my @exit_functions = @_;
  3. my $counter = 1;
  4. foreach &function (@exit_functions) {
  5. print "Performing exit function $counter:\n"; $counter++;
  6. &function;
  7. }
  8. print "\nAll exit functions completed.\n";
  9. }

I realize this will not work as is, but the gist of what I am trying do is to be able to have function_runner(function1, function2, function3, etc) in my end block. Any help point me in the right direction would be greatly appreciated.

Thanks!
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Is it possible to pass a subroutine to another subroutine?

 
1
  #2
Sep 16th, 2008
You could try using a reference to a function.

  1. sub some_function {
  2. print "foo";
  3. }
  4.  
  5. my $some_function = \&some_function;

You can pass $some_function like any other scalar, but you need to use the arrow operator to run the function.

  1. $some_function->()

Assuming you pass references to functions to your sub rotuine:

  1. sub function_runner {
  2. my @exit_functions = @_;
  3. my $counter = 1;
  4. foreach my $function (@exit_functions) {
  5. print "Performing exit function $counter:\n";
  6. $counter++;
  7. $function->();
  8. }
  9. print "\nAll exit functions completed.\n";
  10. }
Last edited by KevinADC; Sep 16th, 2008 at 12:06 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 32
Reputation: klactose is an unknown quantity at this point 
Solved Threads: 1
klactose klactose is offline Offline
Light Poster

Re: Is it possible to pass a subroutine to another subroutine?

 
0
  #3
Sep 22nd, 2008
Hey Kevin,

Just wanted to thank you. Passing references to the functions is exactly what I was trying to do, and it worked perfectly.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC