While I have not tested, the use of "eval" should work if you play with it 
enough.  I perform something simular with building a list of menu items based 
off a "grepped" list of unique server names from a Database query.  These are 
inserted into the Menu at runtime so if a new servername is added to the 
database records, it just "works".  However, it DID take a while for me to get 
it to work to make sure that things were scoped properly.   Additionally, since 
it is evalled, you will not catch errors in your sub definition until runtime 
clicking the button that fires the event.  I would suggest starting with the 
event just calling something like a one line function like print or msgbox 
until you get results.  Then define more of the sub in bits and pieces with 
lots of testing in between.

It is all rather nasty looking code, but it works very well(except there is not 
really an easy way to create accelorator keys for the menu items since it is 
dynamic..)

sub build_Menu_Subs
{
        #my %server_menu = %{+shift};
        #my %version_menu = %{+shift};
        my $count = 1;
        foreach my $b (keys %server_menu)
        {
                no strict;
                my $Estring = qq|sub $b|;
                $Estring .= qq|_Click{|;
                $Estring .= qq|selectServer("$b");\n|;
                $Estring .= qq|print "$b" . "_Click\n";\n}|;
                eval $Estring;
                $count++;

        }
        $count =1;
        foreach my $b (sort (keys %version_menu))
        {
                no strict;
                my $Estring = qq|sub $b|;
                $Estring .= qq|_Click{\n|;
                $Estring .= qq|selectVersion("$b");\n|;
                $Estring .= qq|print "$b" . "_Click\n";\n}|;
                eval $Estring;
                $count++;
        }
}

Joe Frazier, Jr.
Technical Support Engineer
Peopleclick Service Support

Tel:  +1-800-841-2365
E-Mail: mailto:[EMAIL PROTECTED]


> -----Original Message-----
> From: Jeremy White [mailto:[EMAIL PROTECTED]
> Sent: Friday, May 16, 2003 10:48 AM
> To: [EMAIL PROTECTED]; perl-win32-gui-users@lists.sourceforge.net
> Subject: Re: [perl-win32-gui-users] How to define generic handler for
> buttons
> 
> 
> I know I'm going to regret replying with out testing my codeJ
> 
> The way I handle this kind of thing is to eval the handler 
> for each object, 
> which in turn calls a generic handler, something like this:
> 
>   foreach my $button ('one','two','three','four') {
>     my $string='sub ::'.$button.'_Click { 
> ::Generic_ButtonClick('."'$button'".');}';
>     eval $string;
>     print "$string \n";
>     die 'error with eval '.$@ if $@;
>     }
> 
> 
>   sub ::Generic_ButtonClick {
>     my $button=shift;
>     print "$button clicked";
>   }
> 
> Hopefully that will make some sence,
> 
> cheers,
> 
> jez.
> >From: Gerard ter Haar <[EMAIL PROTECTED]>
> >To: perl-win32-gui-users@lists.sourceforge.net
> >Subject: [perl-win32-gui-users] How to define  generic 
> handler for buttons
> >Date: Fri, 16 May 2003 14:12:15 GMT
> >
> >Hi,
> >
> >I have just started with Win32::GUI. I want to create a 
> small application 
> >which starts a
> >main window with a lot of buttons on it. The buttons are read from a 
> >config-file.
> >
> >So far so good, the visual creation works just fine! However 
> I now have a 
> >lot of
> >dynamically created buttons and therefore can't add all 
> >'<ButtonName>_Click' functions to
> >my application.
> >
> >Can I define a general Button-handler? Based on the ButtonName I can 
> >determine what to do.
> >
> >Please advice, I haven't found anything is the FAQ/etc.
> >
> >Thanks and kind regards,
> >
> >gerard ter haar
> >
> >
> >
> >
> >-------------------------------------------------------
> >Enterprise Linux Forum Conference & Expo, June 4-6, 2003, Santa Clara
> >The only event dedicated to issues related to Linux 
> enterprise solutions
> >www.enterpriselinuxforum.com
> >
> >_______________________________________________
> >Perl-Win32-GUI-Users mailing list
> >Perl-Win32-GUI-Users@lists.sourceforge.net
> >https://lists.sourceforge.net/lists/listinfo/perl-win32-gui-users
> 
> _________________________________________________________________
> Get Hotmail on your mobile phone http://www.msn.co.uk/msnmobile
> 
> 
> 
> -------------------------------------------------------
> Enterprise Linux Forum Conference & Expo, June 4-6, 2003, Santa Clara
> The only event dedicated to issues related to Linux 
> enterprise solutions
> www.enterpriselinuxforum.com
> 
> _______________________________________________
> Perl-Win32-GUI-Users mailing list
> Perl-Win32-GUI-Users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/perl-win32-gui-users
> 
> 

Reply via email to