tied - hash question at perlmonks.org

Scott Penrose scottp at dd.com.au
Wed Oct 1 20:27:48 CDT 2003

Hash: SHA1

On Thursday, Oct 2, 2003, at 10:59 Australia/Melbourne, peter renshaw 

> found a small discussion on tied hashs on perlmonks.org - 
> http://www.perlmonks.org/index.pl?node_id=295518
> regs PR

Interesting discussion. I agree with the argument that you should be 
using OO.
But... as I discussed a few weeks ago, there are some times reasons to 
use a tied HASH

	* 3rd party code demands it (no access to make changes)
	* Existing code demands it (too damn hard to make changes)
	* Aesthetics demands it

The last is interesting, but if you want to provide a bit of data to a 
user in say Template Toolkit, where they can do...

	my data for key fred = 	[% data.fred %]
	now setting it to george. [% data.fred = 'george' %]
	now my data for fred = [% data.fred %]

Then for simplicity to the user (whom is generally not a hard core perl 
programmer) is a little easier then...

	my data for key fred = 	[% data.fred %]
	now setting it to george. [% data.fred.set('george') %]

There are even other alternatives - but for the purpose of handing on 
code to web developers I like the hash idea. I have also often used a 
hash of subs in template toolkit, so that users can have a hash that 
looks a little like this in perl...

	my %data = (
		'fred' => sub { $this->_callback_fred(@_); },
		'george' => sub { $this->_callback_george(@_); },

Each callback returns either an array or a hash, and then in TT land 
users can simply do things like this...

	[% FOREACH row = data.fred %]
		ID [% row.id %] = [% row.name %]
	[% END %]

or even....

	George First Name is [% data.george.first_name %]

The fact that data is a hash of subs that return a hash or array ref, 
vs a predefined data structure is then completely hidden from the 
template developer.

The reason I did this was to do it as a callback instead, so that I 
only had to generate the data (big database calls) if this particular 
template from this particular developer required it in this 
circumstance :-)

- -- 
Scott Penrose
VP in charge of Pancakes
scottp at dd.com.au

Dismaimer: If you receive this email in error - please eat it 
immediately to prevent it from falling into the wrong hands.

Please do not send me Word or PowerPoint attachments.
See http://www.fsf.org/philosophy/no-word-attachments.html
Version: GnuPG v1.0.6 (Darwin)
Comment: For info see http://www.gnupg.org


More information about the Melbourne-pm mailing list