M
mhearne808[insert-at-sign-here]gmail[insert-dot-he
All: I'm having trouble returning an array from a class implemented
using Damian Conway's inside-out object approach.
Below are two snippets of code:
1) The test script where I am attempting to retrieve an array from an
inside-out attribute from an object.
2) The test class.
I think the main problem is on the line where I am assigning an array
to one of these inside out attributes. Somehow I think the attribute
is only getting the last element of the array that I am attempting to
assign it to.
I would have tested Conway's code verbatim, except that the
File::System->list_files() method in his text does not seem to exist.
I assume from context that list_files() returns an array of files...
Does anyone have (simple) examples of inside-out classes with a method
that returns an array?
#Test script
use PAGER::Test;
use strict;
my $test = PAGER::Test->new();
my @array = $test->getData();
my $n;
foreach $n (@array){
print "$n\n";
}
#end of test script
#Test module
#!/usr/bin/perl
package MYPACKAGE::Test;
use Class::Std::Utils;
use strict;
{
my %data; #the absolute path where pager.pl is located
sub new{
my ($class) = @_;
my $new_object = bless anon_scalar(),$class;
$data{ident $new_object} = (1,2,3,4,5);
return $new_object;
}
sub getData{
my ($self) = @_;
return @{$data{ident $self}};
}
sub DESTROY{
my ($self) = @_;
delete $data{ident $self};
return;
}
}
1;
#end of Test module
using Damian Conway's inside-out object approach.
Below are two snippets of code:
1) The test script where I am attempting to retrieve an array from an
inside-out attribute from an object.
2) The test class.
I think the main problem is on the line where I am assigning an array
to one of these inside out attributes. Somehow I think the attribute
is only getting the last element of the array that I am attempting to
assign it to.
I would have tested Conway's code verbatim, except that the
File::System->list_files() method in his text does not seem to exist.
I assume from context that list_files() returns an array of files...
Does anyone have (simple) examples of inside-out classes with a method
that returns an array?
#Test script
use PAGER::Test;
use strict;
my $test = PAGER::Test->new();
my @array = $test->getData();
my $n;
foreach $n (@array){
print "$n\n";
}
#end of test script
#Test module
#!/usr/bin/perl
package MYPACKAGE::Test;
use Class::Std::Utils;
use strict;
{
my %data; #the absolute path where pager.pl is located
sub new{
my ($class) = @_;
my $new_object = bless anon_scalar(),$class;
$data{ident $new_object} = (1,2,3,4,5);
return $new_object;
}
sub getData{
my ($self) = @_;
return @{$data{ident $self}};
}
sub DESTROY{
my ($self) = @_;
delete $data{ident $self};
return;
}
}
1;
#end of Test module