2

Is this possible using Perl:

my @array = ($class1,$class2,$class3);

foreach my $c (@array)
{
    my $temp = $c->new();
    $temp->run($var1,$var2);
}

The idea behind this is that the array will always contain different class names. I would then like to create an object of that class and run a method from it. Each class is somewhat similar but contains its own logic in the run method?

If this is not possible, is there a different way i could do this? Is this bad programming?

4 Answers 4

6

You need to make sure that the run-Method is always accessible:

my @array = ($class1,$class2,$class3);

foreach my $class (@array) {
    my $temp = $class->new();
    if ($temp->can('run') {
        $temp->run($var1,$var2);
    } else {
        ...
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, just the answer i was looking for
5

What makes a class in perl is the bless statement. You bless a reference with a name of a class, and wham!, it's that class. Nothing too special about it.

Of course, you could end up with a class with no methods which might be a bit of a problem. However, I do this for subclasses where subclasses share a common parent class, but the type of the class changes the behavior of the class:

Package Main_class;
use Carp;

sub new {
    my $class      = shift;   #We'll ignore this one
    my $subclass   = shift;   #This is my actual class

    my $self = {};
    my $class .= "::$subclass";
    bless $self, $class; #Now, it's my class!
    if ( not $self->isa($class) ) {
        croak qw(Subclass "$subclass" is an invalid subclass);
    }
    return $self;
}

In my program, I'll do this:

 my $object = Main_class->new($subclass);

And, if I don't want my program to die...

my $object;
eval {
    $object = Main_class->new($subclass);
}
if ( $@ ) {
    Here be dragons....    #What do you do if that object creation failed...
}

Here's an example of a program where I do this.

Here I'm reading in a file of questions and their types. I read in the macro name, and the type of question it is. I then use my parent class to create the object, but I bless it with the correct subclass. What is important is to use the isa universal method available to all classes. I test whether the object I created is actually a subclass to my class.

1 Comment

Thanks for the great answer, was able to get some insight on OOP in perl. Anyway i think its a bit overkill for what im doing, but i will probably be doing something similar in the future
0

Previous answers cover what you are looking for, but I would probably add that Module::Runtime can be helpful if you'd rather not need to explicitly use()/require() each class' package when doing this sort of thing:

use Module::Runtime;
for my $cls (@classes) {
  my $obj = use_module($cls)->new;
  ...
}

Comments

-1
 use strict;
 use warnings;
 use class1;
 use class2;
 use class3;

 my @array = qw(class1 class2 class3);

 foreach my $c (@array)
 {

     my ($var1, $var2) = (12,34);
     my $temp = eval { $c->new };
     $temp->run($var1,$var2);
 }

untested but this is the sort of things you should be investigating. You do need to 'use' any class you are using and always use strict to save yourself hours future problems

3 Comments

you could do a no strict refs if you wanted but you are writing code on the fly by doing this. you need to do a if($@) after the eval to check that nothing has broke. the strict would be good for the rest of the code and is rule 1 in perl
:/ The point i prefer is to use eval as less as possible. And its very useless here for me :)
@Paulchenkiller The strict and eval are in no way connected. The eval {BLOCK} is simply Perl's error handling mechanism, there is no reason not to do proper error handling. However, the usage of eval in this post defeats it usage: If the $c->new fails (because $c->can("new") is false), then $temp will be undef, and calling a method on an undef value is a fatal exception. Paulchenkiller's post shows a better approach (he should maybe test for $class->can("new")), but eval { $c->new } || next is equaly acceptable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.