You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by DeWitt Clinton <de...@avacet.com> on 2000/06/03 00:39:36 UTC
[OT ANNOUNCE] Beans-0.01
Hi,
This may be one of the strangest programming things I've ever tried to
do. Because I've recently been introduced (somewhat embarrassingly)
to the joys of Java Beans, I thought "hey, wouldn't this be great to
have in Perl, too?"
Well, since I'm masochistic like that, I've written a very partial
implementation of the Java Beans framework in Perl.
It only supports properties for the most part, so it is hardly ready
for serious projects. But the framework is in place, and over time
this may actually turn out to have been a good use of time.
I'm including the README in this mail. You can grab the 0.01 version
of Perl Beans at the following location:
http://unto.net/archives/Beans-0.01.tar.gz
If anyone out there in the world thinks this idea has any merit
whatsoever, I'll probably create a SourceForge project for it and
submit it to CPAN. As always, feedback, advice, and criticism are
always welcome.
-DeWitt
README:
Copyright (C) 2000 DeWitt Clinton <de...@avacet.com>, Avacet, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
NAME
Beans::*
DESCRIPTION
Beans is a (very) partial implementation of the Java Beans framework
INSTALLATION
perl Makefile.PL
make
make test
make install
NOTES
The implementation is not even close to being complete. While a
number of class stubs have been provided, most of the interesting
parts are still not functional.
In particular, the entire event model is absent, along with the
persistence mechanisms. Additionally, since this is at proof of
concept stage, not effort has been put into making these classes
performant. Numerous caching techniques could be put in place to
speed things up.
However, over time there is a reasonable chance that the missing
pieces are filled in. Feel free to contribute!
USAGE
A very simple example is as follows (please excuse the formatting):
First, define a class that has methods getFoo and setFoo in
accordance with the Beans convention.
package GenericBean;
sub new {
my ($proto) = @_;
my $class = ref($proto) || $proto;
my $self = {};
bless ($self, $class);
return $self;
}
sub getFoo {
my ($self) = @_;
return $self->{_foo};
}
sub setFoo {
my ($self, $foo) = @_;
$self->{_foo} = $foo;
}
sub getBar {
my ($self) = @_;
return $self->{_bar};
}
sub setBar {
my ($self, $bar) = @_;
$self->{_bar} = $bar;
}
First, create an instance of the GenericBean class:
use GenericBean;
my $genericBean = new GenericBean();
Now, you can use the Beans classes to reflect on a run-time instance
of that class. Since Perl doesn't have exactly the same notion of
Class that Java does, the semantics are a little different.
use Beans::Class;
use Beans::Introspector;
use Beans::BeanInfo;
use Beans::PropertyDescriptor;
use Beans::Method;
my $genericBeanClass =
Beans::Class::getClassFromInstance($genericBean);
my $beanInfo =
Beans::Introspector::getBeanInfo($genericBeanClass);
my @propertyDescriptors = $beanInfo->getPropertyDescriptors();
You can iterate over the property descriptors and actually write
values to each property:
my $i;
foreach my $propertyDescriptor (@propertyDescriptors) {
my $writeMethod = $propertyDescriptor->getWriteMethod();
$writeMethod->invoke($genericBean, $i);
$i++;
}
Or, read those values back out again:
foreach my $propertyDescriptor (@propertyDescriptors) {
my $readMethod = $propertyDescriptor->getReadMethod();
my $propertyValue = $readMethod->invoke($genericBean);
my $propertyName = $propertyDescriptor->getName();
print "$propertyName: $propertyValue\n";
}
SEE ALSO
Sun's Java Beans tutorial:
http://www.javasoft.com/docs/books/tutorial/javabeans/index.html
Sun's JDK 1.3 API documentation for the java.beans package:
http://java.sun.com/j2se/1.3/docs/api/java/beans/package-summary.html