Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Object references and Class::Struct

Reply
Thread Tools

Object references and Class::Struct

 
 
Stephan Mann
Guest
Posts: n/a
 
      02-22-2008
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:umper;
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 ${$aaa->childs("bbb")}->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
 
Reply With Quote
 
 
 
 
rthangam
Guest
Posts: n/a
 
      02-22-2008
On Feb 22, 1:40 pm, Stephan Mann <(E-Mail Removed)> wrote:
> 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:umper;
> 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 ${$aaa->childs("bbb")}->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.
 
Reply With Quote
 
 
 
 
Stephan Mann
Guest
Posts: n/a
 
      02-22-2008
On 2008-02-22, rthangam wrote:
> On Feb 22, 1:40 pm, Stephan Mann <(E-Mail Removed)> 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 ${$aaa->childs("bbb")}->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:umper 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:arent' => 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 ${$bbb->parent}->foo, "\n";

This works for whatever object I want to store.

greetings, stephan
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace all references to one object with references to other Jack Bates Python 3 08-06-2011 12:57 AM
Typedef A references struct B which references struct A which... DanielEKFA C++ 8 05-16-2005 10:26 AM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
how to understand references to variables and references to constants are distinguished? baumann.Pan@gmail.com C++ 3 11-10-2004 04:16 AM
Pointers and References (and References to Pointers) Roger Leigh C++ 8 11-17-2003 10:14 AM



Advertisments