How do I create an Array of classes in another class

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
$self->{_jobs}->[$i]->{jobName};  

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

SapphireGirlAsked:
Who is Participating?
 
Adam314Connect With a Mentor Commented:
>>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.
   
0
 
SapphireGirlAuthor Commented:
Here is how I added the new class to the array

$self->{_jobs}->[$i] = eval { new CGGVJobs(); } or die ($@);
0
 
Adam314Commented:
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.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
SapphireGirlAuthor Commented:
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?
0
 
Adam314Commented:
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();

0
 
SapphireGirlAuthor Commented:
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?

0
 
SapphireGirlAuthor Commented:
Thank you!
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.