use strict; use Data::Dumper; package First; sub new { my $class = shift; my $self = { foo => 'bar' }; return bless $self, $class; } sub test { my $self = shift; my $arg = shift || 10; return ($arg > 9) ? "okay" : bless $self, 'Second'; } package Second; sub new { my $class = shift; my $self = { foo => 'qux' }; return bless $self, $class; } sub test { my $self = shift; my $arg = shift || 4; return ($arg > 5) ? "okay" : undef; } package main; my $arg = shift || 10; my $t = First->new(); if (ref $t->test($arg)) { warn "first failed, trying second..."; die "second failed" unless $t->test($arg); } print (ref $t, " passed\n"); print Dumper $t;