| | |
Is it possible to pass a subroutine to another subroutine?
Thread Solved
![]() |
•
•
Join Date: Feb 2008
Posts: 28
Reputation:
Solved Threads: 1
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:
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!
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)
sub function_runner { my @exit_functions = @_; my $counter = 1; foreach &function (@exit_functions) { print "Performing exit function $counter:\n"; $counter++; &function; } print "\nAll exit functions completed.\n"; }
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!
You could try using a reference to a function.
You can pass $some_function like any other scalar, but you need to use the arrow operator to run the function.
Assuming you pass references to functions to your sub rotuine:
Perl Syntax (Toggle Plain Text)
sub some_function { print "foo"; } 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)
$some_function->()
Assuming you pass references to functions to your sub rotuine:
Perl Syntax (Toggle Plain Text)
sub function_runner { my @exit_functions = @_; my $counter = 1; foreach my $function (@exit_functions) { print "Performing exit function $counter:\n"; $counter++; $function->(); } print "\nAll exit functions completed.\n"; }
Last edited by KevinADC; Sep 16th, 2008 at 12:06 am.
![]() |
Similar Threads
- passing values from a form to a dialog form (VB.NET)
- Tutorial: Understanding ASP classes (ASP)
- ASP.NET Registration Page (ASP.NET)
- ASP.NET wizard control (ASP.NET)
- Help: passing variable via table field different page subroutine (ASP)
- SubRoutine/Function Code Split (VB.NET)
- modify script (Perl)
- Simple ASP.Net Login Page (Using VB.Net) (ASP.NET)
- Pop3 Mail Watcher (Part 1) (Visual Basic 4 / 5 / 6)
- Populating & Retrieving Data in a listbox : ASP.NET (w/ VB.NET) (ASP.NET)
Other Threads in the Perl Forum
- Previous Thread: where would i find this information?
- Next Thread: How to open directory ?!
| Thread Tools | Search this Thread |





