?
Solved

perl objects, first time, feedback please

Posted on 2006-04-29
1
Medium Priority
?
203 Views
Last Modified: 2010-03-05
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/;
    }
  }

 
0
Comment
Question by:hank1
1 Comment
 
LVL 85

Accepted Solution

by:
ozo earned 500 total points
ID: 16569558
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question