Content deleted Content added
ClueBot NG (talk | contribs) m Reverting possible vandalism by Jinglebells987 to version by Iggy the Swan. Report False Positive? Thanks, ClueBot NG. (3431394) (Bot) |
Add code example of Perl 6 |
||
Line 477:
</source>
=== Perl 5 ===
In [[Perl|Perl programming language]] 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'' as well as a constructor ''new_from_file'' which reads a file for Person attributes, and ''new_from_person'' which uses another Person object as a template.
Line 512:
</source>
=== Perl 5 with Moose ===
With the [[Moose perl|Moose object system]] for Perl, most of this boilerplate can be left out, a default ''new'' is created, attributes can be specified, as well as 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.
Line 544:
use Person;
my $p = Person->new( first_name => 'Sam', last_name => 'Ashe', age => 42 );
</source>
=== Perl 6 ===
With Perl 6, even more boilerplate can be left out, a default ''new'' method is inherited, attributes can be specified, as well as whether they can be set, reset, or are required. In addition, any extra constructor functionality can be included in a ''BUILD'' method which will get called to allow for custom initialization. A ''TWEAK'' method can be specified to post-process any attributes already (implicitely) initialized.
<source lang="perl6">
class Person {
# first name (a string) can only be set at construction time (the . means "public")
has Str $.first-name is required;
# last name (a string) can only be set at construction time (a ! would mean "private")
has Str $.last-name is required;
# age (Integer) can be modified after construction ('rw'), and is not required
# to be passed to be constructor.
has Int $.age is rw;
# Create a 'has-age' method which returns true if age has been set
method has-age { self.age.defined }
# Check custom requirements
method TWEAK {
if $self.has_age && $self.age < 18 { # no under 18s
die "No under-18 Persons";
}
}
}
</source>
In both cases the Person class is instantiated just like in Perl 5:
<source lang="perl6">
use Person;
my $p = Person.new( first-name => 'Sam', last-name => 'Ashe', age => 42 );
</source>
But Perl 6 also has another way of specifying named parameters using the colon syntax:
<source lang="perl6">
my $p = Person.new( :first-name<Sam>, :last-name<Ashe>, :age(42) );
</source>
And should you have set up variables with names identical to the named parameters, you can use a shortcut that will use the '''name''' of the variable for the named parameter:
<source lang="perl6">
my $first-name = "Sam";
my $last-name = "Ashe";
my $age = 42;
my $p = Person.new( :$first-name, :$last-name, :$age );
</source>
|