in reply to Foo::Button->new vs. newButton()

I not a fan of the idea of exporting procedural-style class constructors from modules, since it kind of hides the OO, and a symbol like "newButton" could be handy to users of your library elsewhere. IMHO exporting symbols can get real messy, real fast.

There is always the not-so-great indirect object syntax:

$b = new Foo::Button @args
but that can sometimes be more trouble than its worth.

What about the syntax length bothers you? Are your modules nested deeply? (ex: Olga::Ming::Widgets::Button) If so, you could create your own class loader for your framework. I have done this with the object framework I have been working on and it works quite well. Here is the syntax :

use iI::Framework qw( Web.Request Web.ModPerl.DispatchHandler );
Then the iI::Framework module's import method simply changes all the "."s to "/"s and requires each module. After that the modules can then be refered to as "Request" and "DispatchHandler" instead of "iI::Web::Request" and "iI::Web::ModPerl::DispatchHandler". Its a little weird (maybe not terribly perl-ish), but it keeps my module name length down and still allows me to organize the almost 100 different classes I have into a neat tidy folder heirarchy.

-stvn

Replies are listed 'Best First'.
Re: Re: Foo::Button->new vs. newButton()
by bakunin (Scribe) on Jan 16, 2004 at 19:52 UTC
    No, no the class names are not long at all. But thanks for the input. Well, it hides OO to some point. And I should of course put the symbol exported in EXPORT_OK.

    As for the length, well... this is a static method. And no inheritance is needed beyond Foo::Button along the library. So why write Foo::Button->new and many others (for Text,Menu,FadeMenu,MovieClip,... ) where I can just get away with newFoo()
    It was just a thought. :))
    Thanks.

      Just because you dont inherit it, doesn't mean someone else wont want to.

      -stvn

        Incidentally, if you wanted to get that syntax elsewhere you could provide an autoload routine:-

        sub AUTOLOAD { my $f = $AUTOLOAD; $f=~s/.*:://; if ($f =~ /^new(.*)/) { return $1->new(@_); } else { die "Undefined subroutine: $f"; } }

        I don't think it's a very good idea though, and that wouldn't give you a Foo::Button, just a button. Probably not good at all :) You can do all sorts of interesting things with AUTOLOAD