Object-InsideOut - Re: How To Deal With Arrays Passed Into Objects During Initialization

Posted on Thu Dec 1 01:30:12 2005 by je44ery in response to 1425 (See the whole thread of 6)
Re: How To Deal With Arrays Passed Into Objects During Initialization
First and foremost, thank you very much for responding since my problems are not specifically about Object::InsideOut. I appreciate it much.

Second, you are helping a lot, and again, thank you. I do have some comments, tho. Maybe a question or two, I'm not sure yet.

I have two different responses to your message. I'll be posting them separately and with probably a lot of time between them as I experiment with the code.

Oh, yeah, sorry for my sad code examples the first time. I had tried tons of stuff that day, and I should have cleaned up my examples before posting.

On to my comments. I had originally tried using the :InitArgs field parameter to set the value, but all it does is copy the address of the array (which makes perfect sense), not the array itself. Since code outside the object or class would be able to modify the array, that violates the data encapsulation concept. The main reason I'm using Object::InsideOut is for data encapsulation. Therefore, I can't use :InitArgs because I need a separate copy of the array being pointed-to for use inside the object. I can't modify the pointed-to array and claim data encapsulation.

Here is how I proved this:

$ cat Arraytest.pm package Arraytest; use strict; use warnings; use Object::InsideOut; my @data :Field('Std' => 'data'); my %init_args :InitArgs = ( 'DATA' => { 'Regex' => qr/^data/i, 'Type' => 'ARRAY', 'Field' => \@data, }, ); sub prove_it { my ($self, $args) = @_; print "INSIDE OBJECT = $data[$$self]\n"; } 1;

$ cat arrayt.pl #!/usr/bin/perl -w use warnings; use strict; use Arraytest; my $input = [ "hunger", "pain", "misery", ]; my $newobj = Arraytest->new( 'data' => $input); $newobj->prove_it(); print "OUTSIDE OBJECT = $input\n"; exit 0;

Here is the output. You see I have the same address. What I need is a whole new copy of that array.

# ./arrayt.pl INSIDE OBJECT = ARRAY(0x301482c0) OUTSIDE OBJECT = ARRAY(0x301482c0)
Direct Responses: 1431 | Write a response