Bad practice or duct-tape hack?by xyzzy (Pilgrim)
|on Aug 13, 2012 at 17:45 UTC||Need Help??|
xyzzy has asked for the wisdom of the Perl Monks concerning the following question:
I don't make any attempts to hide my (occasional) disregard for "best practices" or logical clarity in code. I even admitted just how much I do so in a recent thread. In this post I want to present an example of something I did in an application I am writing for a paying client and ask the wiser, more orthodox monks to either explain how (and why) to make my methodology a little more kosher, or perhaps it really is an acceptable WTDI for my purposes.
At the bottom of this explanation is a Catalyst method to load CSS and JS files for my main template. The template wrapper has a section like this:
Each action would then push the files that needed to be included into arrayrefs in the stash. This worked at first but was somewhat cumbersome, and required me to remember the names of each file and which were needed for what functionality:
This quickly became a pain in the ass, but I didn't write a "solution" until this morning. I wanted to reduce the above a single statement like so: $c->forward('include',[qw/jq ui form/]);. I realized that the hash of include definitions should go either into the global YAML configuration file (if I used a YAML config file), or at least with the rest of the application configurations (in the main module for the package), but I don't know enough about how Catalyst works to be able to retrieve this configuration from within my Controller. I also considered that, since this is all template stuff, I should really write this into my config template file (I do have one) and then rewrite my wrapper template logic to pull things fro there, but I really didn't want to figure out how to do that, so I just made an anonymous hash inside the foreach loop that processes the arguments for the include action and passed each argument to it to get the needed files. Actually the anonymous hash is inside the assignment of another hash, which I'm sure adds to the computational complexity, processing time, references floating around behind the scenes, the same thing getting instantiated over and over for no reason, etc. But it makes perfect sense to me.
$,=qq.\n.;print q.\/\/____\/.,q./\ \ / / \\.,q. /_/__.,q..
Happy, sober, smart: pick two.