943,907 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Marked Solved
  • Views: 1880
  • Perl RSS
Sep 15th, 2008
0

Is it possible to pass a subroutine to another subroutine?

Expand Post »
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:
perl Syntax (Toggle Plain Text)
  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!
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
klactose is offline Offline
43 posts
since Feb 2008
Sep 16th, 2008
1

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

You could try using a reference to a function.

Perl Syntax (Toggle Plain Text)
  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.

Perl Syntax (Toggle Plain Text)
  1. $some_function->()

Assuming you pass references to functions to your sub rotuine:

Perl Syntax (Toggle Plain Text)
  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.
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Sep 22nd, 2008
0

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

Hey Kevin,

Just wanted to thank you. Passing references to the functions is exactly what I was trying to do, and it worked perfectly.
Reputation Points: 10
Solved Threads: 1
Light Poster
klactose is offline Offline
43 posts
since Feb 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: where would i find this information?
Next Thread in Perl Forum Timeline: How to open directory ?!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC