Go Premium for a chance to win a PS4. Enter to Win


How do I create an Array of classes in another class

Posted on 2009-04-13
Medium Priority
Last Modified: 2012-05-06
How do I create an array of classes in a perl class.

Here is what I would like to do.
Now to use these 2 classes I have a method that will return the _jobName called  jobName

so I can reference the method in the job class after I have instantiated it like $self->jobName("MyJob");

Here jobName is an accessor function with get and set properties.
Thus in a method of the job_array class I want to go thru a loop and set the jobName of each
job in the job array.

I thought it would be here self is the job_array class

Here $self is the job_array class
and jobName is the method in the job class and $self->{_jobs} is the array in the job array class and i is just a counter.

When the above code did not work I tried

$self{_jobs}->[$i]->jobName;  but that did not work either.

Thank you.  This is my first time trying to create an array of classes.
Hopefully this makes sense.

The job class
sub new {
   my ($class) = @_; # @ARG
   my $self = {
      _jobName => undef
   bless $self, $class;
   return $self;
# the jobs array class
sub new {
   my ($class) = @_; # @ARG
   my $self = {
      _jobs => [], # An array of job classes
      _job_array_name # An array of job classes
   bless $self, $class;
   return $self;

Open in new window

Question by:SapphireGirl
  • 4
  • 3

Author Comment

ID: 24132468
Here is how I added the new class to the array

$self->{_jobs}->[$i] = eval { new CGGVJobs(); } or die ($@);
LVL 39

Expert Comment

ID: 24132562
With the "jobs array" class, why do you have _jobs and _jobs_array_name?  If _jobs is an array of "job" objects, it looks like the job object contains the name?

Also, typically, you never access the data directly of a class object, you access/update it through the objects methods.

Author Comment

ID: 24132802
Thank you Adam.
Actually the _jobs_array_name was just a test to see if the class was created by the eval function and it was.  I know the 2 seem too similar but I did get it to work.  I do have accessor functions for all of my data that I would consider private.  Are you however talking about my eval statement

$self->{_jobs}->[$i] = eval { new CGGVJobs(); } or die ($@);

Is there a better way?

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 39

Expert Comment

ID: 24133129
There is no need for the eval:
  $self->{_jobs}->[$i] = new CGGVJobs();
Or, if you just want to add a bunch of CGGVJobs objects:
    push @{$self->{_jobs}}, new CGGVJobs();


Author Comment

ID: 24197043
Thank you Adam,
  Your previous post worked.  I just have one last question about scope of these classes.
Creating an array of classes sounds nice but I am not sure that the information will be valid because of the scope of each class.

For instance:
I have a JobArray class that fills a set of (lets say) 5 job classes.

So Here $self is the JobArray class.

so I call $self->{_fsmountjobs}->[$i] = new CGGVJobs();
in a loop where _fsmountjobs is an array in the JobArray class.

Will the CGGVJobs object go out of scope outside the loop?
In other works will the CGGVJobs objects be able to be accessed in the method that the loop is in even if it outside the loop.

Also, could I return this array after I have instanciated the objects and set their variables to even a perl script that uses the CGGVJobs.pm and the CGGVJobArray.pm files so I could use the info in the array of objects outside the scope of the method that created the array of objects.

Does that make sense.  I think right off the bat no because I am going to have a scoping problem here.
My first thought is that if I want to use the array of objects I need to use it in the method that creates them or in another method of the JobArray class since _fsmounts[] is a variable in this class.
I do not think I will be able to return the array of objects to a perl script that uses the JobArray class.

Thought.  Is there a better way to do this?

LVL 39

Accepted Solution

Adam314 earned 2000 total points
ID: 24197967
>>Will the CGGVJobs object go out of scope outside the loop?
No.  It will continue to exist outside the loop.

>> ...could I return this array...
Yes.  Something like:
    sub GetArrayOfJobs {
        my $self = shift;
        return @{$self->{_fsmountjobs}};
    Then call like:
    my @jobs = $JobArray->GetArrayOfJobs;

>>Is there a better way to do this
I don't know enough about what you are doing to know.  There is nothing wrong with having a class have some of it's data members be objects of another class though.

Author Closing Comment

ID: 31569640
Thank you!

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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

783 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