Environment[indent]TS672 & TS721 - on W2K3[/indent]
Issue[indent]I have a custom OOPerl class defined that was fairly simple and provided methods for accessing fields that were strings; for example:
#==================== [foo.pm]
package foo;
sub new {
my ($self, $params) = @_;
my $class = ref($self) || $self;
return bless {
list => [$params{name}],
}, $class;
}
sub getList{ return $_->{list}; }
#==================== [x.ipl]
use foo;
my $obj = foo->new({name => "abc"});
print join("\n", @{$foo->getList()}) . "\n";
Now I have new requirements that require me to turn those simple strings into other OOPerl objects because I need to be able to [optionally] get additional information about those fields in the original object, e.g.:
#==================== [bar.pm]
package bar;
sub new {
my ($self, $params) = @_;
my $class = ref($self) || $self;
return bless {
name => $params{name},
type => $params{type},
}, $class;
}
sub getName { return $_->{name}; }
sub getType { return $_->{type}; }
#==================== [MODIFIED foo.pm]
package foo;
use bar;
sub new {
my ($self, $params) = @_;
my $class = ref($self) || $self;
return bless {
list => [bar->new({name => $params{name}, type => "xyz"})],
}, $class;
}
sub getList{ return $_->{list}; }
So far - all would seem reasonably good and pretty easy to deal with - but - I want to be able to preserve backwards-compatibility for code that was written for the original "foo" class - and I'd also like to be able to use basic OO method chaining. If we go back to:
#==================== [x.ipl]
use foo;
my $obj = foo->new({name => "abc"});
print join("\n", @{$foo->getList()}) . "\n";
The above will no longer work - at least not as desired, instead I have to change the last line to something like:
...
foreach my $obj (@{$foo->getList()}) {
print $obj->getName() . "\n";
}
Sure - it seems simple - but this is an overly simplistic example of the issue and I have to consider the number of places within the number of scripts where the pre-existing code was used and now needs to be changed.
What I want to be able to do is somehow default references to the bar object (in a string or scalar context) to be treated as-if they were calls to the bar object's getName() method -- similar to the way in Java that references to objects default to behaving as if you had called the object's toString() method.[/indent]
I cannot figure out how to do this - or even if it is possible. I tried using AUTOLOAD - but either it doesn't do this or I did something wrong - it always seems to be trying to invoke the DESTROY method - and completely ignores my attempts to return $self->{name} or $self->getName().
Anyone have any ideas on this - I really don't want to have to force changes to all the pre-existing uses of the "foo" module if I can avoid it.