Learning OOP

Discussion in 'Perl Misc' started by Nene, Jun 15, 2012.

  1. Nene

    Nene Guest

    I want to learn OOP and really understand it.

    Below is a snip from the perldoc. A few questions; in the constructor, I see
    my $self = {}; Am I creating an annonymous hash called $self?

    $self->{NAME} = undef (Is $self the object? and I'm invoking a method called {NAME)? )

    Can somebody explain in details the method:

    sub name {
    my $self = shift;
    if (@_) { $self->{NAME} = shift }
    return $self->{NAME};


    ###########################################

    package Person;
    use strict;

    ##################################################
    ## the object constructor (simplistic version) ##
    ##################################################
    sub new {
    my $self = {};
    $self->{NAME} = undef;
    $self->{AGE} = undef;
    $self->{PEERS} = [];
    bless($self); # but see below
    return $self;
    }

    ##############################################
    ## methods to access per-object data ##
    ## ##
    ## With args, they set the value. Without ##
    ## any, they only retrieve it/them. ##
    ##############################################

    sub name {
    my $self = shift;
    if (@_) { $self->{NAME} = shift }
    return $self->{NAME};
    }

    sub age {
    my $self = shift;
    if (@_) { $self->{AGE} = shift }
    return $self->{AGE};
    }

    sub peers {
    my $self = shift;
    if (@_) { @{ $self->{PEERS} } = @_ }
    return @{ $self->{PEERS} };
    }

    1; # so the require or use succeeds
     
    Nene, Jun 15, 2012
    #1
    1. Advertising

  2. Nene

    Jim Gibson Guest

    In article <>,
    Nene <> wrote:

    > I want to learn OOP and really understand it.
    >
    > Below is a snip from the perldoc. A few questions; in the constructor, I see
    > my $self = {}; Am I creating an annonymous hash called $self?


    You are creating an anonymous hash. Being anonymous, it does not have a
    name.

    You are then assigning a reference to that anonymous hash to the scalar
    variable $self.

    >
    > $self->{NAME} = undef (Is $self the object? and I'm invoking a method
    > called {NAME)? )


    You are storing the (key,value) pair ('NAME',undef) in the anonymous
    hash referred to by $self. No methods are being called.

    $self is probably an object. We would need to see some more code to say
    for sure. E.g., is $self ever 'blessed' into a class/package.

    >
    > Can somebody explain in details the method:
    >
    > sub name {
    > my $self = shift;
    > if (@_) { $self->{NAME} = shift }
    > return $self->{NAME};


    The first value in the array @_, which is used to pass arguments to
    subroutines, is shifted off and assigned to $self.

    If any values remain in the array @_, the next one ($_[0]) is shifted
    off the front of the array and stored in the hash value $self-{NAME}.

    The value of $self-{NAME} is returned to the caller.

    This is a pattern of method implementation that allows a single method
    to be used both as a "getter" and a "setter":

    # set name
    $self->name("Fred");

    # get name
    my $name = $self->name();

    --
    Jim Gibson
     
    Jim Gibson, Jun 15, 2012
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. LenS

    Newbie learning OOP

    LenS, May 29, 2005, in forum: Python
    Replies:
    5
    Views:
    333
    Terry Hancock
    Jun 1, 2005
  2. LenS

    Newbie learning OOP 2nd ?

    LenS, May 30, 2005, in forum: Python
    Replies:
    2
    Views:
    302
  3. Hal Vaughan
    Replies:
    7
    Views:
    491
  4. Andrey Popp

    [I'm learning C]: Learning to use ucontext

    Andrey Popp, Jan 29, 2012, in forum: C Programming
    Replies:
    5
    Views:
    766
    Keith Thompson
    Jan 31, 2012
  5. Nene
    Replies:
    3
    Views:
    378
Loading...

Share This Page