himik has asked for the wisdom of the Perl Monks concerning the following question:

Hello monkss. I have 22 functions like &func1_1, &func1_2, &func2_1 and etc. Here is what i want to use to see my purpose &func$number1_$number2 or something like $"func".$number1."_".$number2 can you help me? :))

Replies are listed 'Best First'.
Re: Dynamic functions
by kennethk (Abbot) on Sep 09, 2011 at 15:02 UTC
    The question is not entirely obvious to me (see I know what I mean. Why don't you?), but perhaps you would like to use a dispatch table, which is implemented with a hash of subroutine references:

    #!/usr/bin/perl -w use strict; my %dispatch = ( f11 => \&function11, f12 => \&function12, f21 => \&function21, f22 => \&function22, ); sub function11 { return "Function 1-1\n"; } sub function12 { return "Function 1-2\n"; } sub function21 { return "Function 2-1\n"; } sub function22 { return "Function 2-2\n"; } for my $i (1..2) { for my $j (1..2) { print $dispatch{"f$i$j"}->(); } }

    See perlref for more information on sub refs.

      Thank you that is what i search for.
Re: Dynamic functions
by BrowserUk (Patriarch) on Sep 09, 2011 at 15:14 UTC

    Remember that coderefs are just scalars and can be stored in any structure.

    If you need a 2D array of subs, set up exactly that:

    for my $i ( 1 .. 10 ){ for my $j ( 1.. 10 ) { $subs[ $i ][ $j ] = sub{ print 'You called sub_' . $i . '_' . +$j; }; } };; $subs[ 3 ][ 5 ]->();; You called sub_3_5

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Dynamic functions
by egga (Monk) on Sep 09, 2011 at 15:02 UTC

    I would use a dispatch table like the following. It's a little effort to set it up, but that prevents you from calling unforeseen functions for malicious or erroneous inputs:

    my %dispatch = ( 'func1_1' => \&func1_1, 'func1_2' => \&func1_2, 'func2_1' => \&func2_1, ); $dispatch{ 'func' . $number1 . '_' . $number2 }->( @params );
Re: Dynamic functions
by blue_cowdawg (Monsignor) on Sep 09, 2011 at 15:18 UTC

    Another way:

    #!/usr/bin/perl -w use strict; sub func_a_1 { print "A 1\n"; } sub func_a_2 { print "A 2\n"; } sub func_b_1 { print "B 1\n"; } sub func_b_2 { print "B 2\n"; } for my $n( qw/ a b /){ for my $i (1..2) { eval "&func_$n_$i()" } }

    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
      #!/usr/bin/perl -- use strict; use warnings; Main(@ARGV); exit(0); sub Main { my $package = __PACKAGE__; my $stash = do { no strict 'refs'; \%{ $package . '::' } }; print "$package\n$stash\n"; for my $symbol ( keys %$stash ) { next unless $symbol =~ /^func_/; print "\t$symbol\n"; #~ my $coderef = $package->can( $symbol ); # method/@ISA #~ my $coderef = *{ $stash->{$symbol} }{CODE}; my $coderef = \&{ $stash->{$symbol} }; print "\t$coderef\n\t\t", $coderef->(), "\n"; } ## end for my $symbol ( keys %$stash) } ## end sub Main sub func_a_1 { "A 1" } sub func_a_2 { "A 2" } sub func_b_1 { "B 1" } sub func_b_2 { "B 2" } __END__ main HASH(0x3f8b4c) func_a_1 CODE(0x9ad2c4) A 1 func_b_2 CODE(0x9d89fc) B 2 func_a_2 CODE(0x9ad474) A 2 func_b_1 CODE(0x9d898c) B 1