Welcome to Linux Knowledge Base and Tutorial
"The place where you learn linux"
The ONE Campaign to make poverty history

 Create an AccountHome | Submit News | Your Account  

Tutorial Menu
Linux Tutorial Home
Table of Contents

· Introduction to Operating Systems
· Linux Basics
· Working with the System
· Shells and Utilities
· Editing Files
· Basic Administration
· The Operating System
· The X Windowing System
· The Computer Itself
· Networking
· System Monitoring
· Solving Problems
· Security
· Installing and Upgrading
· Linux and Windows

Glossary
MoreInfo
Man Pages
Linux Topics
Test Your Knowledge

Site Menu
Site Map
FAQ
Copyright Info
Terms of Use
Privacy Info
Disclaimer
WorkBoard
Thanks
Donations
Advertising
Masthead / Impressum
Your Account

Communication
Feedback
Forums
Private Messages
Surveys

Features
HOWTOs
News Archive
Submit News
Topics
User Articles
Web Links

Google
Google


The Web
linux-tutorial.info

Who's Online
There are currently, 168 guest(s) and 0 member(s) that are online.

You are an Anonymous user. You can register for free by clicking here

  

perlbot



DESCRIPTION

       The following collection of tricks and hints is intended
       to whet curious appetites about such things as the use of
       instance variables and the mechanics of object and class
       relationships.  The reader is encouraged to consult rele­
       vant textbooks for discussion of Object Oriented defini­
       tions and methodology.  This is not intended as a tutorial
       for object-oriented programming or as a comprehensive
       guide to Perl's object oriented features, nor should it be
       construed as a style guide.  If you're looking for tutori­
       als, be sure to read perlboot, perltoot, and perltooc.

       The Perl motto still holds:  There's more than one way to
       do it.


OO SCALING TIPS

       1    Do not attempt to verify the type of $self.  That'll
            break if the class is inherited, when the type of
            $self is valid but its package isn't what you expect.
            See rule 5.

       2    If an object-oriented (OO) or indirect-object (IO)
            syntax was used, then the object is probably the cor­
            rect type and there's no need to become paranoid
            about it.  Perl isn't a paranoid language anyway.  If
            people subvert the OO or IO syntax then they probably
            know what they're doing and you should let them do
            it.  See rule 1.

       3    Use the two-argument form of bless().  Let a subclass
            use your constructor.  See "INHERITING A CONSTRUC­
            TOR".

       4    The subclass is allowed to know things about its
            immediate superclass, the superclass is allowed to
            know nothing about a subclass.

       5    Don't be trigger happy with inheritance.  A "using",
            "containing", or "delegation" relationship (some sort
            of aggregation, at least) is often more appropriate.
            See "OBJECT RELATIONSHIPS", "USING RELATIONSHIP WITH
            SDBM", and "DELEGATION".

       6    The object is the namespace.  Make package globals
            accessible via the object.  This will remove the
            guess work about the symbol's home package.  See
            "CLASS CONTEXT AND THE OBJECT".

       7    IO syntax is certainly less noisy, but it is also
            prone to ambiguities that can cause difficult-to-find
            bugs.  Allow people to use the sure-thing OO syntax,
            even if you don't like it.

       strated.

               package Foo;

               sub new {
                       my $type = shift;
                       my %params = @_;
                       my $self = {};
                       $self->{'High'} = $params{'High'};
                       $self->{'Low'}  = $params{'Low'};
                       bless $self, $type;
               }

               package Bar;

               sub new {
                       my $type = shift;
                       my %params = @_;
                       my $self = [];
                       $self->[0] = $params{'Left'};
                       $self->[1] = $params{'Right'};
                       bless $self, $type;
               }

               package main;

               $a = Foo->new( 'High' => 42, 'Low' => 11 );
               print "High=$a->{'High'}\n";
               print "Low=$a->{'Low'}\n";

               $b = Bar->new( 'Left' => 78, 'Right' => 40 );
               print "Left=$b->[0]\n";
               print "Right=$b->[1]\n";


SCALAR INSTANCE VARIABLES

       An anonymous scalar can be used when only one instance
       variable is needed.

               package Foo;

               sub new {
                       my $type = shift;
                       my $self;
                       $self = shift;
                       bless \$self, $type;
               }

               package main;

               $a = Foo->new( 42 );
               print "a=$$a\n";


               package Foo;
               @ISA = qw( Bar );

               sub new {
                       my $type = shift;
                       my $self = Bar->new;
                       $self->{'biz'} = 11;
                       bless $self, $type;
               }

               package main;

               $a = Foo->new;
               print "buz = ", $a->{'buz'}, "\n";
               print "biz = ", $a->{'biz'}, "\n";


OBJECT RELATIONSHIPS

       The following demonstrates how one might implement "con­
       taining" and "using" relationships between objects.

               package Bar;

               sub new {
                       my $type = shift;
                       my $self = {};
                       $self->{'buz'} = 42;
                       bless $self, $type;
               }

               package Foo;

               sub new {
                       my $type = shift;
                       my $self = {};
                       $self->{'Bar'} = Bar->new;
                       $self->{'biz'} = 11;
                       bless $self, $type;
               }

               package main;

               $a = Foo->new;
               print "buz = ", $a->{'Bar'}->{'buz'}, "\n";
               print "biz = ", $a->{'biz'}, "\n";


OVERRIDING SUPERCLASS METHODS

       The following example demonstrates how to override a
       superclass method and then call the overridden method.
       The SUPER pseudo-class allows the programmer to call an
       overridden superclass method without actually knowing
       where that method is defined.
                       my $type = shift;
                       bless [], $type;
               }
               sub grr { print "grumble\n" }
               sub goo {
                       my $self = shift;
                       $self->SUPER::goo();
               }
               sub mumble {
                       my $self = shift;
                       $self->SUPER::mumble();
               }
               sub google {
                       my $self = shift;
                       $self->SUPER::google();
               }

               package main;

               $foo = Foo->new;
               $foo->mumble;
               $foo->grr;
               $foo->goo;
               $foo->google;


USING RELATIONSHIP WITH SDBM

       This example demonstrates an interface for the SDBM class.
       This creates a "using" relationship between the SDBM class
       and the new class Mydbm.

               package Mydbm;

               require SDBM_File;
               require Tie::Hash;
               @ISA = qw( Tie::Hash );

               sub TIEHASH {
                   my $type = shift;
                   my $ref  = SDBM_File->new(@_);
                   bless {'dbm' => $ref}, $type;
               }
               sub FETCH {
                   my $self = shift;
                   my $ref  = $self->{'dbm'};
                   $ref->FETCH(@_);
               }
               sub STORE {
                   my $self = shift;
                   if (defined $_[0]){
                       my $ref = $self->{'dbm'};
                       $ref->STORE(@_);
                   } else {


THINKING OF CODE REUSE

       One strength of Object-Oriented languages is the ease with
       which old code can use new code.  The following examples
       will demonstrate first how one can hinder code reuse and
       then how one can promote code reuse.

       This first example illustrates a class which uses a fully-
       qualified method call to access the "private" method
       BAZ().  The second example will show that it is impossible
       to override the BAZ() method.

               package FOO;

               sub new {
                       my $type = shift;
                       bless {}, $type;
               }
               sub bar {
                       my $self = shift;
                       $self->FOO::private::BAZ;
               }

               package FOO::private;

               sub BAZ {
                       print "in BAZ\n";
               }

               package main;

               $a = FOO->new;
               $a->bar;

       Now we try to override the BAZ() method.  We would like
       FOO::bar() to call GOOP::BAZ(), but this cannot happen
       because FOO::bar() explicitly calls FOO::private::BAZ().

               package FOO;

               sub new {
                       my $type = shift;
                       bless {}, $type;
               }
               sub bar {
                       my $self = shift;
                       $self->FOO::private::BAZ;
               }

               package FOO::private;

               sub BAZ {
               sub BAZ {
                       print "in GOOP::BAZ\n";
               }

               package main;

               $a = GOOP->new;
               $a->bar;

       To create reusable code we must modify class FOO, flatten­
       ing class FOO::private.  The next example shows a reusable
       class FOO which allows the method GOOP::BAZ() to be used
       in place of FOO::BAZ().

               package FOO;

               sub new {
                       my $type = shift;
                       bless {}, $type;
               }
               sub bar {
                       my $self = shift;
                       $self->BAZ;
               }

               sub BAZ {
                       print "in BAZ\n";
               }

               package GOOP;
               @ISA = qw( FOO );

               sub new {
                       my $type = shift;
                       bless {}, $type;
               }
               sub BAZ {
                       print "in GOOP::BAZ\n";
               }

               package main;

               $a = GOOP->new;
               $a->bar;


CLASS CONTEXT AND THE OBJECT

       Use the object to solve package and class context prob­
       lems.  Everything a method needs should be available via
       the object or should be passed as a parameter to the
       method.

       A class will sometimes have static or global data to be
               package Bar;

               %fizzle = ( 'Password' => 'XYZZY' );

               sub new {
                       my $type = shift;
                       my $self = {};
                       $self->{'fizzle'} = \%fizzle;
                       bless $self, $type;
               }

               sub enter {
                       my $self = shift;

                       # Don't try to guess if we should use %Bar::fizzle
                       # or %Foo::fizzle.  The object already knows which
                       # we should use, so just ask it.
                       #
                       my $fizzle = $self->{'fizzle'};

                       print "The word is ", $fizzle->{'Password'}, "\n";
               }

               package Foo;
               @ISA = qw( Bar );

               %fizzle = ( 'Password' => 'Rumple' );

               sub new {
                       my $type = shift;
                       my $self = Bar->new;
                       $self->{'fizzle'} = \%fizzle;
                       bless $self, $type;
               }

               package main;

               $a = Bar->new;
               $b = Foo->new;
               $a->enter;
               $b->enter;


INHERITING A CONSTRUCTOR

       An inheritable constructor should use the second form of
       bless() which allows blessing directly into a specified
       class.  Notice in this example that the object will be a
       BAR not a FOO, even though the constructor is in class
       FOO.

               package FOO;

               sub new {
               sub baz {
                       print "in BAR::baz()\n";
               }

               package main;

               $a = BAR->new;
               $a->baz;


DELEGATION

       Some classes, such as SDBM_File, cannot be effectively
       subclassed because they create foreign objects.  Such a
       class can be extended with some sort of aggregation tech­
       nique such as the "using" relationship mentioned earlier
       or by delegation.

       The following example demonstrates delegation using an
       AUTOLOAD() function to perform message-forwarding.  This
       will allow the Mydbm object to behave exactly like an
       SDBM_File object.  The Mydbm class could now extend the
       behavior by adding custom FETCH() and STORE() methods, if
       this is desired.

               package Mydbm;

               require SDBM_File;
               require Tie::Hash;
               @ISA = qw(Tie::Hash);

               sub TIEHASH {
                       my $type = shift;
                       my $ref = SDBM_File->new(@_);
                       bless {'delegate' => $ref};
               }

               sub AUTOLOAD {
                       my $self = shift;

                       # The Perl interpreter places the name of the
                       # message in a variable called $AUTOLOAD.

                       # DESTROY messages should never be propagated.
                       return if $AUTOLOAD =~ /::DESTROY$/;

                       # Remove the package name.
                       $AUTOLOAD =~ s/^Mydbm:://;

                       # Pass the message to the delegate.
                       $self->{'delegate'}->$AUTOLOAD(@_);
               }

               package main;

An undefined database error occurred. SELECT distinct pages.pagepath,pages.pageid FROM pages, page2command WHERE pages.pageid = page2command.pageid AND commandid =


  




Login
Nickname

Password

Security Code
Security Code
Type Security Code


Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name.

Help if you can!


Amazon Wish List

Did You Know?
The Linux Tutorial can use your help.


Friends



Tell a Friend About Us

Bookmark and Share



Web site powered by PHP-Nuke

Is this information useful? At the very least you can help by spreading the word to your favorite newsgroups, mailing lists and forums.
All logos and trademarks in this site are property of their respective owner. The comments are property of their posters. Articles are the property of their respective owners. Unless otherwise stated in the body of the article, article content (C) 1994-2013 by James Mohr. All rights reserved. The stylized page/paper, as well as the terms "The Linux Tutorial", "The Linux Server Tutorial", "The Linux Knowledge Base and Tutorial" and "The place where you learn Linux" are service marks of James Mohr. All rights reserved.
The Linux Knowledge Base and Tutorial may contain links to sites on the Internet, which are owned and operated by third parties. The Linux Tutorial is not responsible for the content of any such third-party site. By viewing/utilizing this web site, you have agreed to our disclaimer, terms of use and privacy policy. Use of automated download software ("harvesters") such as wget, httrack, etc. causes the site to quickly exceed its bandwidth limitation and are therefore expressly prohibited. For more details on this, take a look here

PHP-Nuke Copyright © 2004 by Francisco Burzi. This is free software, and you may redistribute it under the GPL. PHP-Nuke comes with absolutely no warranty, for details, see the license.
Page Generation: 0.06 Seconds