13. Classes, Objects, and Ties
<script type="text/javascript">
</script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
Introduction
#----------------------------- |
Constructing an Object
#----------------------------- |
Destroying an Object
#----------------------------- |
Managing Instance Data
#----------------------------- |
Managing Class Data
#----------------------------- |
Using Classes as Structs
#----------------------------- |
Cloning Objects
#----------------------------- |
Calling Methods Indirectly
#----------------------------- |
Determining Subclass Membership
#----------------------------- |
Writing an Inheritable Class
#----------------------------- |
Accessing Overridden Methods
#----------------------------- |
Generating Attribute Methods Using AUTOLOAD
#----------------------------- |
Solving the Data Inheritance Problem
#----------------------------- |
Coping with Circular Data Structures
#----------------------------- |
Overloading Operators
#----------------------------- |
Creating Magic Variables with tie
#----------------------------- |
age => '
Cloning Objects
___FCKpd___6 |
Calling Methods Indirectly
___FCKpd___7 |
Determining Subclass Membership
___FCKpd___8 |
Writing an Inheritable Class
___FCKpd___9 |
Accessing Overridden Methods
___FCKpd___10 |
Generating Attribute Methods Using AUTOLOAD
___FCKpd___11 |
Solving the Data Inheritance Problem
___FCKpd___12 |
Coping with Circular Data Structures
___FCKpd___13 |
Overloading Operators
___FCKpd___14 |
Creating Magic Variables with tie
___FCKpd___15 |
peers => '@', # but peers field is an array (reference)
};
my $p = Person->
new ()
; # allocate an empty Person struct
$p->name ( "Jason Smythe" ); # set its name field
$p->age ( 13 ); # set its age field
$p->peers ( [ "Wilbur", "Ralph", "Fred" ] ); # set its peers field
# or this way:
@ {$p->peers } = ( "Wilbur", "Ralph", "Fred" );
# fetch various values, including the zeroth friend
printf "At age %d , %s 's first friend is %s ./n",
$p->age, $p->name, $p->peers ( 0 );
#-----------------------------
use Class::Struct ;
struct Person => { name => '
Cloning Objects
___FCKpd___6 |
Calling Methods Indirectly
___FCKpd___7 |
Determining Subclass Membership
___FCKpd___8 |
Writing an Inheritable Class
___FCKpd___9 |
Accessing Overridden Methods
___FCKpd___10 |
Generating Attribute Methods Using AUTOLOAD
___FCKpd___11 |
Solving the Data Inheritance Problem
___FCKpd___12 |
Coping with Circular Data Structures
___FCKpd___13 |
Overloading Operators
___FCKpd___14 |
Creating Magic Variables with tie
___FCKpd___15 |
Cloning Objects
___FCKpd___6 |
Calling Methods Indirectly
___FCKpd___7 |
Determining Subclass Membership
___FCKpd___8 |
Writing an Inheritable Class
___FCKpd___9 |
Accessing Overridden Methods
___FCKpd___10 |
Generating Attribute Methods Using AUTOLOAD
___FCKpd___11 |
Solving the Data Inheritance Problem
___FCKpd___12 |
Coping with Circular Data Structures
___FCKpd___13 |
Overloading Operators
___FCKpd___14 |
Creating Magic Variables with tie
___FCKpd___15 |
struct Family => { head => 'Person', address => '
Cloning Objects
___FCKpd___6 |
Calling Methods Indirectly
___FCKpd___7 |
Determining Subclass Membership
___FCKpd___8 |
Writing an Inheritable Class
___FCKpd___9 |
Accessing Overridden Methods
___FCKpd___10 |
Generating Attribute Methods Using AUTOLOAD
___FCKpd___11 |
Solving the Data Inheritance Problem
___FCKpd___12 |
Coping with Circular Data Structures
___FCKpd___13 |
Overloading Operators
___FCKpd___14 |
Creating Magic Variables with tie
___FCKpd___15 |
$folks = Family->
new ();
$dad = $folks->head ;
$dad->name ( "John" );
$dad->age ( 34 );
printf ( " %s 's age is %d /n", $folks->head->name, $folks->head->age );
#-----------------------------
sub Person::age {
use Carp ;
my ( $self, $age ) = @_ ;
if ( @_ > 2 ) { confess "too many arguments" }
elsif ( @_ == 1 ) { return $struct-> { 'age' } }
elsif ( @_ == 2 ) {
carp "age `$age' isn't numeric" if $age !~ /^/d+/ ;
carp "age `$age' is unreasonable" if $age > 150 ;
$self-> { 'age' } = $age ;
}
}
#-----------------------------
if ($^W ) {
carp "age `$age' isn't numeric" if $age !~ /^/d+/ ;
carp "age `$age' is unreasonable" if $age > 150 ;
}
#-----------------------------
my $gripe = $^W ? /&carp : /&croak ;
$gripe-> ( "age `$age' isn't numeric" ) if $age !~ /^/d+/ ;
$gripe-> ( "age `$age' is unreasonable" ) if $age > 150 ;
#-----------------------------
struct Family => [ head => 'Person', address => '
Cloning Objects
___FCKpd___6 |
Calling Methods Indirectly
___FCKpd___7 |
Determining Subclass Membership
___FCKpd___8 |
Writing an Inheritable Class
___FCKpd___9 |
Accessing Overridden Methods
___FCKpd___10 |
Generating Attribute Methods Using AUTOLOAD
___FCKpd___11 |
Solving the Data Inheritance Problem
___FCKpd___12 |
Coping with Circular Data Structures
___FCKpd___13 |
Overloading Operators
___FCKpd___14 |
Creating Magic Variables with tie
___FCKpd___15 |
#-----------------------------
struct Card => {
name => '
Cloning Objects
___FCKpd___6 |
Calling Methods Indirectly
___FCKpd___7 |
Determining Subclass Membership
___FCKpd___8 |
Writing an Inheritable Class
___FCKpd___9 |
Accessing Overridden Methods
___FCKpd___10 |
Generating Attribute Methods Using AUTOLOAD
___FCKpd___11 |
Solving the Data Inheritance Problem
___FCKpd___12 |
Coping with Circular Data Structures
___FCKpd___13 |
Overloading Operators
___FCKpd___14 |
Creating Magic Variables with tie
___FCKpd___15 |
color => '
Cloning Objects
___FCKpd___6 |
Calling Methods Indirectly
___FCKpd___7 |
Determining Subclass Membership
___FCKpd___8 |
Writing an Inheritable Class
___FCKpd___9 |
Accessing Overridden Methods
___FCKpd___10 |
Generating Attribute Methods Using AUTOLOAD
___FCKpd___11 |
Solving the Data Inheritance Problem
___FCKpd___12 |
Coping with Circular Data Structures
___FCKpd___13 |
Overloading Operators
___FCKpd___14 |
Creating Magic Variables with tie
___FCKpd___15 |
cost => '
Cloning Objects
___FCKpd___6 |
Calling Methods Indirectly
___FCKpd___7 |
Determining Subclass Membership
___FCKpd___8 |
Writing an Inheritable Class
___FCKpd___9 |
Accessing Overridden Methods
___FCKpd___10 |
Generating Attribute Methods Using AUTOLOAD
___FCKpd___11 |
Solving the Data Inheritance Problem
___FCKpd___12 |
Coping with Circular Data Structures
___FCKpd___13 |
Overloading Operators
___FCKpd___14 |
Creating Magic Variables with tie
___FCKpd___15 |
type => '
Cloning Objects
___FCKpd___6 |
Calling Methods Indirectly
___FCKpd___7 |
Determining Subclass Membership
___FCKpd___8 |
Writing an Inheritable Class
___FCKpd___9 |
Accessing Overridden Methods
___FCKpd___10 |
Generating Attribute Methods Using AUTOLOAD
___FCKpd___11 |
Solving the Data Inheritance Problem
___FCKpd___12 |
Coping with Circular Data Structures
___FCKpd___13 |
Overloading Operators
___FCKpd___14 |
Creating Magic Variables with tie
___FCKpd___15 |
release => '
Cloning Objects
___FCKpd___6 |
Calling Methods Indirectly
___FCKpd___7 |
Determining Subclass Membership
___FCKpd___8 |
Writing an Inheritable Class
___FCKpd___9 |
Accessing Overridden Methods
___FCKpd___10 |
Generating Attribute Methods Using AUTOLOAD
___FCKpd___11 |
Solving the Data Inheritance Problem
___FCKpd___12 |
Coping with Circular Data Structures
___FCKpd___13 |
Overloading Operators
___FCKpd___14 |
Creating Magic Variables with tie
___FCKpd___15 |
text => '
Cloning Objects
___FCKpd___6 |
Calling Methods Indirectly
___FCKpd___7 |
Determining Subclass Membership
___FCKpd___8 |
Writing an Inheritable Class
___FCKpd___9 |
Accessing Overridden Methods
___FCKpd___10 |
Generating Attribute Methods Using AUTOLOAD
___FCKpd___11 |
Solving the Data Inheritance Problem
___FCKpd___12 |
Coping with Circular Data Structures
___FCKpd___13 |
Overloading Operators
___FCKpd___14 |
Creating Magic Variables with tie
___FCKpd___15 |
};
#-----------------------------
struct Card => map { $_ => '
Cloning Objects
___FCKpd___6 |
Calling Methods Indirectly
___FCKpd___7 |
Determining Subclass Membership
___FCKpd___8 |
Writing an Inheritable Class
___FCKpd___9 |
Accessing Overridden Methods
___FCKpd___10 |
Generating Attribute Methods Using AUTOLOAD
___FCKpd___11 |
Solving the Data Inheritance Problem
___FCKpd___12 |
Coping with Circular Data Structures
___FCKpd___13 |
Overloading Operators
___FCKpd___14 |
Creating Magic Variables with tie
___FCKpd___15 |
#-----------------------------
struct hostent => { reverse qw {
$ name
@ aliases
$ addrtype
$ length
@ addr_list
}};
#-----------------------------
#define h_type h_addrtype
#define h_addr h_addr_list[0]
#-----------------------------
# make (hostent object)->
type ()
same as (hostent object )->
addrtype ()
*hostent::type = /&hostent::addrtype ;
# make (hostenv object)->
addr ()
same as (hostenv object )->addr_list ( 0 )
sub hostent::addr { shift->addr_list ( 0, @_ ) }
#-----------------------------
package Extra::hostent ;
use Net::hostent ;
@ISA = qw (hostent );
sub addr { shift->addr_list ( 0, @_ ) }
1 ;
#-----------------------------
Cloning Objects
___FCKpd___6 |
Calling Methods Indirectly
___FCKpd___7 |
Determining Subclass Membership
___FCKpd___8 |
Writing an Inheritable Class
___FCKpd___9 |
Accessing Overridden Methods
___FCKpd___10 |
Generating Attribute Methods Using AUTOLOAD
___FCKpd___11 |
Solving the Data Inheritance Problem
___FCKpd___12 |
Coping with Circular Data Structures
___FCKpd___13 |
Overloading Operators
___FCKpd___14 |
Creating Magic Variables with tie
___FCKpd___15 |