- Perl ›
- Object Oriented ›
- Class template
Class template
Here is a template of the class.
1. Class template
The following is a template for the class.
package Book; use strict; use warnings; # Constructor sub new { my $proto = shift; my $class = ref $proto || $proto; my $self = {}; bless $self, $class; $self->init(@_); return $self; } # Object initialization sub init { my ($self, @args) = @_; # Additional processing } # Accessor sub title { my $self = shift; if (@_) { my $old = $self->{title}; return $self->{title} = $_[0]; } else { return $self->{title}; } } sub author { my $self = shift; if (@_) { my $old = $self->{author}; return $self->{author} = $_[0]; } else { return $self->{author}; } } 1;
All you have to do now is add your favorite method.