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

Discussion in 'Perl Misc' started by cartercc, Oct 2, 2008.

  1. cartercc

    cartercc Guest

    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.
     
    cartercc, Oct 2, 2008
    #1
    1. Advertising

  2. cartercc

    Ben Morrow Guest

    Quoth 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.

    <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. ]
     
    Ben Morrow, Oct 2, 2008
    #2
    1. Advertising

  3. cartercc

    C.DeRykus Guest

    On Oct 2, 11:50 am, cartercc <> 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
     
    C.DeRykus, Oct 3, 2008
    #3
  4. C.DeRykus wrote:
    > On Oct 2, 11:50 am, cartercc <> 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
     
    RedGrittyBrick, Oct 3, 2008
    #4
  5. cartercc

    C.DeRykus Guest

    On Oct 3, 2:12 am, RedGrittyBrick <>
    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
     
    C.DeRykus, Oct 3, 2008
    #5
  6. cartercc

    cartercc Guest

    On Oct 3, 5:12 am, RedGrittyBrick <>
    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
     
    cartercc, Oct 3, 2008
    #6
  7. cartercc <> 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
     
    Jürgen Exner, Oct 3, 2008
    #7
  8. cartercc

    cartercc Guest

    On Oct 3, 9:22 am, Jürgen Exner <> wrote:
    > cartercc <> 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
     
    cartercc, Oct 3, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    341
  2. Replies:
    22
    Views:
    763
    peter koch
    Apr 30, 2008
  3. Replies:
    6
    Views:
    352
    James Kanze
    Apr 29, 2008
  4. Navindra Umanee

    strong ref from weak ref?

    Navindra Umanee, Feb 12, 2005, in forum: Ruby
    Replies:
    2
    Views:
    145
    Navindra Umanee
    Feb 12, 2005
  5. Juha Nieminen
    Replies:
    13
    Views:
    621
    Edek Pienkowski
    Aug 29, 2012
Loading...

Share This Page