Professional Web Applications Themes

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. #1

    Default Simple OO problem ...

    # 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 :
    How can I make $z become another (blessed) thing ?
    So when I modify something in $y , and not affecting $z ?

    Thanks in advise,
    Bee
    Bee Guest

  2. #2

    Default 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.
     

    See comments above about bless.
     

    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
    is very difficult to follow and does not illustrate your questions.

    http://danconia.org
    Wiggins Guest

  3. #3

    Default 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
    $main::x or $test::load -> {B}
     
    >
    > 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

Similar Threads

  1. XML::Simple problem
    By Bill McCormick in forum PERL Modules
    Replies: 7
    Last Post: February 18th, 02:00 AM
  2. Simple problem I think!!!!!
    By Gavin in forum PHP Development
    Replies: 7
    Last Post: October 4th, 05:16 AM
  3. LWP::Simple get() problem
    By Hon in forum PERL Miscellaneous
    Replies: 3
    Last Post: September 29th, 11:10 AM
  4. LWP::Simple get() problem
    By Hon Guin Lee - Web Producer - SMI Marketing in forum PERL Modules
    Replies: 0
    Last Post: September 26th, 01:52 PM
  5. A simple problem with a really simple form
    By Simon Harvey in forum Macromedia Dreamweaver
    Replies: 0
    Last Post: July 13th, 04:34 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139