[DCPM] shorthand required

Neil Williams linux at codehelp.co.uk
Thu Feb 15 10:39:54 PST 2007


On Thu, 15 Feb 2007 17:55:28 +0000
Steve Marvell <steve at devon-it.co.uk> wrote:

> I'm not sure what the question is, since the example is so complex. Do
> you have a reduced version of the question/questions?
>
> Steve

Take a single parameter struct:

if ($g->{type} eq 'pilot_address')
{
	my $c = new Contact;
	$c->entryCity($g->{'string'}->[0]->{content});
}

foreach my $a (@contacts)
{
	$writer->startTag('string', 'type' => 'entryCity');
	$writer->characters($a->entryCity) if ($a->entryCity);
	$writer->endTag('string');
}

struct (Contact => {
	"entryCity" => '$',
}

The struct is fixed (it's based in C code). What I want to do is
compress this code because each element is intrinsically predictable
from the struct itself - provided certain meta data is preserved. I
have at least 12 objects, each with 10-20 parameters. So far, that has
led to a module exceeding 1,000 lines and I haven't added the SQLite
code yet.

e.g. is there any way of setting "entryCity" just once?

Can a hash or array be used to specify "entryCity" everywhere except
the struct? That would mean using a variable to declare a method.
Instead of $a->entryCity, some form of $a->$b. I want to stop having to
repeat each parameter handler:

$writer->startTag('string', 'type' => 'entryCity');
$writer->characters($a->entryCity) if ($a->entryCity);
$writer->endTag('string');
$writer->startTag('string', 'type' => 'entryCustom4');
$writer->characters($a->entryCustom4) if ($a->entryCustom4);
$writer->endTag('string');
$writer->startTag('string', 'type' => 'entryPhone1');
$writer->characters($a->entryPhone1) if ($a->entryPhone1);
$writer->endTag('string');

and have some form of:

foreach $foo (keys %bar)
{
	$writer->startTag($bar->type, 'type' => $foo)
	$writer->characters($foo) if ($foo);
	$writer->endTag($bar->type);
}

then a similar loop for setting the value in the struct:

	$c->$foo($g->{$bar->type}->[0]->{content});

That would reduce the module size by maybe 75%.

The full code, including a test routine, is available in CPAN.

--

Neil Williams
=============
http://www.data-freedom.org/
http://www.nosoftwarepatents.com/
http://www.linux.codehelp.co.uk/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://mail.pm.org/pipermail/devoncornwall-pm/attachments/20070215/e0a7f0af/attachment.bin 


More information about the Devoncornwall-pm mailing list