I tried putting the line you tried and then calling my constructor on an already running instance, and it does return an object, but one blessed incorrectly(read: differently) for my purposes. When I try and invoke any methods on this object that is blessed into "Expense=HASH(0x123456)" and not "Expense" it gives me an error.
So, basically, my question is this... Can you please give me a real world scenario or two where I would want to call $object->new and not CLASS->new, and then an example of how you use that new returned object's methods, since it is not blessed into the correct class? Here's the code I used to come to these conclusions:
use Data::Dumper; $exp1 = new Expense; $exp1->place("place1"); $exp1->description("desc1"); $exp1->amount("1"); $exp1->details("details1"); $exp2 = new Expense; $exp2->place("place2"); $exp2->description("desc2"); $exp2->amount("2"); $exp2->details("details2"); $exp3 = $exp1->new; $exp3->place("place3"); $exp3->description("desc3"); $exp3->amount("3"); $exp3->details("details3"); print Data::Dumper->Dump( [ $exp1, $exp2, $exp3 ] , [ qw( exp1 exp2 ex +p3 ) ] ); { package Expense; sub new { #my $either = shift; #my $class = ref($either) || $either; my $class = shift; my $self = {}; $self->{_DESCRIPTION} = undef; $self->{_PLACE} = undef; $self->{_AMOUNT} = undef; $self->{_ID} = undef; $self->{_DETAILS} = undef; bless $self, $class; } sub place { my $self = shift; my $hiddenkey = "_PLACE"; if (@_) { $self->{$hiddenkey} = shift; } return $self->{$hiddenkey}; } sub description { my $self = shift; my $hiddenkey = "_DESCRIPTION"; if (@_) { $self->{$hiddenkey} = shift; } return $self->{$hiddenkey}; } sub amount { my $self = shift; my $hiddenkey = "_AMOUNT"; if (@_) { $self->{$hiddenkey} = shift; } return $self->{$hiddenkey}; } sub id { my $self = shift; my $hiddenkey = "_ID"; if (@_) { $self->{$hiddenkey} = shift; } return $self->{$hiddenkey}; } sub details { my $self = shift; my $hiddenkey = "_DETAILS"; if (@_) { $self->{$hiddenkey} = shift; } return $self->{$hiddenkey}; } } #end package
In reply to Re: (jeffa) Re: My first stab at OO perl...
by Theseus
in thread My first stab at OO perl...
by Theseus
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |