mrguy123 has asked for the wisdom of the Perl Monks concerning the following question:
Program 2 (demo1.pm)#!/usr/bin/perl use strict; use demo2; use demo1; { &poly("demo1"); } sub poly { my ($temp) = @_; my $object = new $temp; ##A function that is used in both objects $object->test; }
Program 3 (demo2.pm)package demo1; sub new { my $class = shift; my $self = {class => $class}; bless ($self,$class); return $self; } sub test { print "demo1\n"; } 1;
In this example, I can create a dynamic object according to the input I get for sub poly, sent from the main function (or another program in real life). If I send demo1, the program prints demo1, and vice versa for demo2.package demo2; sub new { my $class = shift; my $self = {class => $class}; bless ($self,$class); return $self; } sub test { print "demo2\n"; } 1;
Thanks for all you helpmy $temp = "demo1.pm"; require $templ
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Creating a dynamic object in Perl
by Corion (Patriarch) on Jan 07, 2008 at 15:29 UTC | |
by mrguy123 (Hermit) on Jan 07, 2008 at 15:41 UTC | |
|
Re: Creating a dynamic object in Perl
by spx2 (Deacon) on Jan 07, 2008 at 16:39 UTC |