dynamically adding member sub to object?

E

Eckstein C.

Take this code:

Code:
package SomeClass;

sub new {
my $this = shift;
my $class = ref($this) || $this;
my $obj; # { Val = 1 };
$obj->{'SomeProp1'} = '';
$obj->{'SomeProp2'} = '';

bless $obj, $class;
return $obj;
}

package main;

print "<pre>" if $ENV{'HTTP_HOST'};

my $c = new SomeClass;

(The above is just an example, and only for the purposes of my question,
more below.)

The /actual/ class and construction code are out of my hands (generated
by COM objects or sorts,) in a program that allows for Perl scripting,
mostly used for event handling, and provides "built in" objects, such as
$Server (Server object for the script's context), $Window (currrent
Window object), etc.

Ok, here is ever my question comes in. "read only properties" are member
subs, ie $Server->Name;, while "read/write properties" are
$Server->{SomeProp};

What I want to do is go through each "property" of a given $Obj and
(dynamically) create a (lvalue) sub for each one (added to the
"properties" object of course.)

Code:
sub SomeProp : lvalue {
my $this = shift;
$this->{'SomeProp'};
}

OR

sub SomeProp : lvalue { $_[0]->{'SomeProp'}; }


Is this possible? (Without using eval, as the scripts in this app seem
to be wrapped in a eval behind the scenes, evident when theres an error
in the code, in the error mesages. )

Thanks.
 
J

John Bokma

Eckstein C. said:
Is this possible? (Without using eval, as the scripts in this app seem
to be wrapped in a eval behind the scenes, evident when theres an error
in the code, in the error mesages. )

I would inherit from "SomeClass", and use AUTOLOAD.
 
E

Eckstein C.

John said:
I would inherit from "SomeClass", and use AUTOLOAD.

I wanted to avoid that, and I wanted to loop through the "properties"
(hash items) and dynamically generate a method for that class on the
fly, but I can't find any info anywhere (perldoc, google, etc.)
 
J

John Bokma

Eckstein C. said:
I wanted to avoid that, and I wanted to loop through the "properties"
(hash items) and dynamically generate a method for that class on the
fly, but I can't find any info anywhere (perldoc, google, etc.)

use strict;
use warnings;


package SomeClass;

sub new {

my $class = shift;
my $self = {

'foo' => 42,
'bar' => 13,
};

bless $self, $class;
return $self;
}



package main;

{
package SomeClass;

use Carp;

sub AUTOLOAD {

my $self = shift;
my $type = ref $self;
my $autoload = our $AUTOLOAD;
my $name;
( $name = $autoload ) =~ s/.*://;

exists $self->{ $name }
or croak "Can't access '$name' in class $type";

@_ and return $self->{ $name } = shift;

return $self->{ $name };
}
}


my $sc = new SomeClass;

print $sc->foo, "\n";
$sc->foo( $sc->bar );
print $sc->foo, "\n";

HTH,

(PS: I don't recommend to do this, use inheritance instead)
 

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,176
Messages
2,570,949
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top