Intermediate/Advanced perl/applescript crossover question

I was working with records and I have a prototype script for helping me use a given closure a little bit more naturally… here’s the library to start:


on record_keys_string(r)
	try
		r as string
	on error message
		set echo to "echo "" & message & """
		set perl to "| perl -pe 's/^.*\{(.*\}).*$/$1/;s/:.*?[,}]//g;s/ /, /g;'"
		do shell script echo & perl
		return result
	end try
end record_keys_string

on record_list_string(r)
	try
		r as string
	on error message
		set echo to "echo "" & message & """
		set perl to "| perl -pe's/^.*\{(.*)\}.*$/$1/;s/:/, /g;'"
		do shell script echo & perl
		return result
	end try
end record_list_string

on record_keys_list(r)
	set text item delimiters to ", "
	words of record_keys_string(r)
end record_keys_list

on record_list_list(r)
	set text item delimiters to ", "
	words of record_list_string(r)
end record_list_list

on record_values_list(r)
	return r as list
end record_values_list

on record_values_string(r)
	set text item delimiters to ", "
	return (record_values_list(r) as string)
end record_values_string

on list_to_record(l)
	script hackery
		return {«class usrf»:l}
	end script
	return run script hackery
end list_to_record

on list_search(l, s)
	repeat with i from 1 to count l
		if item i of l is s then
			return i
		end if
	end repeat
	return null
end list_search

on value_for_key(r, k)
	set keys to record_keys_list(r)
	set i to list_search(keys, k)
	return item i of record_values_list(r)
end value_for_key

on key_for_value(r, v)
	set values to record_values_list(r)
	set i to list_search(values, v)
	return item i of record_keys_list(r)
end key_for_value

The problem is that it’s really fragile! Can somebody help with the regexen for the case where labels are in pipes (“|”) and can contain anything, for when the value contains commas or colons, and finally as a wish list, does any one know how to get the names of classes for raw data ( «class bunN» → “bundle” etc.)