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

This may just be a pipe dream... I've created an array of Win32::Comboboxes, and I want to have "one sub function to rule them all..." (heh heh... couldn't resist). Basically, this is what I'm talking about...
foreach my $row (1..222){ foreach my $col (1..5){ if ($col =~ '1' && $account !~ $Sheet->Cells($row,$col)->{'Val +ue'}){ $account = $Sheet->Cells($row,$col)->{'Value'}; @labels[$count] = $main->AddLabel( -text => $account, -top => $top, -left => $left ); $top += 15; @comboboxes[$count] = $main->AddCombobox( -name => $account."ComboBox", -top => $top, -left => $left, -width => 125, -height => 150, -tabstop => 1, -style => WS_VISIBLE | 3 | WS_VSCROLL ); $count += 1; $top += 25; } else { if ($col =~ '2'){ $value = $Sheet->Cells($row,$col)->{'Value'}; @comboboxes[$count-1]->InsertItem($value); } } } }
and I want to have a sub function for the change of ANY of the comboboxes.... something like...
sub @comboboxes_Change{ print "You changed somethin'.\n"; }
That doesn't work, obviously. If I hard code in a sub function with something that I KNOW will be valid, like "ACCOUNTNAMEComboBox_Change", then it works fine. Any thoughts? Josh Pavel

Replies are listed 'Best First'.
Re: variable sub function?
by chromatic (Archbishop) on May 30, 2002 at 14:40 UTC
    A general rule of programming: if you want to store a number of distinct things, use an aggregate data type. A general rule of Perl programming: if you want to store several things and access them by name, use a hash.

    Use a hash of subroutine references.

    (As side notes, matching equality of a simple integer with a regular expression is, at best over kill. Assigning a single element to a list slice is, until Perl 6, deprecated. Incrementing $count to add elements to an array is usually better spelled push.)

    Update: Okay, my analysis ignores the realities of Win32::GUI. Disturbing.

      The problem here is that he is using Win32::GUI components. Win32::GUI has some strangaties. If you want to catch events, you need a sub for each event on each object. For example if you have an object named x and want to catch a change event, you will need to have a sub called x_Change. If you create another object called y, you will now need to create a sub called y_Change to catch change events for y. Unlike most GUI implementations, you can't register a sub for an event, the sub names are hard coded for each object.

      This was one of the many reasons I stopped using Win32::GUI.

Re: variable sub function?
by c-era (Curate) on May 30, 2002 at 14:26 UTC
    You could probably use typeglobs. Here is an example:
    sub mySub { print "this sub\n"; } *mySub2 = \&mySub; mySub2();
    And just repeat '*mySub2 = \&mySub' for each sub you need to setup (changing the name of mySub2).

    If you want to learn more about typeglobs, I recomend Advanced perl programming (there are a lot of other goodies in there as well).