develooper Front page | perl.perl6.users | Postings from September 2017

Perl 6 Object Construction - General Advice

Thread Next
From:
Mark Devine
Date:
September 30, 2017 17:34
Subject:
Perl 6 Object Construction - General Advice
Message ID:
BLUPR02MB533935274D48778FA8121E1A07F0@BLUPR02MB533.namprd02.prod.outlook.com
Perl 6,

I am preparing to write several classes for some Perl 6 apps I intend to craft in support of a prominent backup suite.  In looking at the documentation & some of the modules published in the Perl 6 ecosystem, I’m not quite sure that I can identify the best idiomatic object construction (OC) approach for my most frequent cases that will stand the test of time.  I don’t want to start writing too many of these using my baby/teenager-Perl6 skills and then go back and refactor ALL of them later with better OC approaches as I learn more.  I’d like to have a boilerplate OC construct that accommodates most cases, if it is possible to zero in on one.

My most common OC case:  initialize attributes at OC time from external input that will be parsed with grammars &/| scrubbed with elaborate conditional tests.

Mu’s new() cannot assign to the class attributes, but BUILD can.  I’ve used that construct successfully, but I’m not convinced that will be the best approach for the long term.


OC: new() { self.bless() } – BUILD() still confuses me.  (I am hopeful for a future Perl 6 book with a 40-page chapter comprehensively detailing the responsibilities of each of these mechanisms.)  I’ve read https://docs.perl6.org/language/classtut & https://docs.perl6.org/language/objects#Object_Construction, but I’m not getting it all.

I’ve tried multiple approaches to initializing variables with defaults, sometimes failing at compile time when I attempt to do it in the wrong place.  Other times, as I move things around, I break default assignments (I.e. has $.var = ‘/tmp’) that worked magically using Mu’s new().  I am still learning by experimentation…

The Perl 6 ecosystem has lots of clever OC constructs, but I’m still not able to infer which I could boilerplate into an “80%-of-the-time-go-to” OC.

I just recently encountered this construct in more than one module (I.e. https://github.com/sergot/io-socket-ssl/blob/master/lib/IO/Socket/SSL.pm6):



method new(*%args is copy) {

    …        # fiddle with the %args hash as required, but obviously not attributes here

    self.bless(|%args)!initialize;

}



method !initialize {

    …        # directly initialize attributes with conditional logic

    self;

}



Is this construct a bona-fide industrial-strength Perl 6 OC idiom that I could employ with confidence?  Should I keep looking?    Also, in this OC approach, I’m not quite seeing how |%args (I.e. with %args<host> = ‘example.com’) ends up magically as $!host (I.e. =’example.com’) in initialize().  I’ll need just a bit more explanation to understand it.



If anyone can provide more detail on the mechanics/expectations/responsibilities/idioms of Perl 6 OC (new() { self.bless() } – BUILD() & more), I would appreciate your input.  All educated advice is welcome.

Thanks,

Mark

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About