Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is this calling a function: $actions{$page}->();

Status
Not open for further replies.

bobbybobbertson

Programmer
Nov 9, 2001
119
US
I found this line in some code and don't understand it.
Is it calling a function using variables?

$actions{$page}->();

 
You are almost correct. It's calling a function using a reference to a subroutine. The value of the key contained in [tt]$page[/tt] is a reference to a subroutine. If you declared a hash like
Code:
my %actions = (  page1  => \&go_page1,
                 page2  => \&go_page2,
            );
And then set [tt]$page = 'page1'[/tt],
you line of code would call the [tt]go_page1()[/tt] function.

This is just like regular sub references:
Code:
$subref = \&mysub;
# Or 
$subref = sub { ... (sub def here) .. };
# Then call as
$subref->();

jaa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top