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:

[% FOREACH file IN css %]<link rel="stylesheet" href='[%c.uri_for("/ro +ot/static/css/$file.css")%]' />[%END%] [% FOREACH file IN js %]<script src='[%c.uri_for("/root/static/js/$fil +e.js")%]'></script>[%END%]
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:
sub register :Local { ... push @{$c->stash->{js}}, qw/ jquery-1.7.2.min jquery-ui-1.8.20-custom.min jquery.form jquery.validate phone-validate /; push @{$c->stash->{css}}, qw| ui-theme/style forms |; }
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.
sub include :Private { my ($self, $c, @include) = @_ foreach (@include) { my %links = ( css => { ui => ["aristo/theme"], bracket => [qw/ bracket match /], datetime => ["datetimepicker"], table => ["themes/blue/style"], }->{$_}, js => { jq => ["jquery-1.7.2.min"], ui => ["jquery-ui-1.8.20.custom.min"], form => [qw/jquery.form jquery.validate.min phone-vali +dation/], table => ["jquery.tablesorter.min"], datetime => [qw/ jquery-ui-timepicker-addon jquery-ui- +slider-access/], bracket => ["bracket"], swap => ['jquery.swap'], }->{$_} ); push @{$c->stash->{css}}, $_ foreach (@{$links{css}}); push @{$c->stash->{js}}, $_ foreach (@{$links{js}}); } }


$,=qq.\n.;print q.\/\/____\/.,q./\ \ / / \\.,q.    /_/__.,q..
Happy, sober, smart: pick two.

In reply to Bad practice or duct-tape hack? by xyzzy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.