Greenhalgh David <[EMAIL PROTECTED]> wrote:
: 
: Bare in mind that I am still a beginner at coding. Why
: is it "good practice" to create an object when using
: CGI, rather than just diving in?
: 
: For example:
: 
: use CGI ':standard';
: my $q=new CGI;
: my $input=$q->param('input');
: 
: and
: 
: use CGI ':standard';
: my $input=param('input');
: 
: both put the contents of 'input' into $input and the
: last one has less lines and less opportunity for typos,
: but the first is better practice than the second.
: Gently, please; why?

    Your first example is poorly written. It is better
written;

use CGI;
my $q = new CGI;


    Understanding this will help you understand why
objects tend to present better programming practice.
I'll bet you have never looked under the hood of your
programs. Let's pop the bonnet and take a peek. There
is a special variable under the hood called "%main::"
or just "%::". It is also known as the symbol table.
Let's take a look at it:

#!/usr/bin/perl

#use strict;
#use warnings;
#use CGI qw(:standard);

printf "% 22s => % s\n", $_, $::{ $_ } foreach keys %::;

    On my system this produces about 33 entries. Let's
add strict and warnings:

#!/usr/bin/perl

use strict;
use warnings;
#use CGI qw(:standard);

printf "% 22s => % s\n", $_, $::{ $_ } foreach keys %::;


    Now I am up to 40 entries in %::. Let's see
what happens when we add CGI.pm:

#!/usr/bin/perl

use strict;
use warnings;
use CGI qw(:standard);

printf "% 22s => % s\n", $_, $::{ $_ } foreach keys %::;


    I now show 194 entries in the symbol table. Now
let's look at the object-oriented method of using
CGI.pm:

#!/usr/bin/perl

use strict;
use warnings;
use CGI;

printf "% 22s => % s\n", $_, $::{ $_ } foreach keys %::;

    Now I only have 55 entries. What are those
other 139 entries from ':standard'? They're mostly
subroutines that have been added to our script. So
we see that objects tend to import less into main.
They are (usually) better behaved.


    Another important thing about objects is that
you can easily make them more robust. Let's say you
need to do provide a common object to a bunch of
scripts on a web site. They list the states
according to district. You also want to use the
same title, author, style sheet for each page.

    For better or worse you decide to create an
object based on CGI.pm. Here's the object. I
didn't take the time to comment this just cut
& paste it into a file named My_CGI.pm.

package My_CGI;

use base CGI;

sub start_html {
    my $self = shift;

    # return regular call if arguments were passed
    return
        $self->SUPER::start_html( @_ ) if @_;

    # return default call if no arguments were passed
    return
        $self->SUPER::start_html(

            -head => $self->Link( {
                    -rel   => 'stylesheet',
                    -href  => 'css/district.css',
                    -type  => 'text/css'}),
            -title   => 'District Form',
            -author  => '[EMAIL PROTECTED]',
        );
}

sub district_1_popup {
    my $self    = shift;
    my $default = shift || 'TX';

    # states in this district
    my %states = (
        AZ  => 'ARIZONA',
        AR  => 'ARKANSAS',
        OK  => 'OKLAHOMA',
        LA  => 'LOUISIANA',
        TX  => 'TEXAS',
    );

    # in case we pass in a state that is
    #    not in this district
    $default = 'TX' unless $states{ $default };

    $self->popup_menu( {
        name    => 'district_1_state',
        values  => [ keys %states ],
        default => $default,
        labels  => \%states }
    );
}

sub district_2_popup {
    my $self = shift;
    my $default = shift || 'FL';

    # states in this district
    my %states = (
        MS  => 'MISSISSIPPI',
        GA  => 'GEORGIA',
        FL  => 'FLORIDA',
    );

    # in case we pass in a state that is
    #    not in this district
    $default = 'FL' unless $states{ $default };

    $self->popup_menu( {
        name    => 'district_2_state',
        values  => [ keys %states ],
        default => $default,
        labels  => \%states }
    );

}

1;


    And our script might look like:

#!/usr/bin/perl

use strict;
use warnings;

use My_CGI;

my $q = My_CGI->new();

print
        $q->header(),
                $q->start_html(),
                $q->district_1_popup(),
                $q->end_html();

__END__


    When we call $q->start_html() our module
calls CGI.pm with default arguments. The new
method (district_1_popup) calls CGIs
popup_menu() method and returns the
pre-defined states.


HTH,

Charles K. Clarkson
-- 
Head Bottle Washer,
Clarkson Energy Homes, Inc.
Mobile Home Specialists
254 968-8328


































-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to