Dynamic object creation

B

bingo

Hi all,
I was wondering if anyone could help me with this one. I need to create
objects in Perl at runtime ...
Users enter the type of class ex.: (ClassA classB classC) which are all
children of classX
When i need to create an object i do

$handler = ClassX::ClassA->new();

Any hints on how i can create objects at runtime ?

Thanks for the help.

M.
 
B

bingo

The only thing i can think of at this moment is using tie() on the user
input

such as:
my $input = <INPUT>; # user enters ClassA, ClassB or CLassC
....
tie $input, $input

Any suggestions ??

Thanks
 
A

A. Sinan Unur

Hi all,
I was wondering if anyone could help me with this one. I need to create
objects in Perl at runtime ...
Users enter the type of class ex.: (ClassA classB classC) which are all
children of classX
When i need to create an object i do

$handler = ClassX::ClassA->new();

Any hints on how i can create objects at runtime ?

First off, you need to make sure that you are not accepting arbitrary
user input which you then evaluate blindly.

#!/usr/bin/perl

package X;

use strict;
use warnings;

sub whoami { ref shift }

package X::A;

use strict;
use warnings;

use base 'X';
sub new { bless { } => shift }

package X::B;

use strict;
use warnings;

use base 'X';
sub new { bless { } => shift }

package main;

use strict;
use warnings;

my %ok = map { $_ => 1 } qw( A B );

while ( my $class = <STDIN> ) {
chomp $class;
next unless exists $ok{$class};
eval {
print "X::$class"->new->whoami, "\n";
};
warn $@ if $@;
}

__END__
 
A

anno4000

bingo said:
Hi all,
I was wondering if anyone could help me with this one. I need to create
objects in Perl at runtime ...
Users enter the type of class ex.: (ClassA classB classC) which are all
children of classX
When i need to create an object i do

$handler = ClassX::ClassA->new();

Any hints on how i can create objects at runtime ?

Object creation usually happens at run time. Usually, the class an
object will be created in is determined at compile time, being specified
as a bareword. However, the class can also be specified as a simple
variable. So (untested)

my $class_this_time = 'ClassX::' . $user_input;
my $handler = $class_this_time->new( ...);

Anno
 
B

bingo

Works like a charm,
It is the "X::$class" that i was missing. I was evaluating the call
this way:
"X::"."$class"->new

This dosent work, but "X::$class" works.

I still cannot understand why the first construct gets the job done and
not the second.

Thx Sinan Unur,

Mahdi
 
P

Paul Lalli

bingo said:
Works like a charm,
It is the "X::$class" that i was missing. I was evaluating the call
this way:
"X::"."$class"->new

This dosent work, but "X::$class" works.

I still cannot understand why the first construct gets the job done and
not the second.

Does this make it clearer?

$ perl -MO=Deparse,-p -e'"X::"."$class"->new '
('X::' . "$class"->new);
-e syntax OK
$ perl -MO=Deparse,-p -e'("X::"."$class")->new '
('X::' . "$class")->new;
-e syntax OK
$ perl -MO=Deparse,-p -e'"X::".("$class"->new) '
('X::' . "$class"->new);

Or how about this, from `perldoc perlop`?
Perl operators have the following associativity and
precedence, listed from highest precedence to lowest.
<snip>
left terms and list operators (leftward)
left ->
nonassoc ++ --
right **
right ! ~ \ and unary + and -
left =~ !~
left * / % x
left + - .
<snip>

Paul Lalli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,055
Members
47,659
Latest member
salragu

Latest Threads

Top