Solved

How do I create an Array of classes in another class

Posted on 2009-04-13
7
174 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
  • 4
  • 3
7 Comments
 

Author Comment

by:SapphireGirl
Comment Utility
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
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 39

Expert Comment

by:Adam314
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
Thank you!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
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 explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now