Solved

How do I create an Array of classes in another class

Posted on 2009-04-13
7
183 Views
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
$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

0
Comment
Question by:SapphireGirl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 

Author Comment

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

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

Expert Comment

by:Adam314
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.
0
 

Author Comment

by:SapphireGirl
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?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

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

by:Adam314
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();

0
 

Author Comment

by:SapphireGirl
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?

0
 
LVL 39

Accepted Solution

by:
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.
   
0
 

Author Closing Comment

by:SapphireGirl
ID: 31569640
Thank you!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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 (http://dilbert.com/strips/comic/2007-08…
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

617 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