I'm finishing a Perl Module that implements the popular way to declare classes in Perl. The idea came from the Java programmers that we have here, that thinks that the way to declare classes in Perl is ugly, soo I have used Filter::Simple to declare the classes in Perl similar to Java. Now the Java programmers can work better with Perl.

Before release it I want the opinion of the monks, and some help to find a good name for the Perl Module.

Example of class:

class Foo extends Bar { use LWP::Simple qw(get) ; ## import the method get() to this package +. vars ($GLOBAL_VAR) ; ## same as: use vars qw($GLOBAL_VAR); my ($local_var) ; ## constructor/initializer: sub Foo { $this->{attr} = $_[0] ; } ## method with input variables declared: sub get_pages ($base , \@pages , \%options) { my @htmls ; if ( $options{proxy} ) { ... } foreach my $pages_i ( @pages ) { my $url = "$base/$pages_i" ; my $html = get($url) ; push(@htmls , $html) ; $this->cache($url , $html) ; } return @htmls ; } ## method like a normal Perl sub: sub cache { my ( $url , $html ) = @_ ; $this->{CACHE}{$url} = $html ; } }
## Example of use of the class:
package main ; my $foo = new Foo(123) ; $foo->get_pages('http://www.perlmonks.com/', ['/index.pl','/foo'] , +{proxy => 'localhost:8080'}) ;
As you can see you don't need to declare the method new and the variable $this in each method. The inputs of a method can also be declared in the header of the sub, soo you don't need to make:
sub test { my $this = shift ; my ( $arg1 , @list ) = @_ ; $this->other(...); }
You just make:
sub test ($arg1 , @list) { $this->other(...); }
Other good thing is to can declare the reference to an Array or Hash, and already have a variable loading them:
sub testref ($arg1 , \@list , \%hash) { $this->other(...); } ## use: $object->testref("foo" , [1,2,3] , {k1 => 1})
To delcare the method above in the normal way you need to do all of this:
sub testref { my $arg1 = $_[0] ; my @list = @{$_[1]} ; my %hash = %{$_[2]} ; $this->other(...); }
You can see that all the objects are a HASH reference, soo, all the attributs stay at $this->{keyx} ;

I will appreciate any type of feedback and suggestions for new things.

The purpose of this module is to bring an easy way to declare classes in Perl, and with less code.

Update: multiple-inheritance implemented:

class Foo extends Bar, Baz {...}

Graciliano M. P.
"Creativity is the expression of the liberty".

  • Comment on new Class:: module to declare classes in Perl in the popular way: class foo extends bar { sub x($var){..} }
  • Select or Download Code

Replies are listed 'Best First'.
Re: new Class:: module to declare classes in Perl in the popular way: class foo extends bar { sub x($var){..} }
by liz (Monsignor) on Oct 05, 2003 at 07:50 UTC
    The parameter tricks you're playing with sound a lot like what Perl6 is going to allow (although I must admit my knowledge on the current state of things ia a little foggy at the moment).

    A CPAN search reveals:

    It would seem to me that the name of your module(s) has already been chosen ;-)

    Liz

      The first thing that I have made was to test Perl6::Classes, but it doesn't work, since is bery easy to crash it! Soo, I started to write one, and found a lot of things that you have to made to avoid errors on Filter::Simple, specially whe you are working with quotes and comments (comments are not parsed on Filter::Simple).

      For now I have a stable code, and have tested with a lot of modules, and some classes writed on it.

      Note that I'm using some extra things in my module, like the input arguments and html blocks. Note that the idea is not to work with Perl6 concepts, and just have an easier way to declare classes in pure Perl5.

      I'm still looking for a name...

      Graciliano M. P.
      "Creativity is the expression of the liberty".

Re: new Class:: module to declare classes in Perl in the popular way: class foo extends bar { sub x($var){..} }
by demerphq (Chancellor) on Oct 06, 2003 at 16:32 UTC

    Personally I think that this looks much more ugly than Perls way to do things. Also how does it handle multiple-inheritance? My understanding from the CB is that Java doesnt do MI, but it is certain that Perl does....


    ---
    demerphq

      First they ignore you, then they laugh at you, then they fight you, then you win.
      -- Gandhi


      Well, you still can do push(@ISA,Bar);, but I can add class Foo extends Bar , Baz {}

      Graciliano M. P.
      "Creativity is the expression of the liberty".

Re: new Class:: module to declare classes in Perl in the popular way: class foo extends bar { sub x($var){..} }
by bakunin (Scribe) on Oct 06, 2003 at 22:09 UTC
    First of all to those Java folks, THERE IS NOTHING WRONG WITH OUR OO WAYS... It's not ugly, OK?? :)))))
    I believe you made a very good job. The "P" of Perl stands for Practical. And "practicality" is a relative term, especially in the programming realm.
    If Java programmers want or need perl, but don't like the interface, if they think it is not "practical", someone like gmpnassos will come up, and make Perl "practical" for them. Perl, by nature, lets anyone to do that!
    And for the name... Hmmmmmm, actually, I can't even name my own subroutines. Here's one,though: Class::JavaStyle


    Good Luck, work on it more, and make Perl proud :))))
      Thanks, but JavaStyle? Please, keep out of the name Java! ehehehe

      The declaration style for class that I have implemented in Perl is far way from Java resources.

      I use Java and create development resources for it, but this doesn't means that I like it. Java is good as a language concept for OO, but in terms of be Pratical... Well, we have Perl, why think about this! ;-P

      Graciliano M. P.
      "Creativity is the expression of the liberty".