Chapter 2
Listing 2 1. Overloading Unary Operations in the Even Class
package Even;
use overload
    ++  => "incr",
        => "decr",
    +0  => "numify",
sub new {
   my ($pkg, $num) = @_;
   croak("Even requires an even number to start with!") if $num % 2;
   return bless(\$num, $pkg);   
}
sub incr {
   my $self = shift;
   $$self += 2;
   return $self;
}
sub decr {
   my $self = shift;
   $$self  = 2;
   return $self;
}
sub numify {
   my $self = shift;
   return $$self;
}
1;
This module also serves as a demonstration of an idea briefly discussed 
earlier: Objects need not be based on hashes but can be based on any reference. In 
this case objects in the Even class are implemented using a scalar as the under 
lying type. When new() creates a object, it simply blesses a reference to a scalar:
return bless(\$num, $pkg);
Then when object methods need access to the underlying scalar, they simply use a 
scalar dereference:
$$self  = 2;
52






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

web hosting perl

 

Our partners: PHP: Hypertext Preprocessor Best Web Hosting Java Web Hosting Inexpensive Web Hosting  Jsp Web Hosting

Cheapest Web Hosting Jsp Hosting Cheap Hosting

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved