[Omaha.pm] Chaining a method (called "init")

Brian Wiese bwiese at cotse.com
Thu Sep 22 23:58:17 PDT 2005


I'm a bit out of touch with my perl syntax, could your explain this with 
some comments?

Jay Hannah wrote:

>This is just a sanity check that chaining methods works the way I
>thought it did.
>
>---------------------------
>$ cat j.pl
>#!/usr/bin/perl
>
>use vars qw( @ISA );
>
>package A;
>sub new {
>  # I really don't understand this mojo, 
>  # but we know it works when we use it...
>  my ($caller) = (@_);
>  my $caller_is_obj = ref($caller);
>  my $class = $caller_is_obj || $caller;
>  my $self = bless {}, ref($class) || $class;
>}
>sub init { print "a"; }
>
>package B;
>@ISA = ('A');
>sub init { $_[0]->SUPER::init; print "b"; }
>
>package C;
>@ISA = ('B');
>sub init { $_[0]->SUPER::init; print "c"; }
>
>package D;
>@ISA = ('C');
>sub init { $_[0]->SUPER::init; print "d"; }
>
>package E;
>@ISA = ('D');
>sub init { $_[0]->SUPER::init; print "e"; }
>
>package main;
>my $obj = E->new;
>print "\$obj is $obj\n";
>$obj->init;
>print "\n";
>---------------------------
>
>Yup. It does what I expected:
>---------------------------
>$ perl j.pl
>$obj is E=HASH(0x815c088)
>abcde
>---------------------------
>
>Yay! I'm not losing my mind after all!
>
>j
>_______________________________________________
>Omaha-pm mailing list
>Omaha-pm at pm.org
>http://mail.pm.org/mailman/listinfo/omaha-pm
>
>  
>


-- 
bwiese[at]cotse.com | brianwiese.net | 402.297.9392
"What we do in life echoes in eternity" - Gladiator



More information about the Omaha-pm mailing list