Re: Using "Perl Best Practices" inside-out objects



Paul - Thanks very much! I didn't know about the [] syntax for
creating references to arrays. I presume now that the mythical
list_files() method returns a reference to an array, not an array.
Might have been helpful if Conway had used a real method in his
example.

--Mike

On May 15, 1:35 pm, Paul Lalli <mri...@xxxxxxxxx> wrote:
On May 15, 3:17 pm, "mhearne808[insert-at-sign-here]gmail[insert-dot-

here]com" <mhearne...@xxxxxxxxx> wrote:
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 snipped the class structure, because inside-out objects have nothing
at all to do with this problem. Giant Red Herring. The problem is
this line:

$data{ident $new_object} = (1,2,3,4,5);

If you print $data{ident $new_object}, you will see it contains the
value 5. That is because you cannot assign a list of values to a
scalar. What you're actually doing is using the comma operator in
scalar context. (see perldoc perlop for more info on that).

Instead, you need to assign a reference to an anonymous array to that
scalar value. The construct for creating such a reference is square
brackets, not parentheses.

$data{ident $new_object} = [ 1, 2, 3, 4, 5];

Once again, this has nothing to do with objects, be they 'normal' or
inside-out. This is the same issue as if you had errantly done:

my $foo = (1, 2, 3, 4, 5);
In that case, $foo would get the value 5. Whereas if you'd correctly
done:
my $foo = [1, 2, 3, 4, 5];
then $foo would get a reference to an anonymous array that contains
the values (1, 2, 3, 4, 5).

Hope this helps,
Paul Lalli


.



Relevant Pages

  • Re: VB-101: Passing Arrays ByVal vs ByRef
    ... changed if an array is passed by Val. ... ' new reference ... As each function creates a new array object, ... 'secondArray' and 'secondArrayCopy'. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Need help with textboxes
    ... The JavaScript 1.5 Reference already states: ... All forms and their children are stored in an array ... use dot notation or object literals. ... No. Bracket property accessors allow their argument to be any string value. ...
    (comp.lang.javascript)
  • Re: Garbage Collection Issues in long-standing services
    ... the pinned array should get unpinned or ... The receive case is done quite well, I do create a 4K buffer and reuse it ... and remove the reference to my wrapper socket class, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Inheritance of constructors.
    ... Bare in mind that was just for sake of example. ... In case of "reference by value" that's not ... Foo foo = new Foo; ... The changes to the array are visible in Foo, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to give selective access to the methods in a class?
    ... different memory locations at different times. ... The reference still leads to the ... So array resizing ... program pushes certain objects into the circular buffer, ...
    (comp.lang.java.programmer)