Variable variable name

chromatic chromatic at wgz.org
Wed Jul 3 20:13:04 CDT 2002


On Wednesday 03 July 2002 16:14, Jason White wrote:

> Here is some code.
>
> my $functionToCall = "foo";
>
> ???????( )
>
> sub foo(){
>
>  print "FOO!\n";
>
> }
>
>
> How can I use the variable $functionToCall to replace ???????  with foo( )

You could look in the symbol table for a glob of the appropriate name and 
check to see if there's something in the CODE slot, but I sometimes prefer 
can():

	my $sub = UNIVERSAL::can( __PACKAGE__, $functionToCall );
	$sub->() if $sub and defined &$sub;

-- c
TIMTOWTDI



More information about the Pdx-pm-list mailing list