Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

HTML, Tieing and Late Binding

by h0mee (Acolyte)
on Jun 05, 2001 at 13:11 UTC ( [id://85749]=perlquestion: print w/replies, xml ) Need Help??

h0mee has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: HTML, Tieing and Late Binding
by davorg (Chancellor) on Jun 05, 2001 at 13:26 UTC

    The Template Toolkit does a great job of isolating the logic from the presentation. The HTML page (in a template) contains a very simple formatting language which accesses varaibles that are set up in your Perl script. These variables can even be mapped to subroutines and it all "just works".

    It sounds like this will address the problems you're describing.


    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

Re: HTML, Tieing and Late Binding
by Vynce (Friar) on Jun 05, 2001 at 13:28 UTC

    it isn't what you asked for, but it's what you want.

Re: HTML, Tieing and Late Binding
by Anonymous Monk on Jun 05, 2001 at 23:17 UTC
    You can use @{[ your perl here ]} where you call your function like @{[func($something or $other)]}.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://85749]
Approved by root
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-17 03:08 GMT
Find Nodes?
    Voting Booth?

    No recent polls found