How do I create an Array of classes in another class

Posted on 2009-04-13
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?
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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 and the 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 500 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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

803 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