Click here to get back home

Object references and Class::Struct

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Object references and Class::Struct Stephan Mann 02-22-2008
Posted by Stephan Mann on February 22, 2008, 3:40 am
Please log in for more thread options
Hi!

I need to create a double linked tree so I want to use objects and
therefore read the Class::Struct manual and the Perl FAQ entry. One
thing I can't figure out though is how to store a object reference.

The following code works, and stores references in the childs hash:


use strict;
use warnings;
use Data::Dumper;
use Class::Struct qw(struct);

struct FooBar => {
foo => '$',
bar => '$',
parent => 'FooBar',
childs => '%',
};

my $aaa = new FooBar(foo => "aaa", bar => "AAA");
my $bbb = new FooBar(foo => "bbb", bar => "BBB");
$bbb->parent($aaa);
$aaa->childs("bbb" => $bbb);

print Dumper($aaa);
print Dumper($bbb);

print $->foo, "\n";
print $bbb->parent->foo, "\n";


According to the manual, I should be able to define

parent => '*FooBar'

and store a object reference in it. But I then can't assign a value any
longer. Both

$bbb->parent($aaa);
$bbb->parent($aaa);

give me a "parent argument is wrong class" error. So how do I store a
object reference in a struct?!

tia, stephan

Posted by rthangam on February 22, 2008, 5:34 am
Please log in for more thread options
> Hi!
>
> I need to create a double linked tree so I want to use objects and
> therefore read the Class::Struct manual and the Perl FAQ entry. One
> thing I can't figure out though is how to store a object reference.
>
> The following code works, and stores references in the childs hash:
>
> use strict;
> use warnings;
> use Data::Dumper;
> use Class::Struct qw(struct);
>
> struct FooBar => {
> foo => '$',
> bar => '$',
> parent => 'FooBar',
> childs => '%',
>
> };
>
> my $aaa = new FooBar(foo => "aaa", bar => "AAA");
> my $bbb = new FooBar(foo => "bbb", bar => "BBB");
> $bbb->parent($aaa);
> $aaa->childs("bbb" => $bbb);
>
> print Dumper($aaa);
> print Dumper($bbb);
>
> print $->foo, "\n";
> print $bbb->parent->foo, "\n";
>
> According to the manual, I should be able to define
>
> parent => '*FooBar'
>
> and store a object reference in it. But I then can't assign a value any
> longer. Both
>
> $bbb->parent($aaa);
> $bbb->parent($aaa);
>
> give me a "parent argument is wrong class" error. So how do I store a
> object reference in a struct?!
>
> tia, stephan

An object is perl is a reference which knows which class it belongs to
so simply passing $aaa should be enough, which you have done it anyway
in your code.

1. An object is simply a reference that happens to know which class
it belongs to.
2. A class is simply a package that happens to provide methods to
deal with object references.
3. A method is simply a subroutine that expects an object reference
(or a package name, for class methods) as the first argument.

Posted by Stephan Mann on February 22, 2008, 6:15 am
Please log in for more thread options
On 2008-02-22, rthangam wrote:
>> struct FooBar => {
>> foo => '$',
>> bar => '$',
>> parent => 'FooBar',
>> childs => '%',
>>
>> };
>>
>> my $aaa = new FooBar(foo => "aaa", bar => "AAA");
>> my $bbb = new FooBar(foo => "bbb", bar => "BBB");
>> $bbb->parent($aaa);
>> $aaa->childs("bbb" => $bbb);
>>
>> print Dumper($aaa);
>> print Dumper($bbb);
>>
>> print $->foo, "\n";
>> print $bbb->parent->foo, "\n";
>
> An object is perl is a reference which knows which class it belongs to
> so simply passing $aaa should be enough, which you have done it anyway
> in your code.
>
> 1. An object is simply a reference that happens to know which class
> it belongs to.
> 2. A class is simply a package that happens to provide methods to
> deal with object references.
> 3. A method is simply a subroutine that expects an object reference
> (or a package name, for class methods) as the first argument.

Okay -- but this is kind of confusing, since Data::Dumper shows the one as
a reference, but the other one not. So I got the feeling I'm copying
values.

'FooBar::childs' => {
        'bbb' => \bless( {

'FooBar::parent' => bless( {

However, I've now taken a different approach. Since I need multiple
objects but can't use inheritance because Class::Struct doesn't allow
@ISA and Export won't work because I don't have multiple files, I'll now
use a scalar to store my reference.

        parent => '$',
...
$bbb->parent($aaa);
...
print $->foo, "\n";

This works for whatever object I want to store.

greetings, stephan

Similar ThreadsPosted
A doubt in using Class::Struct October 5, 2006, 2:34 pm
Class::Struct array assignment and access April 29, 2006, 2:31 pm
Object References as a member of a bigger Object August 5, 2007, 7:02 pm
FAQ 4.74 How do I define methods for every class/object? February 23, 2005, 6:03 pm
FAQ 4.74 How do I define methods for every class/object? April 26, 2005, 11:03 pm
FAQ 4.74 How do I define methods for every class/object? July 12, 2005, 4:03 pm
FAQ 4.74 How do I define methods for every class/object? October 11, 2005, 10:03 pm
FAQ 4.74 How do I define methods for every class/object? October 31, 2005, 5:03 am
FAQ 4.74 How do I define methods for every class/object? December 30, 2005, 11:03 pm
FAQ 4.74 How do I define methods for every class/object? March 8, 2006, 3:03 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap