PERL OOP newbie question: What is the best way to handle array data?

Discussion in 'Perl Misc' started by lovellj@mcmaster.ca, May 1, 2007.

  1. Guest

    Hi,

    The only way I could get arrays to work with OOP in perl was to use a
    reference to an array. I am wondering is this safe - ie is there any
    chance that some part of the array may get overwritten in memory since
    I am just using the reference in the functions? What is the best way
    to do this?

    I tried assigning $self->{'data_array'} = ("MON","TUE","WED"), but
    then I found when I try to access this data, the array gets messed up
    and it comes out as a scalar or as the last value in the array.


    I apologize in advance if there is an obvious way to do this. Thanks
    so much,
    Jon


    #####Test_class.pm
    #!/usr/bin/perl -w
    package Test_class;
    use strict;
    sub new
    {
    my $class = shift;
    my $self = {};
    my @arr = ("MON","TUE","WED");
    $self->{'data_array'} = \@arr;
    bless ($self,$class);
    return $self;
    }

    sub function1
    {
    my $self = shift;
    my @data_array = @{ $self->{'data_array'} };
    ###use the data...
    ###..... Is this safe - is there any chance parts of the array will
    be deleted in memory
    }
    1;

    ###############
     
    , May 1, 2007
    #1
    1. Advertising

  2. On 30 Apr 2007 23:22:51 -0700,
    <> wrote:
    > Hi,
    >
    > The only way I could get arrays to work with OOP in perl was to use a
    > reference to an array. I am wondering is this safe - ie is there any
    > chance that some part of the array may get overwritten in memory since
    > I am just using the reference in the functions? What is the best way
    > to do this?


    I'm wondering what exactly you mean by 'get arrays to work'. Arrays
    don't work. They're used to store data.

    > I tried assigning $self->{'data_array'} = ("MON","TUE","WED"), but
    > then I found when I try to access this data, the array gets messed up
    > and it comes out as a scalar or as the last value in the array.


    Ah.

    There is indeed some confusion in your head about what arrays are.

    $self->{'data_array'} is a scalar. It is a single element out of a hash,
    which is accessed via a reference to it, stored in $self. It cannot be
    an array, because all you can store in a scalar, is a scalar. When you
    assign a list to a scalar, you end up with the last element in the
    scalar.

    A lot of this is explained int he perlref documentation. It addresses
    building complex data structures (which Perl objects most often are)
    with references.

    As an example:

    If you want to store an array in a complex data structure, you do indeed
    do that by storing a reference to the array. You can do that in several
    ways:

    # Store a reference to an anonymous array
    my $foo = [1, 2, 3];

    # Set $foo to the value 3, ignoring the other elements in the list
    $foo = (1, 2, 3);

    # access the first element
    my $bar = $foo->[0];

    # Store a reference to a named array
    my @ar = (1, 2, 3);
    $foo = \@ar;


    I have the vague feeling that you're looking for the anonymous array
    syntax.

    Martien
    --
    |
    Martien Verbruggen | Begin at the beginning and go on till you
    | come to the end; then stop.
    |
     
    Martien verbruggen, May 1, 2007
    #2
    1. Advertising

  3. Paul Lalli Guest

    On May 1, 2:22 am, wrote:
    > The only way I could get arrays to work with OOP in perl was to use a
    > reference to an array. I am wondering is this safe - ie is there any
    > chance that some part of the array may get overwritten in memory since
    > I am just using the reference in the functions? What is the best way
    > to do this?


    Martien's answer is quite correct and I strongly suggest you take it
    in. I'd just like to point out that this really has nothing to do
    with OOP directly. This is how all multi-dimensional structures work
    in Perl. Objects in Perl simply happen to be (usually) defined as
    hashes, and so any array attributes you want to ascribe them create a
    multi-level structure.

    You cannot store an array in an array, nor an array in a hash, nor a
    hash in an array, nor a hash in a hash. Arrays and Hashes store lists
    of scalars. Period. No way around that. References, however, are
    scalars. Whether it's a reference to a scalar, reference to an array,
    or reference to a hash, the reference itself is a scalar. Therefore,
    you can store a *reference* to an array in either an array or a hash.
    This is how mulit-level data structures are created in perl.

    No, there is no danger of data getting over written. This is not C.
    You are not using "pointers" that can be manipulated to accidentally
    access other data. You are using references. Very different concept.

    I strongly suggest you have a read of:
    perldoc perlreftut
    perldoc perllol
    perldoc perldsc
    perldoc perlref
    (in that order)

    Paul Lalli
     
    Paul Lalli, May 1, 2007
    #3
  4. Guest

    On May 1, 6:28 am, Paul Lalli <> wrote:

    > I strongly suggest you have a read of:
    > perldoc perlreftut
    > perldoc perllol
    > perldoc perldsc
    > perldoc perlref
    > (in that order)


    Thanks very much.
    Jon
     
    , May 1, 2007
    #4
  5. Joe Smith Guest

    Re: PERL OOP newbie question: What is the best way to handle arraydata?

    wrote:
    > I tried assigning $self->{'data_array'} = ("MON","TUE","WED"), but
    > then I found when I try to access this data, the array gets messed up
    > and it comes out as a scalar or as the last value in the array.


    That is correct; the comma operator in scalar context does exactly that.

    You'll need to know when it is appropriate to use [] instead of ().

    Using an array:
    @array = ("MON","TUE","WED");
    @array = qw(MON TUE WED);

    Using a scalar (a reference to an anonymous array):
    $array_ref = ["MON","TUE","WED"];
    $array_ref = [qw(MON TUE WED)];
    $self->{'data_array'} = [ qw(SUN MON TUE WED THU FRI SAT) ];

    -Joe
     
    Joe Smith, May 4, 2007
    #5
    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. Ravikanth[MVP]
    Replies:
    6
    Views:
    3,895
    Aemca
    Jul 18, 2003
  2. Alan Silver
    Replies:
    4
    Views:
    340
    Alan Silver
    Feb 16, 2005
  3. Stef Mientki
    Replies:
    17
    Views:
    444
    Michele Simionato
    Jan 11, 2007
  4. Veli-Pekka Tätilä
    Replies:
    7
    Views:
    157
    Veli-Pekka Tätilä
    Aug 20, 2005
  5. Mike Copeland
    Replies:
    5
    Views:
    495
    Ian Collins
    Apr 14, 2012
Loading...

Share This Page