perl objects, first time, feedback please

Just starting to look at perl objects.  Would
like some simple feedback .. do I have this right?

Generally speaking, all the class data associated with
an 'instance' of a class, should be placed 'within'
the original objects blessed hash (the use of a hash
is convention).

Here's my first test object.  Is this how you generally
do it?

  package Obj2;

  my $this_is_static_data = 4;

  sub
  new {
    my $class = $_[0];  # -> notation inserts class name;
    my $p_hash = {};    
    bless $p_hash, $class;
    return $p_hash;
  }

  sub
  add_new_value_and_echo_all {
    my ($self, $name, $value) = @_;
    $self->{$name} = $value;  

    foreach my $k (keys %$self) {
      print qq/BUG: $k = $self->{$k}\n/;
    }
  }

  sub
  init_an_array_A {  # do it this way
    my ($self) = @_;
    my $p_scores = [];

    foreach my $y (qw(a b c e d f)) {
      push @$p_scores, $y;
    }
    $self->{'scores'} = $p_scores;
  }

  sub
  init_an_array_B {  # or do it this way
    my ($self) = @_;
    my $p_scores = [];
    $self->{'scores'} = $p_scores;

    foreach my $y (qw(a b c e d f)) {
      push @{$self->{'scores'}}, $y;
    }

  }
  sub
  say_an_array {
    my ($self) = @_;

    my $array_ref = $self->{'scores'};
    foreach my $y (@$array_ref) {
      print qq/BUG say $y\n/;
    }
  }

 
LVL 1
hank1Asked:
Who is Participating?
 
ozoCommented:
Statements about where data "should" be placed are more a mater of style than having Perl right.

Your code all works, although
      print qq/BUG: $k = $self->{$k}\n/;
will just display ARRAY(address)
when $self-{scores} is not a scalar
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.