Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

pdcawley (485)

  (email not shown publicly)
AOL IM: pdcawley (Add Buddy, Send Message)

Journal of pdcawley (485)

Thursday March 07, 2002
11:53 AM

Fun bits of code

[ #3352 ]

Hell, if James is showing off large chunks of code in his journal, here's some UNIVERSAL methods I'm toying with adding to our project here. The basic idea was stolen (again) from a Smalltalk idiom.

package UNIVERSAL;

use Scalar::Util qw/blessed weaken isweak/;
use overload ();
use strict;
our %__dependents;

sub hash {
        my $package = ref $_[0];
        return $_[0] unless $package;
        require overload;
        return "" unless overload::Overloaded($package);
        bless $_[0], 'overload::Fake';
        my $val = 0 + $_[0];
        bless $_[0], $package;
        return "";

sub add_dependent {
        my $self = shift;

        my $dependent = shift;

        unless (blessed($self)) {
                require Carp;
                Carp::Croak("You can't attach dependents to a class");
        weaken($__dependents{$self->hash}{$dependent->hash} = $dependent);

sub remove_dependent {
        my $self = shift;
        my $dependent = shift;

        unless (blessed ($self)) {
                require Carp;
                Carp::croak("You can't remove dependents from a class");
        delete $__dependents{$self->hash}{$dependent->hash};

sub dependents {
        my $self = shift;
        values %{$__dependents{$self->hash}};

sub changed {
        my $self = shift;

        $_->update(@_) for $self->dependents;

sub update { }

The idea is that you can use this to help implement Model/View/Controller type magic in such a way that the methods of the Model neither know nor care what views there are on it (if any).

Updated with a couple of typos fixed. It should compile now...

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.