Re: Stealing a good idea from Gtk#



* Torsten Schoenfeld <kaffeetisch gmx de> [2004-09-26 23:45]:
  use Foo qw(Gtk2::Button Gtk2::Widget);

  $button -> Clicked += \&clicked;

  $window -> DeleteEvent += sub {
    Gtk2 -> main_quit();
    return FALSE;
  };

Cool, eh?  That looks even better than the Gtk# stuff.  I just
love Perl.

It's definitely cool. Three things irk me, though:

    a) += just isn't used that way in Perl, and I think
       overloading its meaning the way its done here is too
       idiosyncratic.

    b) There is no indication that we're doing something with a
       signal here, not just using any old mutator.

    c) StudlyCaps, particularly as a vehicle to solve namespace
       clashes.

As far as a) is concerned, the really Perlish way would of course
be a tied array that you can push subrefs onto and delete from...
but ehm, that would be overengineered. :)

I'm thinking the operator should be a simple assignment, in which
case you can switch from overload to a tied interface, too.

And as far as indication *and* StudlyCaps are concerned, how
about a magic SIGNAL package?

Put together with some magic from CPAN, it leads to astonishingly
little code. Try this on for size..

    package SIGNAL;
    use strict;
    use warnings;
    use Tie::Simple; # CPAN module

    sub AUTOLOAD : lvalue {
        my $obj = shift;
        my ( $sig ) = ( our $AUTOLOAD ) =~ /^.*::(.*)$/;
        my $connect = sub { $obj->signal_connect( $sig => $_[0] ) };
        tie my $curry, Tie::Simple => undef, STORE => $connect;
        return $curry;
    }

    1;

A little bit simpler than your code, isn't it. :)

It is completely untested, but if I didn't screw it up too
thoroughly, you should now be able to say 

    $window->SIGNAL::delete_event = sub {
        Gtk2->main_quit();
        return FALSE;
    };

To me, that looks much nicer than your use samples. No
StudlyCaps, clear indication that we're working with a signal
which is something special, and simple assignment.

Regards,
-- 
Aristotle
"If you can't laugh at yourself, you don't take life seriously enough."



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]