tags:

views:

131

answers:

2

Hi,

I wrote a simple program that using Class::ArrayObjects but It did not work as I expected. The program is:

TestArrayObject.pm:

package TestArrayObject;
use Class::ArrayObjects define => { 
                       fields  => [qw(name id address)], 
                   };

sub new {
    my ($class) = @_;
    my $self = [];
    bless $self, $class;
    $self->[name] = '';
    $self->[id] = '';
    $self->[address] = '';
    return $self;
}
1;

Test.pl

use TestArrayObject;
use Data::Dumper;

my $test = new TestArrayObject;
$test->[name] = 'Minh';
$test->[id] = '123456';
$test->[address] = 'HN';
print Dumper $test;

When I run Test.pl, the output data is:

$VAR1 = bless( [
             'HN',
             '',
             ''
           ], 'TestArrayObject' );

I wonder where is my data for 'name' and 'id'?

Thanks, Minh.

+9  A: 

Always use use strict. Try to use use warnings as often as possible.

With use strict your test script won't even run, Perl will issue the following error messages instead:

Bareword "name" not allowed while "strict subs" in use at test.pl line 8.
Bareword "id" not allowed while "strict subs" in use at test.pl line 9.
Bareword "address" not allowed while "strict subs" in use at test.pl line 10.
Execution of test.pl aborted due to compilation errors.

That's because the names for your arrays indices are only visible to your TestArrayObject module, but not to the test script.

To keep your class object-oriented, I suggest you implement accessors for your variables, such as get_name/set_name and use those accessors from outside your class module.

innaM
A: 

From Manni's comment, I've made some changes in my program as below:

TestArrayObject.pm:

package TestArrayObject;
use strict;
use Class::ArrayObjects define => {
                                  fields  => [qw(name id address)],
                                };
sub new {
    my ($class) = @_;
    my $self = [];
    bless $self, $class;    
    return $self;
}

sub Name {
    my $self = shift;
    $self->[name] = shift if @_;
    return $self->[name];
}

sub Id {
    my $self = shift;
    $self->[id] = shift if @_;
    return $self->[id];
}

sub Address {
    my $self = shift;
    $self->[address] = shift if @_;
    return $self->[address];
}

1;

==> I added some get/set method for accessing internal array object.

Test.pl:

use strict;
use TestArrayObject;
use Data::Dumper;

my $test = new TestArrayObject;
$test->Name('Minh');
$test->Id('123456');
$test->Address('HN');
print Dumper $test;

And the final output is:

$VAR1 = bless( [
             'Minh',
             '123456',
             'HN'
           ], 'TestArrayObject' );

It's exactly what I expected.

Thanks.

Minh Le
You can save some lines of code and management bloat when wrote method generator `BEGIN { for (qw(name id address)) { no strict 'refs'; my $no = *{ucfirst()} = sub {my $self = shift; $self->[$no] = shift if @_; return $self->[$no];};}}`
Hynek -Pichi- Vychodil
Anyway I dislike get/set and recommend using separated get and set. `BEGIN { for (qw(name id address)) {no strict 'refs'; my $no = my $name = ucfirst; *{"get$name"} = sub {shift->[$no]}; *{"set$name"} = sub {$_[0]->[$no] = $_[1]};}}`
Hynek -Pichi- Vychodil
Yes, I agree with you about using get/set. It makes the code more OO.
Minh Le
You should use Moose. There is no reason to write all thisboilerplate... Perl is not Java after all ;)
jrockway