I'm rewriting a framework from Perl5 to Perl6 for my work purposes. At some place I need to collect information from other modules/classes by executing a public sub
they might provide; or they may not. So, it necessary to find out if the sub
is present. This is not a big deal when a module is referenced directly (Foo::<&my-sub>
) or by a symbolic name in a string (&::("Foo")::my-sub). But for the simplicity of it I would like to allow to pass module names as-is (lets say collector
is the method collecting the info):
self.collector( Foo );
Where Foo
could be the following:
module Foo {
use Bar;
use Baz;
our sub my-sub { Bar, 'Baz' }
}
And this is where I'm missing something important from Perl6 syntax because the following:
method collector ( $mod ) {
my $mod-name = $mod.WHO;
my @mods;
with &::($mod-name)::my-sub {
@mods.push: &$_();
}
}
is currently the only way I can perform the task.
I didn't try a type capture yet though. Should work as expected, I guess. So, the question is more about extending my knowelge of the syntax.
my-method
, but also talks about asub
. Unlike in Perl 5, where methods are resolved via a package, in Perl 6 they are not; instead, methods are installed in the meta-object and subs default to being lexical. Either can be marked withour
, and only then do they go into the package. It's not quite clear which you want, or if you just want general "how do I do a plugin architecture in Perl 6" kind of answer. – Philinemy multi sub-from-mod (::T Mu:U $mod, Str $sub) { T::{"&{$sub}"} } my multi sub-from-mod (Str $mod, Str $sub) { samewith( ::($mod), $sub ); }
but still wonder if there is better way. – BourneNil
? If so, perhaps can is what you need? More generally, as Jonathan wrote, is your question really a general "how do I do a plugin architecture in P6"? – Splittingour sub
s from module-like objects (stashes, I guess). The.?method
won't work for me because the information being collected could come from anywhere including roles and classes consuming these roles. Any component providing the info could also point to other modules where it should be collected from. Perhaps plugins model would do somehow, but I have no time for redesign. And anyway I'd like to extend my syntax knoweldge. – BourneT::{"&{$sub}"}
with&T::($sub)
. – Splittingmy \T := $mod ~~ Str ?? ::($mod) !! $mod; &T::($subname)
Much shorter and tidier! – Bournemy \Package = $;
ormy \Package = $ = ...;
sets up a variable with the sigil slashed but the identifier still assignable which allows the following.Package &&= ::(Package);
is horribly golfed, demanding a comment, and semantically different from your line but would be a viable alternative to what your ternary does in practice. And becausePackage
has no sigil,&Package::($subname);
would work. :) (Runs off to hide...) – Splittingmy \T = $mod && ::($mod);
which is much easier to grasp when it's a single statement. And it's beautiful! 😉 – Bourne