diff options
author | Casey West <casey@geeknest.com> | 2001-10-02 15:24:43 -0400 |
---|---|---|
committer | Jarkko Hietaniemi <jhi@iki.fi> | 2001-10-03 12:04:49 +0000 |
commit | 769c28980682c9f2c20f6c60950b1b28469d23fa (patch) | |
tree | d2046771ece0dc2b9be446bf02f88eb00da59a41 /pod/perlbot.pod | |
parent | e8012c20b486a1b6d3694359216843ea2b78d297 (diff) | |
download | perl-769c28980682c9f2c20f6c60950b1b28469d23fa.tar.gz |
(retracted by #12338)
Subject: [PATCH] Code clean up for perlboot.pod
Date: Tue, 2 Oct 2001 19:24:43 -0400
Message-ID: <20011002192443.B2163@stupid.geeknest.com>
Subject: [PATCH] Code clean up for perlbot.diff
From: Casey West <casey@geeknest.com>
Date: Tue, 2 Oct 2001 19:25:22 -0400
Message-ID: <20011002192522.C2163@stupid.geeknest.com>
Subject: [PATCH] Code clean up for perlcall.pod
From: Casey West <casey@geeknest.com>
Date: Tue, 2 Oct 2001 19:25:57 -0400
Message-ID: <20011002192557.D2163@stupid.geeknest.com>
Subject: [PATCH] Code clean up for perldata.pod
From: Casey West <casey@geeknest.com>
Date: Tue, 2 Oct 2001 19:26:29 -0400
Message-ID: <20011002192629.E2163@stupid.geeknest.com>
Subject: [PATCH] Code clean up for perldbmfilter.pod
From: Casey West <casey@geeknest.com>
Date: Tue, 2 Oct 2001 19:26:59 -0400
Message-ID: <20011002192659.F2163@stupid.geeknest.com>
Subject: [PATCH] Code clean up for perlebcdic.pod
From: Casey West <casey@geeknest.com>
Date: Tue, 2 Oct 2001 19:27:37 -0400
Message-ID: <20011002192737.G2163@stupid.geeknest.com>
p4raw-id: //depot/perl@12313
Diffstat (limited to 'pod/perlbot.pod')
-rw-r--r-- | pod/perlbot.pod | 173 |
1 files changed, 102 insertions, 71 deletions
diff --git a/pod/perlbot.pod b/pod/perlbot.pod index bc4e4da1f7..17b3755336 100644 --- a/pod/perlbot.pod +++ b/pod/perlbot.pod @@ -82,11 +82,13 @@ variables. Named parameters are also demonstrated. package Foo; sub new { - my $type = shift; + my $type = shift; my %params = @_; - my $self = {}; - $self->{'High'} = $params{'High'}; - $self->{'Low'} = $params{'Low'}; + my $self = {}; + + $self->{High} = $params{High}; + $self->{Low} = $params{Low}; + bless $self, $type; } @@ -94,23 +96,25 @@ variables. Named parameters are also demonstrated. package Bar; sub new { - my $type = shift; + my $type = shift; my %params = @_; - my $self = []; - $self->[0] = $params{'Left'}; - $self->[1] = $params{'Right'}; + 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"; + my $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"; + my $b = Bar->new( Left => 78, Right => 40 ); + print "Left = $b->[0]\n"; + print "Right = $b->[1]\n"; =head1 SCALAR INSTANCE VARIABLES @@ -120,15 +124,15 @@ An anonymous scalar can be used when only one instance variable is needed. sub new { my $type = shift; - my $self; - $self = shift; + my $self = shift; + bless \$self, $type; } package main; - $a = Foo->new( 42 ); - print "a=$$a\n"; + my $a = Foo->new( 42 ); + print "a = $$a\n"; =head1 INSTANCE VARIABLE INHERITANCE @@ -143,25 +147,29 @@ object. sub new { my $type = shift; my $self = {}; - $self->{'buz'} = 42; + + $self->{buz} = 42; + bless $self, $type; } package Foo; - @ISA = qw( Bar ); + our @ISA = qw( Bar ); sub new { my $type = shift; my $self = Bar->new; - $self->{'biz'} = 11; + + $self->{biz} = 11; + bless $self, $type; } package main; - $a = Foo->new; - print "buz = ", $a->{'buz'}, "\n"; - print "biz = ", $a->{'biz'}, "\n"; + my $a = Foo->new; + print "buz = $a->{buz}\n"; + print "biz = $a->{biz}\n"; @@ -175,7 +183,9 @@ relationships between objects. sub new { my $type = shift; my $self = {}; - $self->{'buz'} = 42; + + $self->{buz} = 42; + bless $self, $type; } @@ -184,16 +194,18 @@ relationships between objects. sub new { my $type = shift; my $self = {}; - $self->{'Bar'} = Bar->new; - $self->{'biz'} = 11; + + $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"; + my $a = Foo->new; + print "buz = $a->{Bar}->{buz}\n"; + print "biz = $a->{biz}\n"; @@ -207,14 +219,17 @@ where that method is defined. package Buz; sub goo { print "here's the goo\n" } - package Bar; @ISA = qw( Buz ); + + package Bar; + our @ISA = qw( Buz ); sub google { print "google here\n" } + package Baz; sub mumble { print "mumbling\n" } package Foo; - @ISA = qw( Bar Baz ); + our @ISA = qw( Bar Baz ); sub new { my $type = shift; @@ -236,7 +251,7 @@ where that method is defined. package main; - $foo = Foo->new; + my $foo = Foo->new; $foo->mumble; $foo->grr; $foo->goo; @@ -250,24 +265,28 @@ This example demonstrates an interface for the SDBM class. This creates a package Mydbm; - require SDBM_File; - require Tie::Hash; - @ISA = qw( Tie::Hash ); + use SDBM_File; + use Tie::Hash; + + our @ISA = qw( Tie::Hash ); sub TIEHASH { my $type = shift; my $ref = SDBM_File->new(@_); - bless {'dbm' => $ref}, $type; + bless { dbm => $ref }, $type; } + sub FETCH { my $self = shift; - my $ref = $self->{'dbm'}; + my $ref = $self->{dbm}; $ref->FETCH(@_); } + sub STORE { my $self = shift; - if (defined $_[0]){ - my $ref = $self->{'dbm'}; + + if ( defined $_[0] ) { + my $ref = $self->{dbm}; $ref->STORE(@_); } else { die "Cannot STORE an undefined key in Mydbm\n"; @@ -277,13 +296,13 @@ This example demonstrates an interface for the SDBM class. This creates a package main; use Fcntl qw( O_RDWR O_CREAT ); - tie %foo, "Mydbm", "Sdbm", O_RDWR|O_CREAT, 0640; - $foo{'bar'} = 123; - print "foo-bar = $foo{'bar'}\n"; + tie my %foo, 'Mydbm', 'Sdbm', O_RDWR|O_CREAT, 0640; + $foo{bar} = 123; + print "foo-bar = $foo{bar}\n"; - tie %bar, "Mydbm", "Sdbm2", O_RDWR|O_CREAT, 0640; - $bar{'Cathy'} = 456; - print "bar-Cathy = $bar{'Cathy'}\n"; + tie my %bar, 'Mydbm', 'Sdbm2', O_RDWR|O_CREAT, 0640; + $bar{Cathy} = 456; + print "bar-Cathy = $bar{Cathy}\n"; =head1 THINKING OF CODE REUSE @@ -301,6 +320,7 @@ that it is impossible to override the BAZ() method. my $type = shift; bless {}, $type; } + sub bar { my $self = shift; $self->FOO::private::BAZ; @@ -314,7 +334,7 @@ that it is impossible to override the BAZ() method. package main; - $a = FOO->new; + my $a = FOO->new; $a->bar; Now we try to override the BAZ() method. We would like FOO::bar() to call @@ -327,6 +347,7 @@ FOO::private::BAZ(). my $type = shift; bless {}, $type; } + sub bar { my $self = shift; $self->FOO::private::BAZ; @@ -339,7 +360,9 @@ FOO::private::BAZ(). } package GOOP; - @ISA = qw( FOO ); + + our @ISA = qw( FOO ); + sub new { my $type = shift; bless {}, $type; @@ -351,7 +374,7 @@ FOO::private::BAZ(). package main; - $a = GOOP->new; + my $a = GOOP->new; $a->bar; To create reusable code we must modify class FOO, flattening class @@ -364,6 +387,7 @@ method GOOP::BAZ() to be used in place of FOO::BAZ(). my $type = shift; bless {}, $type; } + sub bar { my $self = shift; $self->BAZ; @@ -374,19 +398,21 @@ method GOOP::BAZ() to be used in place of FOO::BAZ(). } package GOOP; - @ISA = qw( FOO ); + + our @ISA = qw( FOO ); sub new { my $type = shift; bless {}, $type; } + sub BAZ { print "in GOOP::BAZ\n"; } package main; - $a = GOOP->new; + my $a = GOOP->new; $a->bar; =head1 CLASS CONTEXT AND THE OBJECT @@ -409,12 +435,12 @@ method where that data is located. package Bar; - %fizzle = ( 'Password' => 'XYZZY' ); + my %fizzle = ( Password => 'XYZZY' ); sub new { my $type = shift; my $self = {}; - $self->{'fizzle'} = \%fizzle; + $self->{fizzle} = \%fizzle; bless $self, $type; } @@ -425,27 +451,29 @@ method where that data is located. # or %Foo::fizzle. The object already knows which # we should use, so just ask it. # - my $fizzle = $self->{'fizzle'}; + my $fizzle = $self->{fizzle}; - print "The word is ", $fizzle->{'Password'}, "\n"; + print "The word is $fizzle->{Password}\n"; } package Foo; - @ISA = qw( Bar ); - %fizzle = ( 'Password' => 'Rumple' ); + our @ISA = qw( Bar ); + + my %fizzle = ( Password => 'Rumple' ); sub new { my $type = shift; my $self = Bar->new; - $self->{'fizzle'} = \%fizzle; + $self->{fizzle} = \%fizzle; bless $self, $type; } package main; - $a = Bar->new; - $b = Foo->new; + my $a = Bar->new; + my $b = Foo->new; + $a->enter; $b->enter; @@ -468,7 +496,8 @@ object will be a BAR not a FOO, even though the constructor is in class FOO. } package BAR; - @ISA = qw(FOO); + + our @ISA = qw(FOO); sub baz { print "in BAR::baz()\n"; @@ -476,7 +505,7 @@ object will be a BAR not a FOO, even though the constructor is in class FOO. package main; - $a = BAR->new; + my $a = BAR->new; $a->baz; =head1 DELEGATION @@ -493,14 +522,16 @@ behavior by adding custom FETCH() and STORE() methods, if this is desired. package Mydbm; - require SDBM_File; - require Tie::Hash; - @ISA = qw(Tie::Hash); + use SDBM_File; + use Tie::Hash; + + our @ISA = qw( Tie::Hash ); + our $AUTOLOAD; sub TIEHASH { my $type = shift; - my $ref = SDBM_File->new(@_); - bless {'delegate' => $ref}; + my $ref = SDBM_File->new(@_); + bless { delegate => $ref }; } sub AUTOLOAD { @@ -516,12 +547,12 @@ behavior by adding custom FETCH() and STORE() methods, if this is desired. $AUTOLOAD =~ s/^Mydbm:://; # Pass the message to the delegate. - $self->{'delegate'}->$AUTOLOAD(@_); + $self->{delegate}->$AUTOLOAD(@_); } package main; use Fcntl qw( O_RDWR O_CREAT ); - tie %foo, "Mydbm", "adbm", O_RDWR|O_CREAT, 0640; - $foo{'bar'} = 123; - print "foo-bar = $foo{'bar'}\n"; + tie my %foo, 'Mydbm', 'adbm', O_RDWR|O_CREAT, 0640; + $foo{bar} = 123; + print "foo-bar = $foo{bar}\n"; |