Class::Std::Slots

Provide signals and slots for standard classes
Download

Class::Std::Slots Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Andy Armstrong
  • Publisher web site:
  • http://search.cpan.org/~andya/

Class::Std::Slots Tags


Class::Std::Slots Description

Provide signals and slots for standard classes Class::Std::Slots is a Perl module that provides signals and slots for standard classes.Conventionally the ways in which objects of different classes can interact with each other is designed into those classes; changes to that behaviour require either changes to the classes in question or the creation of subclasses.Signals and slots allow objects to be wired together dynamically at run time in ways that weren't necessarily anticipated by the designers of the classes. For example consider a class that manages time consuming downloads: package My::Downloader; use Class::Std; { sub do_download { my $self = shift; # ... do something time consuming ... } }For a particular application it might be desirable to be able to display a progress report as the download progresses. Unfortunately My::Downloader isn't wired to allow that. We could improve My::Downloader by providing a stub function that's called periodically during a download: package My::Downloader::Better; use Class::Std; { sub progress { # do nothing } sub do_download { my $self = shift; # ... do something time consuming periodically calling progress() ... } }Then we could subclass My::Downloader::Better to update a display: package My::Downloader::Verbose; use base qw(My::Downloader::Better); use Class::Std; { sub progress { my $self = shift; my $done = shift; print "$done % done "; } }That's not bad - but we had to create a subclass - and we'd have to arrange for it to be created instead of a My::Downloader::Better anytime we want to use it. If displaying the progress involved updating a progress bar in a GUI we'd need to embed a reference to the progress bar in each instance of My::Downloader::Verbose.Instead we could extend My::Downloader::Better to call an arbitrary callback via a supplied code reference each time progress() was called ... but then we have to implement the interface that allows the callback to be defined. If we also want notifications of retries and server failures we'll need still more callbacks. Tedious.SYNOPSIS package My::Class::One; use Class::Std; use Class::Std::Slots; { signals qw( my_signal ); sub my_slot { my $self = shift; print "my_slot triggered "; } sub do_stuff { my $self = shift; print "Doing stuff... "; $self->my_signal; # send signal print "Done stuff. "; } } package My::Class::Two; use Class::Std; use Class::Std::Slots; { signals qw( another_signal ); sub another_slot { my $self = shift; print "another_slot triggered "; $self->another_signal; } } package main; my $ob1 = My::Class::One->new(); my $ob2 = My::Class::Two->new(); # No signal yet $ob1->do_stuff; # Connect to a slot in another class $ob1->connect('my_signal', $ob2, 'another_slot'); # Emits signal $ob1->do_stuff; # Connect an anon sub as well $ob1->connect('my_signal', sub { print "I'm anon... "; }); # Emits signal invoking two slots $ob1->do_stuff; Requirements: · Perl


Class::Std::Slots Related Software