Categories
Posts in this category
- A shiny perl6.org site
- Creating an entry point for newcomers
- Sprixel, a 6 compiler powered by JavaScript
- Another perl6.org iteration
- Blackjack and Perl 6
- Why I commit Crud to the Perl 6 Test Suite
- This Week's Contribution to Perl 6 Week 5: Implement Str.trans
- This Week's Contribution to Perl 6
- This Week's Contribution to Perl 6 Week 8: Implement $*ARGFILES for Rakudo
- This Week's Contribution to Perl 6 Week 6: Improve Book markup
- This Week's Contribution to Perl 6 Week 2: Fix up a test
- This Week's Contribution to Perl 6 Week 9: Implement Hash.pick for Rakudo
- This Week's Contribution to Perl 6 - Lottery Intermission
- This Week's Contribution to Perl 6 Week 3: Write supporting code for the MAIN sub
- This Week's Contribution to Perl 6 Week 1: A website for proto
- This Week's Contribution to Perl 6 Week 4: Implement :samecase for .subst
- This Week's Contribution to Perl 6 Week 7: Implement try.rakudo.org
- What is the "Cool" class in Perl 6?
- Report from the Perl 6 Hackathon in Copenhagen
- Custom operators in Rakudo
- A Perl 6 Date Module
- Defined Behaviour with Undefined Values
- Dissecting the "Starry obfu"
- The case for distributed version control systems
- Perl 6: Failing Softly with Unthrown Exceptions
- The first Perl 6 module on CPAN
- Gabor: Keep going
- Google Summer of Code Mentor Recap
- Building a Huffman Tree With Rakudo
- Immutable Sigils and Context
- Is Perl 6 really Perl?
- List.classify
- Perl 6: Lost in Wonderland
- Lots of momentum in the Perl 6 community
- Monetize Perl 6?
- Musing and the future of feather and the Pugs repository
- Musings on Rakudo's spectest chart
- My first executable from Perl 6
- My first YAPC - YAPC::EU 2010 in Pisa
- Trying to implement new operators - failed
- Programming Languages Are Not Zero Sum
- Notes from the YAPC::EU 2010 Rakudo hackathon
- Let's build an object
- Perl 6 is optimized for fun
- How to get a parse tree for a Perl 6 Program
- Pascal's Triangle in Perl 6
- Perl 6 in 2009
- Perl 6 ticket life cycle
- The Perl Survey and Perl 6
- The Perl 6 Advent Calendar
- Perl 6 Questions on Perlmonks
- Physical modeling with Math::Model and Perl 6
- How to Plot a Segment of a Circle with SVG
- Publicity for Perl 6
- Fixing Rakudo Memory Leaks
- Rakudo architectural overview
- Rakudo Rocks
- Rakudo "star" announced
- My personal "I want a PONIE" wish list for Rakudo Star
- Rakudo's rough edges
- Rats and other pets
- Releasing Rakudo made easy
- Set Phasers to Stun!
- Starry Perl 6 obfu
- Recent Perl 6 Developments August 2008
- The State of Regex Modifiers in Rakudo
- Strings and Buffers
- Subroutines vs. Methods - Differences and Commonalities
- A SVG plotting adventure
- A Syntax Highlighter for Perl 6
- Test Suite Reorganization: How to move tests
- The Happiness of Design Convergence
- Perl 6 Tidings from September and October 2008
- Perl 6 Tidings for November 2008
- Perl 6 Tidings from December 2008
- Perl 6 Tidings from January 2009
- Perl 6 Tidings from February 2009
- Perl 6 Tidings from March 2009
- Perl 6 Tidings from April 2009
- Perl 6 Tidings from May 2009
- Perl 6 Tidings from May 2009 (second iteration)
- Perl 6 Tidings from June 2009
- Perl 6 Tidings from August 2009
- Perl 6 Tidings from October 2009
- Timeline for a syntax change in Perl 6
- Visualizing match trees
- Want to write shiny SVG graphics with Perl 6? Port Scruffy!
- We write a Perl 6 book for you
- When we reach 100% we did something wrong
- Where Rakudo Lives Now
- Why was the Perl 6 Advent Calendar such a Success?
- What you can write in Perl 6 today
- Why you don't need the Y combinator in Perl 6
- You are good enough!
Sun, 23 Aug 2009
Let's build an object
Permanent link
Building an object in Perl 6 is rather easy. As the author of a class you
don't really have to care (at least in the simplest case), you inherit a
default constructor from class Object. As a consumer of that
class you just write YourClass.new(attrib1 => $value1) to
create an object of class YourClass, at the same time
initializing an attribute.
Running initializations code
If you want to run some initialization code on object creation, you don't
have to touch the new method at all. Something like this
works:
class C { submethod BUILD { say "Created a new instance of C"; } } C.new();
The BUILD submethod is called by the constructor automatically, and can
do any initialization that's necessary. It also receives the named arguments
that the user passes on to new().
(In case you wonder, a submethod is a public method that's not inherited to child classes).
Custom constructors
Suppose you're not a big fan of named arguments, and you want to write a
constructor that takes one mandatory positional parameter. In that case you'd
write a custom new method. To create an object, that method has
to call self.bless:
class C { has $.size; method new($x) { self.bless(*, size => 2 * $x); } } say C.new(3).size; # prints 6
The star * as the first argument to bless tells
it to create an empty object itself.
If you want to enable additional named parameters, that's easily done:
class C { has $.size; method new($x, %n) { self.bless(*, size => 2 * $x, |%n); } }
Note that these two concepts (custom new() and BUILD() (sub)methods) are orthogonal; you can use both at once, and both peacefully coexist.
Understanding object initialization
As demonstrated above you don't need to understand the full process of building and initializing objects to manipulate it. If you still want to know, read on.
Suppose you have a class C which inherits from another class
B, then the process of building an object of class C
looks like this:
The user calls C.new (which is inherited from class Object),
which in turn calls self.bless(*, |%args). bless
creates a new P6Opaque object which is the storage for the newly
created object. This is the call to CREATE in the image above.
After the storage has been allocated and the attributes initialized,
new passes control to BUILDALL (passing along all
named parameters), which in turn calls BUILD in all classes in
the inheritance hierarchy, starting at the top of the hierarchy and calling
the BUILD method of class C at last.
This design allows you to substitute parts of the initialization with least
effort, and especially writing custom new and BUILD
methods very easily.
Instead of passing a * to bless, you can also
pass along an object of different storage type, which means that you can
instruct Perl 6 to store your objects by a different mechanism, for example by
the gnome library GObject or similar
(though that's not yet implemented in Rakudo).
Comments / Trackbacks:
Trackback URL:
/blog-en/perl-6/object-construction-and-initialization.trackback
Chris Dolan wrote
Thanks!
This is exactly the explanation I've been seeking for over a year. Thank you!
Write a comment
The comments on this blog post have been disabled; the comment form below will not work.