[Petal] OT - method chaining

Steve Purkis spurkis at mkdoc.com
Thu Jun 5 16:36:11 BST 2003


On Thursday, June 5, 2003, at 03:04  pm, William McKee wrote:

> On Thu, Jun 05, 2003 at 02:47:50PM +0100, Steve Purkis wrote:
>> No, it's from Smalltalk.  (But then, Java copied everything from there
>> so... ;-)
>> By default, methods return the current object in Smalltalk.  It's
>> almost like an implicit 'with' statement...
>
> Thanks for the history lesson. I've heard good things about Smalltalk
> but don't grok this concept. It looks to me like you are calling
> multiple object methods which I suppose makes sense if the object is
> returned each time.

Yup, that's exactly what it's doing.


>  Nonetheless, I'm still of the opinion that it's atypical syntax for a 
> Perl module.

You might be surprised - I've seen this syntax across many modules on 
CPAN.


>  Simply passing the settings as a hash ref seems sufficient to me.

Hmm..  but that only applies to constructors & accessors.  You apply 
method chaining (or cascading) to any old method:

	my $doc = Browser->new->connect('foo.com')->send($HTTP_GET)->recv();

And combine it with statements like:

	my $dir = $document->language->direction;

And error handling (using die(), Carp, Error, etc..), you can say quite 
a lot in really obvious ways without worrying wether or not they're 
gonna work.

AFA Petal goes, I'd like to be able to say:

	my $string = Petal->new()->base_dir( '/tmp' )->process( %vars );

But maybe that's just me :)

Anyways, I have digressed.  And I should be working!  tsk tsk...

You're quite right that passing arguments to new() is sufficient.  It 
really amounts to the same thing.  I think it's just another one of 
those things that boils down to a matter of preference.

-Steve



More information about the Petal mailing list