in reply to Re^4: How to call subroutines using variables ?
in thread How to call subroutines using variables ?
Worked great, but I did have to get rid of the & in front of my subroutine names. So:
my @all_subs = (\sub_1,\sub_2);
In which sense did you "have" to? That is something completely different.
#!/usr/bin/perl -l use strict; use warnings; my $obj=bless {}, 'Foo'; my @allsubs=(\&sub1,\&sub2); my $code=$allsubs[rand @allsubs]; $obj->$code(qw/foo bar/); sub sub1 { my $caller=shift; print "Caller is $caller, this is sub1, args are <@_>"; } sub sub2 { my $caller=shift; print "Caller is $caller, this is sub2"; } __END__
|
|---|