Re: Using "Perl Best Practices" inside-out objects
- From: "mhearne808[insert-at-sign-here]gmail[insert-dot-here]com" <mhearne808@xxxxxxxxx>
- Date: 15 May 2007 12:44:58 -0700
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
.
- Follow-Ups:
- Re: Using "Perl Best Practices" inside-out objects
- From: Paul Lalli
- Re: Using "Perl Best Practices" inside-out objects
- References:
- Using "Perl Best Practices" inside-out objects
- From: mhearne808[insert-at-sign-here]gmail[insert-dot-here]com
- Re: Using "Perl Best Practices" inside-out objects
- From: Paul Lalli
- Using "Perl Best Practices" inside-out objects
- Prev by Date: how to create multiple zip files that are no larger than 2 Gb each
- Next by Date: Re: Using "Perl Best Practices" inside-out objects
- Previous by thread: Re: Using "Perl Best Practices" inside-out objects
- Next by thread: Re: Using "Perl Best Practices" inside-out objects
- Index(es):
Relevant Pages
|