Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello,
I'm trying to use Class::Struct, but I'm missing something (probably at a more general level...).
If I run this code I see that if I write a method to override the one created by the Class:Struct module, the attribute name (new_id, in the following) has not the package name prepended. So when I try to access it it is undefined.
Could you please provide me some clue ?
TIA
use strict; use warnings; use Data::Dumper; package Datum; use Class::Struct; struct (Datum => { id => '$', new_id => '$', }); sub Datum::new_id { my $self = shift(); if (@_) { $self->{new_id} = $_[0] + 1; } return $self->{new_id}; } package Dataset; use Class::Struct; struct (Dataset => { member => '$', }); sub Dataset::dump_new_id { my $self = shift(); print "\n\n new_id is undefined\n\n" unless defined ($self->{new_i +d}); } package main; my $dataset = Dataset->new; my $datum = Datum->new; $datum->id(3); $datum->new_id(3); $dataset->member($datum); $dataset->dump_new_id; die Dumper($dataset);
That gives:
new_id is undefined $VAR1 = bless( { 'Dataset::member' => bless( { 'Datum::id' => 3, 'new_id' => 4, 'Datum::new_id' => unde +f }, 'Datum' ) }, 'Dataset' );

Replies are listed 'Best First'.
Re: Overriding methods in Class::Struct
by davidrw (Prior) on Jul 10, 2005 at 17:04 UTC
    did you mean this? (note the ->member in there) or did you mean for dump_new_id to be in the Datnum package?
    sub Dataset::dump_new_id { my $self = shift(); print "\n\n new_id is undefined\n\n" unless defined ($self->member +->{new_id}); }
      yes, sorry... that's what i meant. and it works. i was wondering if Datum::new_id and new_id are the same. thanks