[DCPM] Creating a module of objects

Neil Williams linux at codehelp.co.uk
Wed Jan 10 14:50:34 PST 2007


Starting with a few structs, I'm parsing an XML file to create objects
for each, populating the objects and pushing the objects onto an array.
I then insert that array into a hash, using the object name as the key,
so that I have a single variable to return to the script. This works
fine when all this is local to the current script but I want to reuse
these objects now and I fancy creating a new module to do it. It may
even turn up on CPAN eventually.

Example struct:

struct (Expense => {
	form_of_payment => '$',
	distance_unit => '$',
	expense_vendor => '$',
	expense_city => '$',
	expense_attendees => '$',
	category => '$',
	expense_note => '$',
	type_of_expense => '$',
	guid => '$',
	expense_amount => '$',
	expense_date => '$',
	currency_code => '$',
	kvp_mnemonic => '$',
	kvp_string => '$',
	kvp_fraction => '$',
});


sub QSFParse {
	my $file = $_;
	my $xs1 = XML::Simple->new();
	my $doc = $xs1->XMLin($file, forcearray => [ 'guid' ]);
	parse_objects($doc);
	$object_list{'pilot_address'} = @contacts;
	$object_list{'pilot_expenses'} = @expenses;
	$object_list{'gpe_expenses'} = @expenses;
	$object_list{'pilot_datebook'} = @appointments;
	$object_list{'Split'} = @splits;
	$object_list{'Account'} = @accounts;
	$object_list{'Trans'} = @transactions;
	return %object_list;
}

parse_objects just creates and populates each object.

1. I'm missing an new() function and I'm not sure what should go in one
- the objects are created during the parse, new doesn't seem to have
anything to do.
2. How do I dereference the arrays in the script calling the module? If
I simply do: my @splits = $object_list{'Split'};
my @transactions = $object_list{'Trans'};
my @accounts = $object_list{'Account'};

my $trans_count = @transactions;
my $acc_count = @accounts;
my $split_count = @splits;

then each *_count == 1, whatever the incoming file contains.

Any good references on objects and modules?

--


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/20070110/d80f19b6/attachment.bin 


More information about the Devoncornwall-pm mailing list