A while back I wrote a script to take care of an events schedule on a web site. It reads data from an XML file (such as title, date, description, URL, etc.) puts the data in a hash and rearranges it into HTML for output. Of course, it also watches the current date, and makes sure past events don't show up, and other tricks like that. It has been a real time-saver.
Lately I've been finding other potential uses for the script on the site. The problem is, I've got code in the script which is very specific for that particular XML file. For instance, the routines that turn the data into HTML - they expect very specific input:
Right now, if I want to handle a different XML file with different elements, I have to make a copy of my script and retrofit it.sub basic_list { my (%entry) = @_; if ($entry{'subtitle'} ne "") { $entry{'subtitle'} = "<h4>$entry{'subtitle'}</h4>" } print <<END; <li><h3>$entry{'title'}</h3>$entry{'subtitle'}$entry{'long_date'}<br/> +$entry{'description'}</li> END }
But I don't want to go making a dozen scripts which are 99% the same and 1% different. (I also don't want to muck about in my lovingly crafted script every time I want to change the way my HTML output looks.) I know there's got to be a better way to do this, to separate out the details and make one program that works with all the XML files I throw at it. It's probably something really simple that I just haven't learned yet.
What's my best strategy here? I toyed with the idea of putting specific subroutines like the one above in the XML file itself, letting my script read and execute them, but I'm not sure if that's really the way to go. Is that ever done?
In reply to one script, not twelve nearly identical ones! by drewbert
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |