1 How do I compare Class::Struct instances (objects)
for equality (or precedence)?
E.g.,
use Class::Struct MyStruct =>
[foo => '$',
bar => '$',
baz => '$'];
my MyStruct $a = MyStruct->new(foo => "a");
my MyStruct $b = MyStruct->new(bar => "b");
my MyStruct $a1 = MyStruct->new(foo => "a");
Both ($a eq $a1) and ($a eq $b) return false which is wrong for the
first expression.
I can write my own comparison, of course:
sub struct_eq ($$) {
my ($x,$y) = @_;
List::AllUtils::notall
(sub { $_ }, (List::AllUtils::pairwise
(sub { $a eq $b }, @$x, @$y)));
}
but this does simple version does not work because some fields are
undefined and I have to check than and it quickly becomes a nightmare.
I could write a tedious function like
sub MyStruct_eq ($$) {
my ($x,$y) = @_;
(defined $x->foo and defined $y->foo and $x->foo eq $y->foo)
or (not defined $x->foo and not defined $y->foo)
.....
}
but there must be a better way.
2. Is there a way to get a list of all fields in a struct?
Class::Fields seems to claim to be able to do it, but I cannot figure
out how to use it.
Class::Fields->show_fields('MyStruct') returns nothing.
MyStruct->show_fields is not defined.
I am lost.
I would also like to be able to access MyStruct instance fields by name.
I know I can do that using hash{} instead of array[] in the definition,
but I would like to preserve the underlying array structure for passing
to Text::CSV functions (I guess I could use "values" for that too).
E.g., I want to be able to write something like
my $mystruct_foo_pos = MyStruct->getpos('foo');
and have $mystruct_foo_pos set to 0.
then I will be able to write
my MyStruct $a = MyStruct->new(...);
$a[$mystruct_foo_pos] = "a";
Thanks!
--
Sam Steingold (http://sds.podval.org/) on CentOS release 5.3 (Final) X
http://iris.org.il http://pmw.org.il http://memri.org http://ffii.org
http://dhimmi.com http://palestinefacts.org http://jihadwatch.org
My other CAR is a CDR.
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/