oo newbie question

Discussion in 'Perl Misc' started by Paulus, Sep 29, 2005.

  1. Paulus

    Paulus Guest

    Hi!
    I'm new to oo programming in perl. I'm trying to access an array in a
    class but can't understand what I'm getting.

    This is my class, It contains an array and a method to access that
    array:

    package foo;
    sub new {
    my $self = {
    _arr => []
    };
    return bless $self;
    }
    sub arr {
    my $self = shift;
    if (@_) { @{$self->{_arr}} = @_ }
    return @{$self->{_arr}};
    }
    1;

    This is my test program:
    use strict;
    use foo;
    use Data::Dumper;
    my $bar = foo->new;
    my @source = [1,2,3];
    $bar->arr(@source);
    my @apple = $bar->arr;
    print $bar->arr."\n";
    print @apple."\n";
    print Dumper($bar->arr);

    It gives the following output:
    1
    1
    $VAR1 = [
    1,
    2,
    3
    ];

    I fail to understand why the first print gives '1' ... as I can see
    with Dumper, the array is stored properly? How do I access the array
    elements? I expected @apple to be a copy of _arr ...

    Thanks for your help!
    Paul
    Paulus, Sep 29, 2005
    #1
    1. Advertising

  2. Also sprach Paulus:

    > I'm new to oo programming in perl. I'm trying to access an array in a
    > class but can't understand what I'm getting.
    >
    > This is my class, It contains an array and a method to access that
    > array:
    >
    > package foo;
    > sub new {
    > my $self = {
    > _arr => []
    > };
    > return bless $self;
    > }
    > sub arr {
    > my $self = shift;
    > if (@_) { @{$self->{_arr}} = @_ }
    > return @{$self->{_arr}};
    > }
    > 1;
    >
    > This is my test program:
    > use strict;
    > use foo;
    > use Data::Dumper;
    > my $bar = foo->new;
    > my @source = [1,2,3];


    Here you store a reference to an anonymous array (containing the
    elements 1, 2, 3) in the first element of @source. What you more likely
    want is

    my @source = (1, 2, 3);

    > $bar->arr(@source);


    Here you pass the array containing the array-reference to arr().

    > my @apple = $bar->arr;
    > print $bar->arr."\n";


    This produces 1 because you are using the '.' operator which evaluates
    its operands in scalar context. Note that evaluating an array in scalar
    context yields its length. Since you stored a reference to an array as
    the first element of that array, the length of that array is 1.

    > print @apple."\n";


    The same problem here: You're evaluating @apple in scalar context.

    If you had written

    print @apple, "\n"; # list context

    you'd still not get the desired output. The output would be something
    like

    ARRAY(0x814cbb8)

    because @apple has only one element: the already mentioned
    array-reference.

    > print Dumper($bar->arr);
    >
    > It gives the following output:
    > 1
    > 1
    > $VAR1 = [
    > 1,
    > 2,
    > 3
    > ];
    >
    > I fail to understand why the first print gives '1' ... as I can see
    > with Dumper, the array is stored properly?


    What you neglect is that Data::Dumper() expects a _reference_. If you
    really want to see what the return value of $bar->arr looks like,
    rewrite that line to

    print Dumper \$bar->arr;

    and look at the output again. I'm sure you'll spot the difference.

    > How do I access the array
    > elements? I expected @apple to be a copy of _arr ...


    It is a copy of @{ $self->{_arr} }. Your problem was that _arr didn't
    contain what you thought it did.

    I am not sure if your problems are really with OO. What you may actually
    need is a refresher on Perl's data-structures and references. Just skim
    once again through 'perldoc perldata', "List value constructors". Also
    read about "Context" in the same document. And in case you don't feel
    entirely comfortable, 'perldoc perlreftut' will also help you.

    Tassilo
    --
    use bigint;
    $n=71423350343770280161397026330337371139054411854220053437565440;
    $m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);
    Tassilo v. Parseval, Sep 29, 2005
    #2
    1. Advertising

  3. Paulus

    Paulus Guest

    Thank you very much for your explanation! I'll work on my knowledge of
    data structures and references ;)

    Paul
    Paulus, Sep 29, 2005
    #3
    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. Jerry C.
    Replies:
    8
    Views:
    205
    Uri Guttman
    Nov 23, 2003
  2. Kruno Saho
    Replies:
    0
    Views:
    125
    Kruno Saho
    Apr 7, 2013
  3. Dave Angel
    Replies:
    0
    Views:
    109
    Dave Angel
    Apr 7, 2013
  4. rusi
    Replies:
    0
    Views:
    99
  5. Miki Tebeka
    Replies:
    0
    Views:
    74
    Miki Tebeka
    Apr 7, 2013
Loading...

Share This Page