h0mee has asked for the wisdom of the Perl Monks concerning the following question:
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:my $html =<<HTML; <html>This is $crap that the $client told me to $spit out.</h +tml> HTML
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...$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;
With this quick bit of clueless code, we can now write some cleaner scripts:package HackedBind; sub TIESCALAR { my ($class, $func_ref) = @_; $class = $func_ref; return bless \$class; } sub FETCH { my $self = shift; return &{$$self}; } 1;
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... --homeeuse 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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: HTML, Tieing and Late Binding
by davorg (Chancellor) on Jun 05, 2001 at 13:26 UTC | |
|
Re: HTML, Tieing and Late Binding
by Vynce (Friar) on Jun 05, 2001 at 13:28 UTC | |
|
Re: HTML, Tieing and Late Binding
by Anonymous Monk on Jun 05, 2001 at 23:17 UTC |