Can I use a function ref to call a function in a web script?

C

cartercc

Can I assign a function to a scalar as a ref in a module file and call
the function by using the scalar in a heredoc?This is for a web app. I
have three components, a module, a CGI script, and a database. This is
a front end to a database which runs select, update, insert, and
delete queries and displays the results.

I have a bunch of standard functions in the module, such as
print_header(), print_footer(), print_link(), connect_to_database(),
etc., along with some global variables.

I have a CGI script that starts off like this:
use CGI;
use DBI;
use WebModule;
And mostly consists of a heredocs that output pure html.

And I have a database (Postgres).

The CGI script contains mostly forms for passing data to the
database. I'm using heredocs to spit out the html. I'm finding that I
am repeating a bunch of code, six times now, which looks like this:
<td><select name="postype">
<option>Classified</option>
<option>Faculty</option>
<option>Lecturer</option>
<option>PartTime</option>
<option>Professional</option>
<option>RFP</option>
<option>Vacant</option>
</select>
</td>

I can create a function that prints this to the html document, but
that involves ending the heredoc, calling the function, and then
starting the heredoc, like this:

print <<form;
<!-- pure html -->
form
print_select_element_in_form();
print <<form;
<!-- more pure html -->
form

Here is the question: Can I assign this function to a scalar ref in my
module file and call the function by using the scalar in the heredoc?
I'm slightly frustrated as I have spent most of the morning in a
futile attempt to do this (and I'm sure that I'm making some stupid
mistake, or maybe it can't be done.)

TIA, CC.
 
B

Ben Morrow

Quoth cartercc said:
Can I assign a function to a scalar as a ref in a module file and call
the function by using the scalar in a heredoc?This is for a web app.
The CGI script contains mostly forms for passing data to the
database. I'm using heredocs to spit out the html. I'm finding that I
am repeating a bunch of code, six times now, which looks like this:
<td><select name="postype">
<option>Classified</option>
<option>Faculty</option>
<option>Lecturer</option>
<option>PartTime</option>
<option>Professional</option>
<option>RFP</option>
<option>Vacant</option>
</select>
</td>

I can create a function that prints this to the html document, but
that involves ending the heredoc, calling the function, and then
starting the heredoc, like this:

print <<form;
<!-- pure html -->
form
print_select_element_in_form();
print <<form;
<!-- more pure html -->
form

Here is the question: Can I assign this function to a scalar ref in my
module file and call the function by using the scalar in the heredoc?
I'm slightly frustrated as I have spent most of the morning in a
futile attempt to do this (and I'm sure that I'm making some stupid
mistake, or maybe it can't be done.)

If the repeated HTML really is the same every time you can simply assign
it to a variable and interpolate that, but I guess you've worked that
out. Otherwise, the only way to interpolate random expressions is the
rather ugly

@{ [ print_select_element_in_form() ] }

which calls the function (in list context), builds an anon array out of
the results, and then interpolates that array.

You would be *much* better off using a real template system, with your
HTML in separate files from your code. It sounds to me like
HTML::Template would fit your needs nicely; if you need more power the
usual tool is Template.

Ben
 
C

C.DeRykus

Can I assign a function to a scalar as a ref in a module file and call
the function by using the scalar in a heredoc?This is for a web app. I
have three components, a module, a CGI script, and a database. This is
a front end to a database which runs select, update, insert, and
delete queries and displays the results.

I have a bunch of standard functions in the module, such as
print_header(), print_footer(), print_link(), connect_to_database(),
etc., along with some global variables.

I have a CGI script that starts off like this:
use CGI;
use DBI;
use WebModule;
And mostly consists of a heredocs that output pure html.

And I have a database (Postgres).

The CGI script contains mostly forms for passing data to the
database. I'm using heredocs to spit out the html. I'm finding that I
am repeating a bunch of code, six times now, which looks like this:
<td><select name="postype">
<option>Classified</option>
<option>Faculty</option>
<option>Lecturer</option>
<option>PartTime</option>
<option>Professional</option>
<option>RFP</option>
<option>Vacant</option>
</select>
</td>

I can create a function that prints this to the html document, but
that involves ending the heredoc, calling the function, and then
starting the heredoc, like this:

print <<form;
<!-- pure html -->
form
print_select_element_in_form();
print <<form;
<!-- more pure html -->
form

Here is the question: Can I assign this function to a scalar ref in my
module file and call the function by using the scalar in the heredoc?
I'm slightly frustrated as I have spent most of the morning in a
futile attempt to do this (and I'm sure that I'm making some stupid
mistake, or maybe it can't be done.)

Not much easier on the eyes but
you could pare down to a single
print with a stacked heredoc:

print <<FORM, func(), <<FORM;
foo bar
FORM
bat boom
FORM
 
R

RedGrittyBrick

C.DeRykus said:
Not much easier on the eyes but
you could pare down to a single
print with a stacked heredoc:

print <<FORM, func(), <<FORM;
foo bar
FORM
bat boom
FORM

my $optionlist=func(); print <<FORM
foo bar
$optionlist
bat boom
FORM

Is there anything inherently wrong with this?
 
C

C.DeRykus

C.DeRykus wrote:
...



my $optionlist=func(); print <<FORM
foo bar
$optionlist
bat boom
FORM

Is there anything inherently wrong with this?

Yes, that's better if there are multiple interpolations of the
same variable and then you don't
need to stack heredoc's at all.

But there was some doubt:

BenM> If the repeated HTML really
BenM> is the same every time you
BenM> can simply assign it to a
BenM> variable and interpolate
BenM> that ... otherwise, the only
BenM> way to interpolate random
BenM> expressions.

If random though, a stacked heredoc
with a single print is simpler
than the OP's code IMO. The suggested HTML::Template may be best in
the long run.
 
C

cartercc

my $optionlist=func(); print <<FORM
foo bar
$optionlist
bat boom
FORM

Is there anything inherently wrong with this?

No, there isn't. I had never used a $scalar to hold multi-line text
before, but following Ben's suggestion I assigned the entire <select>
element to a variable in the module and it worked just as I had
wanted. I didn't realize that you could do this, but now I know.

I've been playing with Lisp a lot for a few months, and am noticing a
certain amount of commonality between Lisp and Perl. Writing functions
that return data is (obviously) a common Lisp idiom, and I've been
trying to do the same in Perl. I guess that this is something that one
language supports but another language does not.

On another (longish) note, we seem to have a number of paradigms for
web applications. Roughly, they seem to fall into these categories
(the names are merely suggestive):
1. Flash - creating movies that stand in for web sites.
2. Visual (Dreamweaver, FrontPage), etc) - creating web sites by using
point and click tools.
3. Hand Coding - the way we used to do it.
4. Templating (PHP, ColdFusion, JSP, etc.) - writing web pages that
incorporate snippets of code. You write the HTML and where ever you
want functionality you insert a tag that does what you want.
5. Programming (CGI inc. Perl and Python) - writing programs that
produce HTML as output. You write the code and where ever you want
HTML you write a procedure that outputs HTML.

It seems to me that web apps that are data driven rely mostly on
Templating and Programming. By inclination and philosophy, I much
prefer Programming to Templating, although Templating seems to be used
much more than Programming. Maybe it's how we view user interfaces. I
see a user interface as incidental to the data and functionality and
like to focus on getting the requirements in code before considering
the user interface. Most of my colleagues seem to focus on the
interface first and concentrate on the functionality later (not that
we could ever make a clean cut distinction between functionality and
interface.)

Comments?

CC
 
J

Jürgen Exner

cartercc said:
I've been playing with Lisp a lot for a few months, and am noticing a
certain amount of commonality between Lisp and Perl. Writing functions
that return data is (obviously) a common Lisp idiom, and I've been
trying to do the same in Perl. I guess that this is something that one
language supports but another language does not.

Maybe I am a bit dense, but what would a function return if not data?

Ok, it could return a function if it is a HOF, but that's rather
uncommon.

jue
 
C

cartercc

Maybe I am a bit dense, but what would a function return if not data?

Code. Lisp syntax is very regular. For example
(foo (bar baz) (foo (foo baz bar) baz bar) foo foo (f up b all r))
could be function calls, lists of data, special operators, without any
distinction.
Ok, it could return a function if it is a HOF, but that's rather
uncommon.

Virtually every object in Lisp is a cons cell, that is, everything
whether code or data is a binary tree. Cons cells contain pointers,
and the object pointed to could be another pointer, a data value (e.g.
1), a data structure, or a function. In fact, Lisp doesn't make any
distinction between pointers, primitive values, data structures, or
functions -- they are all identical as far as Lisp is concerned. It's
up to the human user to make the distinction.

A Lisp macro returns code, so a function call to a macro produces
source code that could product source code, data, or any other Lisp
object.

What I previous said was ambiguous, so I owe you an apology.

CC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top