Re: Object Contruction
Hi On 03/18/2015 01:06 PM, Tom Browder wrote: My new object needs some methods run during construction. How can I do that without defining my own new method? http://doc.perl6.org/language/objects#Object_Construction lists at least two possible ways. Probably the most interesting one is BUILDALL with a callsame; see the last example (or example skeleton) in that section. Cheers, Moritz
Re: Object Contruction
On Wed, Mar 18, 2015 at 7:22 AM, Moritz Lenz mor...@faui2k3.org wrote: ... http://doc.perl6.org/language/objects#Object_Construction lists at least two possible ways. Probably the most interesting one is BUILDALL with a callsame; see the last example (or example skeleton) in that section. Thanks, Moritz, I read that but it's a bit confusing to me. I'll experiment with it some more and hopefully have some more specific questions. Best, -Tom
Re: Object Contruction
On Wed, Mar 18, 2015 at 11:32 AM, Tom Browder tom.brow...@gmail.com wrote: On Wed, Mar 18, 2015 at 7:22 AM, Moritz Lenz mor...@faui2k3.org wrote: ... http://doc.perl6.org/language/objects#Object_Construction lists at least two possible ways. Probably the most interesting one is BUILDALL with a callsame; see the last example (or example skeleton) in that section. For my purposes I think the BUILD is best. The BUILDALL method seems to put me in limbo as far as the constructed object and using self. I made a simple class and a driver Perl script: $ cat T.pm HERE_PM class T; has $.a is rw; has $.b is rw; has $.c is rw; submethod BUILD( # set defaults here :$!a = 15, :$!b, :$!c, ) { self.set_b; } multi method set_b { if (self.a 10) { self.b = 1; } else { self.b = 0; } } multi method set_b($x) { self.b = $x; } HERE_PM $ cat T.pl HERE_PL #!/usr/bin/env perl6 use v6; use lib '.'; use T; my $t = T.new; say \$t.a = {$t.a}; say \$t.b = {$t.b}; say \$t.c = {$t.c.defined ?? $t.c !! 'undefined'}; $t.set_b(20); $t.c = 'defined'; say \$t.a = {$t.a}; say \$t.b = {$t.b}; say \$t.c = {$t.c.defined ?? $t.c !! 'undefined'}; HERE_PM $ perl6 T.pl $t.a = 15 $t.b = 0 $t.c = undefined $t.a = 15 $t.b = 20 $t.c = defined Best, -Tom
Re: Object Contruction
You are correct, Liz, but I was trying those pieces to demonstrate to myself that all was available to me in the methods and all worked as I expected. It demos very roughly what I think I have to do to translate Geo::Ellipsoid to Perl 6. It's a WIP and I'm learning Perl 6 as I go. The prog is a toy and not otherwise useful. Thanks. BTW, will you or any other Perl 6 people be presenting at YAPC::NC? I don't see a speaking line-up yet. Best, -Tom
Re: Object Contruction
On 18 Mar 2015, at 23:21, Tom Browder tom.brow...@gmail.com wrote: You are correct, Liz, but I was trying those pieces to demonstrate to myself that all was available to me in the methods and all worked as I expected. It demos very roughly what I think I have to do to translate Geo::Ellipsoid to Perl 6. It's a WIP and I'm learning Perl 6 as I go. The prog is a toy and not otherwise useful. Thanks. BTW, will you or any other Perl 6 people be presenting at YAPC::NC? I don't see a speaking line-up yet. YAPC::NC ?? You mean YAPC::NA? I will be there, but haven’t had any inspiration for a presentation just yet. Liz
Re: Object Contruction
On 18 Mar 2015, at 21:32, Tom Browder tom.brow...@gmail.com wrote: On Wed, Mar 18, 2015 at 11:32 AM, Tom Browder tom.brow...@gmail.com wrote: On Wed, Mar 18, 2015 at 7:22 AM, Moritz Lenz mor...@faui2k3.org wrote: ... http://doc.perl6.org/language/objects#Object_Construction lists at least two possible ways. Probably the most interesting one is BUILDALL with a callsame; see the last example (or example skeleton) in that section. For my purposes I think the BUILD is best. The BUILDALL method seems to put me in limbo as far as the constructed object and using self. I made a simple class and a driver Perl script: $ cat T.pm HERE_PM class T; has $.a is rw; has $.b is rw; has $.c is rw; submethod BUILD( # set defaults here :$!a = 15, :$!b, Why not set the default here? :$!b = $!a 10 ?? 1 !! 0; :$!c, ) { self.set_b; Then you won’t need this. } multi method set_b { if (self.a 10) { self.b = 1; } else { self.b = 0; } } Nor this. multi method set_b($x) { self.b = $x; } Nor this. HERE_PM $ cat T.pl HERE_PL #!/usr/bin/env perl6 use v6; use lib '.'; use T; my $t = T.new; say \$t.a = {$t.a}; say \$t.b = {$t.b}; say \$t.c = {$t.c.defined ?? $t.c !! 'undefined'}; $t.set_b(20); This would then just be: $t.set(20); $t.c = 'defined'; say \$t.a = {$t.a}; say \$t.b = {$t.b}; say \$t.c = {$t.c.defined ?? $t.c !! 'undefined'}; HERE_PM $ perl6 T.pl $t.a = 15 $t.b = 0 $t.c = undefined $t.a = 15 $t.b = 20 $t.c = defined Liz