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

Jay Hannah jhannah at omnihotels.com
Wed Sep 21 10:47:15 PDT 2005


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


More information about the Omaha-pm mailing list