Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: OOP in CGI

by davorg (Chancellor)
on Jul 29, 2005 at 08:58 UTC ( [id://479300]=note: print w/replies, xml ) Need Help??


in reply to OOP in CGI

Your second example doesn't actually work as you need to import some of the CGI subroutines into your namespace. You can do this by importing only the functions you need:

use CGI qw(header start_html p param);

Or by importing pre-defined sets of functions:

use CGI ':standard';

As others have pointed out, the problem with this approach is that it can lead to namespace pollution, so it's often important to spend time working out which sets of functions to import. As I almost never use the HTML generation routines from CGI.pm (prefering to create my output with a templating system), I often find myself using:

use CGI ':cgi';

As that imports all of the functions related to the CGI protocol. Sometimes I can even just get away with:

use CGI qw(param header);

As they are the functions I use most often.

I never use the OO version. I've never come across a situation where I've needed two CGI objects at the same time.

And as for your last point, you'll need to use Javascript for that. But your Javascript can, of course, be generated by your CGI program.

--
<http://www.dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

Replies are listed 'Best First'.
Re^2: OOP in CGI
by wink (Scribe) on Jul 29, 2005 at 09:20 UTC

    It was not meant to be a working fragment, merely an example of what I was talking about. :) I usually use

    use CGI qw(:standard);

    for my CGI apps (all two of them that I've made so far). I'm not really familiar with what has been called "namespace polution" but I assume it refers to an abundance of crap all in the namespace that can lead to more memory usage, longer access times, more processor usage, etc.

      "Namespace pollution" is where you end up having more symbols in your symbol table than the ones that you put there yourself. It can make things a bit confusing and the chances of having two symbols with the same name is increased. For example, if you have both use CGI ':standard' and use LWP::Simple in your program (which is a pretty common thing to want to do) then both of these modules try to export a subroutine called "head" into your symbol table. This will potentially lead to the universe exploding.

      --
      <http://www.dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

Re^2: OOP in CGI
by Your Mother (Archbishop) on Jul 29, 2005 at 23:44 UTC

    I like davorg's explanation. I also tend to do use CGI qw(param header); and such often, or even fully qualify it all, use CGI (); print CGI::header(...). I've written hundreds of CGIs and I've never once started a CGI with the object interface (though I've coded with it in other hackers' scripts). It's extra typing and unless you actually need to pass an object around, like to a templating system or something (I've never had to because Template::Toolkit has a built-in interface to CGI.pm), it's always struck me as a little pointless and really kind of weird.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://479300]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-03-29 10:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found