What is the difference between the function oriented and Object oriented approach in the modules (CPAN). Mention the merits as well demerits of these two types.

Showing Answers 1 - 3 of 3 Answers

shalusanju

  • Aug 13th, 2010
 

In the object-oriented style you create one or more CGI objects and then use object methods to create the various elements of the page. Each CGI object starts out with the list of named parameters that were passed to your CGI script by the server. You can modify the objects, save them to a file or database and recreate them. Because each object corresponds to the "state" of the CGI script, and because each object's parameter list is independent of the others, this allows you to save the state of the script and restore it later.

eg.
  1. #!/usr/local/bin/perl -w
  2.    use CGI;                       # load CGI routines
  3.    $q = CGI->new;                 # create new CGI object
  4.    print $q->header,              # create the HTTP header
  5.    $q->start_html('hello world'), # start the HTML         
  6.    $q->h1('hello world'),         # level 1 header         
  7.    $q->end_html;                  # end the HTML
In the function-oriented style, there is one default CGI object that you rarely deal with directly. Instead you just call functions to retrieve CGI parameters, create HTML tags, manage cookies, and so on. This provides you with a cleaner programming interface, but limits you to using one CGI object at a time. The following example prints the same page, but uses the function-oriented interface. The main differences are that we now need to import a set of functions into our name space (usually the "standard" functions), and we don't need to create the CGI object.
eg.
  1. #!/usr/local/bin/perl
  2. use CGI qw/:standard/; # load standard CGI routines
  3. print header, # create the HTTP header
  4. start_html('hello world'), # start the HTML
  5. h1('hello world'), # level 1 header
  6. end_html; # end the HTML

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions