[Petal] [PATCH] Options via constructor rather than package globals

Steve Purkis spurkis at mkdoc.com
Thu Jun 5 11:26:34 BST 2003


Hi Grant,

I think this is an excellent idea.  After reading through the patch it 
looks to me like you've covered everything - good job!  The only 
suggestions I can propose are on OO-style:

1. a base class for all Petal objects, ie: Petal::Object, or
    Petal::Base.  To begin with, this would contain new() and
    _initialize(), which are easily overrided.

2. cascade-able accessor methods which let you do:

	$template
	  ->language('fr')
	  ->input('HTML')
	  ->output('XHTML');

3. use accessors within _initialize().  In the off-chance that
    someone comes along and sub-classes Petal :)

Additionally, you might get rid of a lot of cut-n-paste work in 
_initialize() by doing something like:

	...
	foreach my $param (qw( taint disk_cache ... )) {
         no strict 'refs';
		my $val = exists $o{$param}
		  ? $o{param}
		  : ${'Petal::' . uc(param)
		$self->$param( $val );
	}

And as for implementing (2), at the moment I do something like this:

	sub language {
		my $self = shift;
		if (@_) {
			$self->{_language} = shift;
			return $self;
		} else {
			return $self->{_language};
		}
	}

Which is a bit more long-winded, but useful.

Again, good work.  Lets hope Jean-Michel agrees ;-)

-Steve

On Monday, June 2, 2003, at 10:49  am, Grant McLean wrote:

> Hi All
>
> A couple of weeks back, I proposed a patch to allow Petal options
> to be set via arguments to the constructor rather than via global
> variables in the Petal package namespace.  A patch implementing
> that change is attached.
>
> Two small changes to the test scripts were required.  In both
> cases a global variable was changed _after_ the template object
> was created but the tests assumed the change _would_ be reflected
> in the template output - precisely the type of situation this
> patch is intended to protect against.  Aside from those two cases,
> all other tests still pass.
>
> The patch also adds six test scripts to exercise the functionality
> of the new constructor arguments.
>
> I bumped the version number up to 0.93_01 to indicate a developer
> release.
>
> In case the patch attachment doesn't make it through the mailing
> list software, both the patch file and a complete patched
> distribution are available here:
>
>   http://www.web.co.nz/~grantm/petal/petal_options.patch
>   http://www.web.co.nz/~grantm/petal/Petal-0.93_01.tar.gz
>
> I'd appreciate any feedback people might have.
>
> Regards
> Grant



More information about the Petal mailing list