Every once in a while I come across the problem where I want a perl script that spits out some HTML that is simple enough for a client/friend to maintain. It would be ideal to treat the HTML like a form or solid blah of data:
my $html =<<HTML; &lt;html&gt;This is $crap that the $client told me to $spit out.&lt;/h +tml&gt; HTML
Of course, inevitably, what happens is that the variable that are embedded inside of the quoted string has a good chance of changing due to user input, location in program, etc. It starts becoming a pain in the ass for your HTML literate, but not perl literate friend of yours to maintain a dynamic form when it starts looking like:
$html1 = <<DATA; blah blah $blah DATA $var = 'she is '.($some_cond) ? "so ugly" : "so phat"; $html2 = qq|This is your Mamma: $var|; $final = $html1.$html2.$html3;
etc. etc. Which comes to something I wish perl would have- some form of simple late binding of functions to scalars in instances like this when you don't want to break up your pretty HTML formatting with perl code: Ideally, you want your CGI scripts to look as much like a vbscript/jsp application as possible: HTML with embedded code, and not a perl script with HTML embedded inside of it. For the purposes of doing this, I'm playing with a very simple wrapper class for tying scalars to functions with- it's not as straightfoward as I would like to bind functions to scalars, but it works...
package HackedBind; sub TIESCALAR { my ($class, $func_ref) = @_; $class = $func_ref; return bless \$class; } sub FETCH { my $self = shift; return &{$$self}; } 1;
With this quick bit of clueless code, we can now write some cleaner scripts:
use CGI; use POSIX; $cgi = new CGI; require HackedBind; tie $clients, 'HackedBind', \&clients; tie $coders, 'HackedBind', \&coders; tie $timestamp, 'HackedBind', \&{return strftime('MM-DD-YY',localtime(time));}; sub clients { return ((scalar @$cgi->param('clients') > 1) ? join(",", $cgi->param('clients')) : $cgi->param('clie +nts')); } sub coders { return (scalar @$cgi->param('clients'))*2; } #now tell your client to ignore everything above and edit #below for changes: $data = <<DATA; The Time is $timestamp. Our clients are $clients. We have assigned $coders coders to the project. DATA
Is there a better way to do this? Is there a better way to do late variable binding? Am I missing anything obvious? Again, keep in mind that it's rather late, and I am heavily intoxicated... --homee

In reply to HTML, Tieing and Late Binding by h0mee

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.