Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Constructor (object-oriented programming)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Perl 5 === In [[Perl]] version 5, by default, constructors are [[factory method]]s, that is, methods that create and return the object, concretely meaning create and return a blessed reference. A typical object is a reference to a hash, though rarely references to other types are used too. By convention the only constructor is named ''new'', though it is allowed to name it otherwise, or to have multiple constructors. For example, a Person class may have a constructor named ''new'', and a constructor ''new_from_file'' which reads a file for Person attributes, and ''new_from_person'' which uses another Person object as a template. <syntaxhighlight lang="perl"> package Person; # In Perl constructors are named 'new' by convention. sub new { # Class name is implicitly passed in as 0th argument. my $class = shift; # Default attribute values, if you have any. my %defaults = ( foo => "bar" ); # Initialize attributes as a combination of default values and arguments passed. my $self = { %defaults, @_ }; # Check for required arguments, class invariant, etc. if ( not defined $self->{first_name} ) { die "Mandatory attribute missing in Person->new(): first_name"; } if ( not defined $self->{last_name} ) { die "Mandatory attribute missing in Person->new(): last_name"; } if ( defined $self->{age} and $self->{age} < 18 ) { die "Invalid attribute value in Person->new(): age < 18"; } # Perl makes an object belong to a class by 'bless'. bless $self, $class; return $self; } 1; </syntaxhighlight> ==== Perl 5 with Moose ==== In the [[Moose perl|Moose object system]] for Perl, most of this boilerplate can be omitted, a default ''new'' is created, attributes can be specified, and whether they can be set, reset, or are required. In addition, any extra constructor functionality can be included in a ''BUILD'' method which the Moose generated constructor will call, after it has checked the arguments. A ''BUILDARGS'' method can be specified to handle constructor arguments not in hashref / key => value form. <syntaxhighlight lang="perl"> package Person; # enable Moose-style object construction use Moose; # first name ( a string) can only be set at construction time ('ro') has first_name => (is => 'ro', isa => 'Str', required => 1); # last name ( a string) can only be set at construction time ('ro') has last_name => (is => 'ro', isa => 'Str', required => 1); # age (Integer) can be modified after construction ('rw'), and is not required # to be passed to be constructor. Also creates a 'has_age' method which returns # true if age has been set has age => (is => 'rw', isa => 'Int', predicate => 'has_age'); # Check custom requirements sub BUILD { my $self = shift; if ($self->has_age && $self->age < 18) { # no under 18s die "No under-18 Persons"; } } 1; </syntaxhighlight> In both cases the Person class is instiated like this: <syntaxhighlight lang="perl"> use Person; my $p = Person->new( first_name => 'Sam', last_name => 'Ashe', age => 42 ); </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)