Download
objects and modules n.
Skip this Video
Loading SlideShow in 5 Seconds..
Objects and Modules PowerPoint Presentation
Download Presentation
Objects and Modules

Objects and Modules

0 Views Download Presentation
Download Presentation

Objects and Modules

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Objects and Modules

  2. Introduction • Object Oriented Programming • Everything in life is an object and can be described as one • Classes • Encapsulation of data and functions • Information hiding • One class does not know how another class works • Classes can still communicate with one another • Interfaces • Data is called a class’s attributes • Functionality is called a class’s methods

  3. Using a Class • How to use a class • Make an instance of that class • Create an object of that class type • All objects are references • The new constructor • Use the arrow operator to call the classes methods • objectName -> methodName( arguments )

  4. 1 #!/usr/bin/perl 2 # Fig. 14.1: fig14_01.pl 3 # Using the FileHandle module 4 5 use warnings; 6 use strict; 7 use FileHandle; Two new FileHandle objects are created using the new constructor 8 9my $write = new FileHandle; 10 my $read = new FileHandle; 11 12$write->open( ">filehandle.txt" ) or The files are opened using the FileHandle’s open method 13 die( "Could not open write" ); 14 $read->open( "input.txt" ) or 15 die( "Could not open read" ); 16 17 $write->autoflush( 1 ); 18 Using the print method of FileHandle 19 my $i = 1; 20 21 while ( my $line = $read->getline() ) { 22 $write->print( $i++, " $line" ); fig14_01.pl

  5. This is a sample file. With some lines for demonstration purposes. Fig. 14.2 Contents of input.txt. 1 This is a sample file. 2 With some lines for 3 demonstration purposes. Fig. 14.3 Contents of filehandle.txt (after the program has executed). Using a Class

  6. Creating a Simple Class • Implementation • Details of a class • How it is organized • What data it uses • What algorithms it uses • Can change as long as they still do the same basic things • If so then client code need not be changed • The bless function • Get and Set methods • Used to retrieve or store data into attributes • Should have means of validating the data

  7. 1 #!/usr/bin/perl 2 # Fig. 14.4: Date.pm 3 # A simple Date class 4 5 package Date; 6 7 use strict; 8 use warnings; 9 10 sub new 11 { 12 my $date = { the_year => 1000, The bless function receives a reference and turns it into an object 13 the_month => 1, 14 the_day => 1, }; 15 16 bless( $date ); 17return $date; The new method then returns the Date object 18 } 19 20 sub year 21 { 22 my $self = shift(); 23 24 $self->{ the_year } = shift() if ( @_ ); The year function can be used to either set the value of the_year or else return the value to the user 25 26 return $self->{ the_year }; 27 } 28 Date.pm

  8. 29 sub month 30 { 31 my $self = shift(); 32 33 $self->{ the_month } = shift() if ( @_ ); 34 35 return $self->{ the_month }; 36 } 37 38 sub day 39 { 40 my $self = shift(); 41 42 $self->{ the_day } = shift() if ( @_ ); 43 44 return $self->{ the_day }; 45 } 46 47 sub setDate 48 { 49 if ( @_ == 4 ) { 50 my $self = shift(); The setDate method is used to set all there values; day, month and year 51 $self->month( $_[ 0 ] ); 52 $self->day( $_[ 1 ] ); 53 $self->year( $_[ 2 ] ); The month function can be used to either set the value of the_month or else return the value to the user The day function can be used to either set the value of the_day or else return the value to the user 54 } 55 else { 56 print( "Method setDate requires three arguments.\n" ); 57 } 58 } 59 Date.pm

  9. 60sub print The print function is used to display the month, day and year in the proper format 61 { 62 my $self = shift(); 63 print( $self->month ); 64 print( "/" ); 65 print( $self->day ); 66 print( "/" ); 67 print( $self->year ); 68 } 69 Date.pm

  10. 1 #!/usr/bin/perl 2 # Fig. 14.5: fig14_05.pl 3 # Using class Date. The new function is called to create a new Date object 4 5 use Date; Set the date by sending three arguments Uses the arrow operator to get the value of the_month 6 use strict; Call the print method to display the entire date 7 use warnings; 8 9my $today = new Date; 10 11$today->setDate( 7, 14, 2000 ); 12print( $today->month() ); 13 print( "\n" ); 14$today->print(); 15 print( "\n" ); fig14_05.pl Program Output 7 7/14/2000

  11. Inheritance • Inheritance • Deriving a class • Classes can be made based off of pre-existing classes • They can use attributes and behaviors of the old class • Software reuse • The @ISA array • Composition • Objects of old classes can be data members

  12. 1 #!/usr/bin/perl 2 # Fig. 14.6: Employee.pm 3 # Implementation of class Employee. 4 5 package Employee; The new function has to be made differently in order to account for the fact that a user may not want to make a employee object every time. 6 7 use strict; 8 use warnings; The bless function makes a new object of the right class and returns it to the user 9 use Date; 10 11sub new 12 { 13 my $type = shift(); 14 my $class = ref( $type ) || $type; 15 16 my $hireDay = new Date; 17 my $self = { firstName => undef, 18 lastName => undef, 19 hireDay => $hireDay }; 20 21 bless( $self, $class ); 22 return $self; 23 } 24 Employee.pm

  13. 25sub firstName 26 { 27 my $self = shift(); 28 29 $self->{ firstName } = shift() if ( @_ ); 30 Creates a new data member that stores the first name of the employee or returns the value to the user 31 return $self->{ firstName }; 32 } 33 34sub lastName 35 { Stores or returns the value of lastName of the user 36 my $self = shift(); 37 38 $self->{ lastName } = shift() if ( @_ ); 39 40 return $self->{ lastName }; 41 } 42 43sub hireDay Sets or prints the value of hireDay 44 { 45 my $self = shift(); 46 47 if ( @_ ) { 48 $self->{ hireDay }->setDate( @_ ); 49 } 50 else { 51 $self->{ hireDay }->print(); 52 } 53 } 54 Employee.pm

  14. 1 #!/usr/bin/perl 2 # Fig. 14.7: Hourly.pm 3 # Implementation of class Hourly. 4 5 package Hourly; 6 7 use strict; 8 use warnings; 9 use Employee; @ISA is used to implement class Employee 10our @ISA = ( "Employee" ); 11 12sub new 13 { The new constructor creates an hourly object 14 my $object = shift(); 15 my $class = ref( $object ) || $object; 16 17my $self = $class->SUPER::new(); The SUPER keyword is used to call the new method of Employee, the class we are inheriting from 18 $self->{ rate } = undef; 19 20 bless( $self, $class ); 21 return $self; 22 } 23 Hourly.pm

  15. 24sub rate 25 { 26 my $self = shift(); 27 Sets the rate to which the hourly employee makes per hour 28 $self->{ rate } = shift() if ( @_ ); 29 30 return $self->{ rate }; 31 } 32 Hourly.pm

  16. 1 #!/usr/bin/perl 2 # fig. 14.8: fig14_08.pl 3 # Using classes Hourly and Employee 4 5 use strict; The program uses both Employee and Hourly 6 use warnings; 7use Employee; 8 use Hourly; 9 10 my $worker = new Hmployee; 11 12$worker->firstName( "Jason" ); 13 $worker->lastName( "Black" ); Sets the values of firstName, lastName and hireDay 14 $worker->hireDay( 8, 5, 1995 ); 15print( $worker->firstName(), " ", 16 $worker->lastName(), " was hired on " ); 17 $worker->hireDay(); Prints out the data just entered by getting them from the object’s data members 18 print( ".\n\n" ); 19 20 my $hour = new Hourly; 21 22 $hour->firstName( "John" ); 23$hour->lastName( "White" ); Sets the values of firstName, lastName, hireDay and rate 24 $hour->hireDay( 11, 30, 1999 ); 25 $hour->rate( 9.50 ); 26 print( $hour->firstName(), " ", $hour->lastName(), 27 " was hired on " ); 28 $hour->hireDay(); 29 print( ".\n" ); 30 printf( "He makes \$%.2f per hour.\n", $hour->rate() ); fig14_08.pl

  17. Jason Black was hired on 8/5/1995. John White was hired on 11/30/1999. He makes $9.50 per hour. fig14_08.pl Program Output

  18. Overriding Methods • Method overriding • Redefine an inherited method in a derived class • Uses that class over the base class • Derived class methods take precedence over base class methods

  19. 1 #!/usr/bin/perl 2 # Fig. 14.9: Employee2.pm 3 # Implementation of class Employee2. 4 5 package Employee2; 6 7 use strict; 8 use warnings; 9 use Date; 10 The new constructor creates a new object of the right type. 11sub new 12 { 13 my $object = shift(); 14 my $class = ref( $object ) || $object; 15 Each attribute is initialized when the object is created 16my $self = { firstName => shift(), 17 lastName => shift(), }; 18 19my $hireDay = new Date; In this employee class the hireDay is actually a Date object 20 21 if ( $_[ 0 ] ) { 22 my ( $month, $day, $year ) = split( /\//, $_[ 0 ] ); 23 $hireDay->day( $day ); 24 $hireDay->month( $month ); 25 $hireDay->year( $year ); 26 } 27 28 $self->{ hireDay } = $hireDay; 29 30 bless( $self, $class ); Employee2.pm

  20. 31 return $self; 32 } 33 34sub firstName Get or sets the value of firstName 35 { 36 my $self = shift(); 37 38 $self->{ firstName } = shift() if ( @_ ); 39 40 return $self->{ firstName }; 41 } 42 43sub lastName 44 { Get or sets the value of lastName 45 my $self = shift(); 46 47 $self->{ lastName } = shift() if ( @_ ); 48 49 return $self->{ lastName }; 50 } 51 52sub hireDay 53 { Get or sets the value of hireDay 54 my $self = shift(); 55 56 if ( @_ ) { 57 $self->{ hireDay }->setDate( @_ ); 58 } 59 else { 60 $self->{ hireDay }->print(); 61 } 62 } Employee2.pm

  21. 63 64sub write Writes out all of the data about the user 65 { 66 my $self = shift(); 67 68 print( "Hello, my name is ", $self->firstName, ".\n" ); 69 print( "I was hired on " ); 70 $self->hireDay(); 71 print( ".\n" ); 72 } 73 Employee2.pm

  22. 1 #!/usr/bin/perl 2 # Fig. 14.10: Hourly2.pm 3 # Implementation of class Hourly2. 4 5 package Hourly2; 6 7 use strict; 8 use warnings; Inherits Employee2 9 use Employee2; 10our @ISA = ( "Employee2" ); 11 Uses the base class to get the three data members 12 sub new 13 { 14 my $object = shift(); 15 my $class = ref( $object ) || $object; 16 17my $self = $class->SUPER::new( @_[ 0 .. 2 ]); 18 $self->{ rate } = $_[ 3 ]; 19 20 bless( $self, $class ); 21 return $self; 22 } 23 24 sub rate 25 { 26 my $self = shift(); 27 28 $self->{ rate } = shift() if ( @_ ); 29 30 return $self->{ rate }; 31 } Hourly2.pm

  23. 32 Base class is overridden 33sub write 34 { 35 my $self = shift(); 36 $self->SUPER::write(); 37 printf( "I make \$%.2f per hour.\n", $self->rate ); 38 } 39 Calls the write method of the base class and then outputs the extra data Hourly2.pm

  24. 1 #!/usr/bin/perl 2 # Fig. 14.11: Boss.pm 3 # Implementation of class Boss. 4 5 package Boss; 6 7 use strict; 8 use warnings; Inherits Employee2 9 use Employee2; 10our @ISA = ( "Employee2" ); 11 12 sub new Uses the base class to get the three data members 13 { 14 my $object = shift(); 15 my $class = ref( $object ) || $object; 16 17my $self = $class->SUPER::new( @_[ 0 .. 2 ] ); 18 19 $self->{ title } = $_[ 3 ]; 20 21 bless( $self, $class ); 22 return $self; 23 } 24 25sub title The new method, title, sets the rank that the employee holds in the office 26 { 27 my $self = shift(); 28 29 $self->{ title } = shift() if ( @_ ); 30 31 return $self->{ title }; 32 } Boss.pm

  25. The write method uses the Employee2write method and also adds more to it 33 34sub write 35 { 36 my $self = shift(); 37 38 print( "My name is Mr. ", $self->lastName(), ".\n" ); 39 print( "I am the boss and I am a ", $self->title(), ".\n" ); 40 } 41 Boss.pm

  26. 1 #!/usr/bin/perl 2 # Fig. 14.12: fig14_12.pl 3 # Overriding base class methods. Uses all three of the of the created classes. Employee2, Hourly2 and Boss 4 5 use strict; 6 use warnings; 7use Employee2; Creates a new Employee2 object and specifies its data 8 use Hourly2; 9 use Boss; Creates a new Hourly2 object 10 11my $worker = new Employee2( "Jason", "Black", "8/5/1995" ); 12 13 print( $worker->firstName(), " ", 14 $worker->lastName(), " was hired on " ); 15 $worker->hireDay(); 16 print( ".\n" ); 17 $worker->write(); 18 print( "\n" ); 19 20my $hour = new Hourly2; 21 22 $hour->firstName( "John" ); 23 $hour->lastName( "White" ); 24 $hour->hireDay( 11, 30, 1999 ); 25 $hour->rate( 9.50 ); 26 print( $hour->firstName(), " ", 27 $hour->lastName(), " was hired on " ); fig14_12.pl

  27. 28 $hour->hireDay(); 29 print( ".\n" ); 30 printf( "This worker makes \$%.2f per hour.\n", $hour->rate() ); Creates a new Boss object, and then sets and outputs its data 31 $hour->write(); 32 print( "\n" ); 33 34my $boss = new Boss; 35 36 $boss->firstName( "George" ); 37 $boss->lastName( "Red" ); 38 $boss->hireDay( 5, 31, 1979 ); 39 $boss->title( "manager" ); 40 print( $boss->firstName(), " ", 41 $boss->lastName(), " is the boss.\n" ); 42 print( "This worker was hired " ); 43 $boss->hireDay(); 44 print( " and their title is ", $boss->title(), ".\n" ); 45 $boss->write(); 46 print( "\n" ); fig14_12.pl

  28. Jason Black was hired on 8/5/1995. Hello, my name is Jason. I was hired on 8/5/1995. John White was hired on 11/30/1999. This worker makes $9.50 per hour. Hello, my name is John. I was hired on 11/30/1999. I make $9.50 per hour. George Red is the boss. This worker was hired 5/31/1979 and their title is manager. My name is Mr. Red. I am the boss and I am a manager. fig14_12.pl Program Output

  29. Other Class Relationships: Multiple Inheritance, Composition and Containment • Multiple inheritance • Adding more than one class to the @ISA array • Depth first search • Composition • The “has a” relationship • One or more attributes that are of other classes • Containment • A class that can store objects of another class • Container classes

  30. 1 #!/usr/bin/perl 2 # Fig. 14.13: WorkGroup.pm 3 # Implementation of class WorkGroup. 4 5 use strict; 6 use warnings; Workgroup.pm inherits from Employee2, Hourly2 and Boss 7use Employee2; 8 use Hourly2; 9 use Boss; 10 11 package WorkGroup; 12 13 sub new 14 { 15 my $object = shift(); 16 my $class = ref( $object ) || $object; 17 An anonymous array is created to store employees 18my $group = [ @_ ]; 19 20 bless( $group, $class ); 21 return $group; 22 } 23 The add method allow the user to add a new employee to the list 24sub add 25 { 26 my $object = shift(); 27 28 foreachmy $employee ( @_ ) { 29 push( @$object, $employee ); 30 } 31 } WorkGroup.pm

  31. 32 33sub listMembers Lists all the members that are contained within the WorkGroup array 34 { 35 my $object = shift(); 36 37 foreachmy $employee ( @$object ) { 38 $employee->write(); 39 } 40 } 41 WorkGroup.pm

  32. 1 #!/usr/bin/perl 2 # Fig. 14.14: fig14_14.pl 3 # Demonstrating composition. 4 Creates a new Emplyee2 object 5 use strict; 6 use warnings; Creates a new Hourly2 object 7 use Employee2; Creates a new WorkGroup object and stores the two new objects within it 8 use Hourly2; 9 use Boss; Creates a new boss abject and adds to the array 10 use WorkGroup; Lists everything in the array 11 12my $worker = new Employee2( "Jason", "Black", "8/5/1995" ); 13my $hour = new Hourly2( "John", "White", "11/30/1999", 9.50 ); 14 15my $group = new WorkGroup( $worker, $hour ); 16 17 my $boss = new Boss( "George", "Red", "5/31/1979", "manager" ); 18 19$group->add( $boss ); 20 21$group->listMembers(); fig14_14.pl

  33. Hello, my name is Jason. I was hired on 8/5/1995. Hello, my name is John. I was hired on 11/30/1999. I make $9.50 per hour. My name is Mr. Red. I am the boss and I am a manager. fig14_14.pl Program Output

  34. Base Class UNIVERSAL • The UNIVERSAL class • Perl looks here after every other class • Want to add to all classes; add to UNIVERSAL class • The isa method • Whether a class inherits from another class or not • The can method • Whether a class has a particular implementation of a method

  35. 1 #!/usr/bin/perl 2 # Fig. 14.15: fig14_15.pl 3 # Using the UNIVERSAL base class methods. 4 5 use strict; Creates a hash with 3 different objects in it 6 use warnings; 7 use Employee2; Loops for each key in the hash Checks to see if each key in the hash is an Hourly2 object Checks to see if each key in the hash has the method rate 8 use Hourly2; 9 use Boss; 10 11my %people = ( employee => new Employee2, 12 hourly => new Hourly2, 13 boss => new Boss ); 14 15foreachmy $key ( keys( %people ) ) { 16 my $object = $people{ $key }; 17 18 print( "$key is a employee2\n" ) 19 if $object->isa( 'Employee2' ); 20 print( "$key is a hourly2\n" ) if $object->isa( 'Hourly2' ); 21 print( "$key is a boss\n" ) if $object->isa( 'Boss' ); 22 print( "$key can write\n" ) if $object->can( 'write' ); 23 print( "$key can rate\n" ) if $object->can( 'rate' ); 24 print( "$key can title\n" ) if $object->can( 'title' ); 25 print( "\n" ); 26 } fig14_15.pl

  36. employee is a employee2 employee can write boss is a employee2 boss is a boss boss can write boss can title hourly is a employee2 hourly is a hourly2 hourly can write hourly can rate fig14_15.pl Program Output

  37. Encapsulation: Public vs. Private • Encapsulation • The hiding of data • Prevent a user from accessing certain parts of the program • Helps to prevent updating errors • No private methods • Trust programmer to use interface • Do not use only if • Accept the consequences • Know what they are doing

  38. Closure Method • Closure • Can use deep binding and closure to “privatize” data • Variables in a closer are not available outside of the that closure • Must be called with the appropriate arguments • One = key to be returned • Two = key and new element for the key

  39. 1 #!/usr/bin/perl 2 # Fig. 14.16: EmployeeClosure.pm 3 # Encapsulating data using closures. 4 5 package EmployeeClosure; Creates a reference to a closure 6 7 use strict; 8 use warnings; Will only pass the attributes when sent the right arguments 9 use Date; 10 11 sub new 12 { 13 my $object = shift(); 14 my $class = ref( $object ) || $object; 15 16my $employee = { firstName => shift(), 17 lastName => shift(), }; 18 19 my $hireDay = new Date; 20 21if ( $_[ 0 ] ) { 22 my ( $month, $day, $year ) = split( /\//, $_[ 0 ] ); 23 $hireDay->day( $day ); 24 $hireDay->month( $month ); 25 $hireDay->year( $year ); 26 } 27 EmployeeClosure.pm

  40. 28 $employee->{ hireDay } = $hireDay; 29 30 my $self = sub { 31 my $field = shift(); 32 33 $employee->{ $field } = shift() if ( @_ ); 34 return $employee->{ $field }; 35 }; 36 The closure is blessed to make an object 37 bless( $self, $class ); 38 return $self; 39 } 40 41 sub firstName 42 { 43 my $self = shift(); Each get/set method calls the closure by dereferencing the function 44 45 &{ $self }( "firstName", @_ ); 46 } 47 48 sub lastName 49 { 50 my $self = shift(); 51 52 &{ $self }( "lastName", @_ ); 53 } 54 EmployeeClosure.pm

  41. 55 sub hireDay 56 { 57 my $self = shift(); 58 59 my $date = &{ $self }( "hireDay" ); 60 Displays the data of the object 61 $date->setDate( @_ ) if ( @_ ); 62 63 return $date; 64 } 65 66sub write 67 { 68 my $self = shift(); 69 70 print( "Hello, my name is ", $self->( 'firstName' ), ".\n" ); 71 print( "I was hired on " ); 72 73 my $date = $self->( 'hireDay' ); 74 $date->print(); 75 print( ".\n" ); 76 } 77 78 return 1; EmployeeClosure.pm

  42. 1 #!/usr/bin/perl 2 # Fig. 14.17: fig14_17.pl 3 # Using the EmployeeClosure class 4 Creates a new object and sets its values 5 use strict; Sets the values of the EmployeeClosure object 6 use warnings; 7 use EmployeeClosure; Displays the data 8 9my $first = new EmployeeClosure( "Jason", "Black", "8/5/1995" ); 10 my $second = new EmployeeClosure; 11 12$second->firstName( "John" ); 13 $second->lastName( "White" ); 14 $second->hireDay( 11, 30, 1999 ); 15 16 $first->write(); 17$second->write(); fig14_17.pl Program Output Hello, my name is Jason. I was hired on 8/5/1995. Hello, my name is John. I was hired on 11/30/1999.

  43. Implicit Functions • Explicit functions • Need to be called by the programmer • Implicit functions • Automatically called • Generally in all uppercase • BEGIN • END • DESTROY • Most languages do not allow modification of them • Calling in a program is an error

  44. 1 #!/usr/bin/perl 2 # Fig. 14.18: Circular.pm 3 # Using the DESTROY function to destroy objects. 4 5 package Circular; 6 7 use strict; 8 use warnings; 9 10 sub new { 11 my $class = shift(); 12 13 my $a; 14 my $b = \$a; 15 16 $a = \$b; 17 18 my $self = {}; 19 Creates the new object and returns it to the user 20 $self->{ name } = shift(); 21 $self->{ Circle } = \$a; 22 23 bless( $self, $class ); 24 return $self; 25 } 26 27 sub sayHi { 28 my $self = shift(); 29 30 print( "$self->{ name } says hi.\n" ); 31 } Circular.pm

  45. 32 33sub DESTROY { The DESTROY function; will execute automatically 34 my $self = shift(); 35 36 print( "Destroying $self->{ name }.\n" ); 37 ${ $self->{ Circle } } = undef; 38 } 39 40return 1 Displays the variable that went out of scope Circular.pm

  46. 1 #!/usr/bin/perl 2 # Fig. 14.19: 19.pl 3 # Demonstrating the DESTROY function. 4 5 use warnings; 6 use strict; 7 use Circular; 8 9 my $a = new Circular( "Bob" ); 10 11 { 12 print( "Entering First Scope.\n" ); 13 Creates a new object 14my $b = new Circular( "Joe" ); 15 16 { 17 print( "Entering Second Scope.\n" ); 18 Creates a new object 19my $c = new Circular( "Alice" ); 20 $c->sayHi(); 21 print( "Exiting Second Scope.\n" ); When the Alice object’s scope ends the DESTROY function is automatically called 22 } 23 24 $b->sayHi(); 25 print( "Exiting First Scope.\n" ); When the Joe object’s scope ends the DESTROY function is again called automatically 26} 27 28 $a->sayHi(); 29 print( "Exiting program.\n" ); 19.pl

  47. Entering First Scope. Entering Second Scope. Alice says hi. Exiting Second Scope. Destroying Alice. Joe says hi. Exiting First Scope. Destroying Joe. Bob says hi. Exiting program. Destroying Bob. 19.pl Program Output

  48. AUTOLOAD Function • The AUTOLOAD function • Executes when a program cannot find a method in a class or any of its derived classes • The $AUTOLOAD variable receives the called name • Can also be used to prevent several set methods from doing the same thing with only different variables

  49. 1 #!/usr/bin/perl 2 # Fig. 14.20: Date2.pm 3 # Implementation of autoloaded date2 class. 4 5 package Date2; 6 use strict; 7 use warnings; 8 9 my @months = (0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); 10 11 sub new 12 { 13 my $object = shift(); 14 my $class = ref( $object ) || $object; 15 16 my $date = { year => 1000, 17 month => 1, 18 day => 1, }; 19 20 bless( $date, $class ); 21 return $date; The AUTOLOAD function is called whenever a call to a function is made that cannot be found 22 } 23 24sub AUTOLOAD 25 { 26 my $object = shift(); Makes sure the method was called on an object 27my $type = ref( $object ) || 28 die( "$object is not an object" ); Store method name into $name 29 30my $name = $Date2::AUTOLOAD; Date2.pm

  50. 31 32 $name =~ s/.*://; Eliminates the package name 33 exit() if ( $name eq 'DESTROY' ); 34 35 unless ( exists( $object->{ $name } ) ) { 36 die( "Cannot access '$name' field of class $type" ); 37 } Makes sure the method called was not DESTROY 38 39if ( @_ ) { 40 return $object->{ $name } = shift(); 41 } 42 else { Checks to see if a value was passed and if so, sets in the object 43 return $object->{ $name }; 44 } 45 } 46 47 sub setDate 48 { 49 if ( @_ == 4 ) { 50 my $object = shift(); 51 52 $object->year( $_[ 0 ] ); 53 $object->month( $_[ 1 ] ); 54 $object->day( $_[ 2 ] ); 55 } 56 else { 57 print( "the .\n" ); 58 } 59 } 60 Date2.pm