in reply to Typecasting a scalar to a module

I think the concept that you are looking for is 'Object inheritance'. (the exact term, to tell a scalar what package it belongs to is bless

my $local1 = my_module->new(); $local1->func1(); $local1->func2(); my $local2 = my_module->new(); $local2->func1(); $local2->func2(); package my_module; use base qw(a_module); sub func1 { shift->do_something1() } sub func2 { shift->do_something2() }

This will work so long as a_module is set up correctly for inheritance. If it isn't (you can tell because ref($local1) will be 'a_module' and not 'my_module'), it'll behave the same way you've been having problems with, and you'll need to add within your package:

sub new { my $class = shift; return bless a_module->new(), ref($class) || $class; }

Replies are listed 'Best First'.
Re^2: Typecasting a scalar to a module
by Mugatu (Monk) on Mar 04, 2005 at 16:53 UTC
    Inheritance is not always the answer. It makes sense if there is an is-a relationship involved, but often times the relationship is really has-a. A has-a relationship is better served through composition. In the OP's case, he's implementing a kind of poor man's composition, in that his main code is the "object" that has-a copy of the sub object.