# Simple OO problem ... - PERL Beginners

# test::load package test::load; require Exporter; our ISA = qw/Exporter/; our EXPORT = qw/new copy/; sub copy { my \$self ; my \$x = shift; my \$y = \$x -> {B}; \$y .= \$y; \$self -> {YY} = \$y; bless \$self; return \$self; } sub new { my \$self; \$self -> {A} = shift; \$self -> {B} = shift; bless \$self; return \$self; } 1; __END__ # main use test::load; my \$x = test::load -> new ( AAA => 111 ); my \$y = \$x ->copy ; my \$z = \$y; \$y->{YY} = 100; print \$z -> {YY}; __END__ Question ...

1. ## Simple OO problem ...

require Exporter;
our ISA = qw/Exporter/;
our EXPORT = qw/new copy/;

sub copy
{ my \$self ;
my \$x = shift;
my \$y = \$x -> {B};
\$y .= \$y;
\$self -> {YY} = \$y;
bless \$self;
return \$self;
}

sub new
{ my \$self;
\$self -> {A} = shift;
\$self -> {B} = shift;
bless \$self;
return \$self;
}
1;
__END__

# main

my \$x = test::load -> new ( AAA => 111 );
my \$y = \$x ->copy ;
my \$z = \$y;

\$y->{YY} = 100;
print \$z -> {YY};
__END__

Question :
How can I make \$z become another (blessed) thing ?
So when I modify something in \$y , and not affecting \$z ?

Bee
Bee Guest

2. ## Re: Simple OO problem ...

use strict;
use warnings;

This is very confusing code. \$self is generally used for the instance
you are acting on. 'copy' is also an odd choice, some prefer 'clone' (if
that is really what you are intending but I can't tell).

Are you sure B has in it what you think? See below.

perldoc -f bless

I am not sure you are fully understanding bless. Generally you want to
use the 3 argument form to provide for inheritance, and to avoid other
issues.

Again, \$self is generally (in a constructor) used to hold the class. Do
you really want A to be 'AAA' and B to be '111'?? Or were you expecting
'AAA' to be '111'. These are very very poor names.

Just a style issue, Perl provides the following alternate syntax (that
is not required).

my \$x = new test::load ( AAA => 111 );

You might also consider using mixed case package names, generally all
lowercase and all upper case package names are reserved for core features.

perldoc perlstyle

You can't, at least in the way I think you mean. C<bless> is just
another function, you can bless (just about) anything. So \$z is
blessable, by just saying,

bless \$z;

Are you talking about cloning \$y? The code you provided is just copying
the reference to the instance.

Have you read the doentation on references,

perldoc perlreftut
perldoc perlref

And the doentation on Perl OOP,

perldoc perlboot
perldoc perltoot
perldoc perltooc
perldoc perlbot

Tell us what you are really trying to do, give us real classes, method
names, and variables. Are you trying to clone objects? Your pseduo-code

http://danconia.org
Wiggins Guest

3. ## Re: Simple OO problem ...

> >
>
> This is very confusing code. \$self is generally used for the instance
> you are acting on. 'copy' is also an odd choice, some prefer 'clone' (if
> that is really what you are intending but I can't tell).[/ref]

Yes, I do mean Clone.

>
> Are you sure B has in it what you think? See below.[/ref]

Yes, I expecting to take out {B} from the reference picking out from

>
> perldoc -f bless
>
> I am not sure you are fully understanding bless. Generally you want to
> use the 3 argument form to provide for inheritance, and to avoid other
> issues.[/ref]

What I understanding from bless is making a data reference to a package
function. But I really don't get the blessed stuff's influence scope through
out the 'main'.

>
> Again, \$self is generally (in a constructor) used to hold the class. Do
> you really want A to be 'AAA' and B to be '111'?? Or were you expecting
> 'AAA' to be '111'. These are very very poor names.[/ref]

Is that better is I use {Class} to be 'AAA' and {Argv} to be 111 ?

<snipped>

>
> You can't, at least in the way I think you mean. C<bless> is just
> another function, you can bless (just about) anything. So \$z is
> blessable, by just saying,
>
> bless \$z;
>
> Are you talking about cloning \$y? The code you provided is just copying
> the reference to the instance.
>[/ref]

Yes, I am asking about how to make \$z become a stand alone new object.
on the other word, they no more using the same reference, whatever the
referenced data being copied or anything.

I guess I got 70% of how reference and de-reference working, but in case
of OO, I can smell something weir, but I can't tell what's wrong.

<snipped>

Sorry for none of them I can provide, it just a test, on learning level.

Remarks :
Well... thaz really my woes that I still have not much ideas on what OO is
and what OO does difference from function calls. I 've read almost stuff
that mentioned, but what I can really got are just BIG QUESTION MARKS and
EXCLAMATION MARKS. As long as the way I learnt programming, I always relay
my learning on codes, then back to doents. Just llike playing with regex,
I totally don't get anything from perldocs, but I learnt them almost from
trying in code, with pieces of hints. Just like a blind guy trying walk
through a door in a new place, just keep on hitting on the wall until get
passed. I know I really not sensiable to read doents with full of
terminologies. So I opt to try on codes. I know thaz an hard way or not the
level for me to step into the OO world, but I still would say that's a nice
try with helping from buddies from this list.

Thanks again !
Bee

Bee Guest

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•