Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Can I use a function ref to call a function in a web script?

Reply
Thread Tools

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

 
 
cartercc
Guest
Posts: n/a
 
      10-02-2008
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.


 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      10-02-2008

Quoth cartercc <(E-Mail Removed)>:
> 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.

<snip>
>
> 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

--
Heracles: Vulture! Here's a titbit for you / A few dried molecules of the gall
From the liver of a friend of yours. / Excuse the arrow but I have no spoon.
(Ted Hughes, [ Heracles shoots Vulture with arrow. Vulture bursts into ]
'Alcestis') [ flame, and falls out of sight. ] http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
C.DeRykus
Guest
Posts: n/a
 
      10-02-2008
On Oct 2, 11:50 am, cartercc <(E-Mail Removed)> wrote:
> 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

--
Charles DeRykus




 
Reply With Quote
 
RedGrittyBrick
Guest
Posts: n/a
 
      10-03-2008

C.DeRykus wrote:
> On Oct 2, 11:50 am, cartercc <(E-Mail Removed)> wrote:
>> 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
>


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

Is there anything inherently wrong with this?

--
RGB
 
Reply With Quote
 
C.DeRykus
Guest
Posts: n/a
 
      10-03-2008
On Oct 3, 2:12 am, RedGrittyBrick <(E-Mail Removed)>
wrote:
> C.DeRykus wrote:
>...
>
> > 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?
>


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.

--
Charles DeRykus

 
Reply With Quote
 
cartercc
Guest
Posts: n/a
 
      10-03-2008
On Oct 3, 5:12*am, RedGrittyBrick <(E-Mail Removed)>
wrote:
> 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
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      10-03-2008
cartercc <(E-Mail Removed)> wrote:
>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
 
Reply With Quote
 
cartercc
Guest
Posts: n/a
 
      10-03-2008
On Oct 3, 9:22*am, Jürgen Exner <(E-Mail Removed)> wrote:
> cartercc <(E-Mail Removed)> wrote:
> >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?


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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Giving an rvalue ref to a function taking an rvalue ref Juha Nieminen C++ 13 08-29-2012 09:25 PM
passing ref to ptr again as ref to ptr.... osama178@gmail.com C++ 22 04-30-2008 10:42 AM
passing ref to ptr again as ref to ptr.... osama178@gmail.com C++ 6 04-29-2008 08:09 AM
passing ref to ptr again as ref to ptr.... osama178@gmail.com C++ 0 04-24-2008 08:23 PM
strong ref from weak ref? Navindra Umanee Ruby 2 02-12-2005 05:06 PM



Advertisments