The Class and Instance Methods : Methods « Class « Perl

Home
Perl
1.Array
2.CGI
3.Class
4.Data Type
5.Database
6.File
7.GUI
8.Hash
9.Language Basics
10.Network
11.Regular Expression
12.Report
13.Statement
14.String
15.Subroutine
16.System Functions
17.Win32
18.XML
Perl » Class » Methods 
The Class and Instance Methods
 

# Module: House.pm
#!/usr/bin/perl
package House;
sub new{          
     my $class = shift;
     my $ref={};   
     bless($ref);
     return $ref;
}
sub set_owner{    
     my $self = shift;
     print "\$self is a class ", ref($self)," reference.\n";
     $self->{"Owner"= shift;
}
sub display_owner {
     my $self = shift;
     print $self->{"Owner"},"\n";
}
1;




# main.pl
#!/usr/bin/perl
use House;
my $house = House->new;      # Call class method
$house->set_owner ("Tom");   
$house->display_owner;       # Call instance method
                      

   
  
Related examples in the same category
1.Passing Parameters to Instance Methods
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.