4

I have a number of plugin classes each of which have a method 'enable'. The following code works

unit class Top;
has $!rdp;
use Plugin::A;
use Plugin::B;
use Plugin::C;
submethod TWEAK {
  my $rdp := self.rdp;
  Plugin::A.new.enable($rdp);
  Plugin::B.new.enable($rdp);
  Plugin::C.new.enable($rdp);
}

The Plugin... classes are only used once to prepare $rdp, so I thought it would not matter whether the classes are looked up at run time or compile time.

I would like something of the form

for <A B C> {
  require ::("Plugin::$_");
  ::("Plugin::$_").new.enable($rdp)
}

I tried a variety of syntaxes, none of which work.

What would be a way to do this?

4
  • 1
    A perfect Q will "Describe what you’ve already tried and the results of any research". Did you do more than try "a variety of syntaxes"? You didn't mention try, but did you try that? You didn't mention searching (google, SO, Rakudo issues), but did you try that too? I did a search of Rakudo issues for "try require packages"; did you try something like that? Please consider adding such details to your Q. TIA! Commented Jul 5, 2024 at 12:57
  • @raiph all reasonable criticisms. I try to give a minimal question. Yes, lots of searching. Commented Jul 5, 2024 at 13:53
  • Hi @RichardHainsworth "I try to give a minimal question." Appropriate minimalism is good 👍 but when it's inappropriate it's problematic. A mod just suggested I move a comment about that from my answer. You didn't provide a minimal reproducible example. If a Q isn't R as well as M it's very likely too M. I made up for it by spending hours more developing my answer than I would have otherwise needed. That hopefully meant it was a smoother experience for you, but we're not really supposed to do that because it wastes a lot of answerers' time/energy and/or encourages posting of poorly framed Qs and/or As. Hth. 🤞 Commented May 19 at 12:15
  • While I'm adding commentary (long after the horse bolted! 😊) it feels appropriate to mention another aspect of minimalism that SO denizens have come to understand is inappropriate in Qs. I mentioned it in my first comment but couldn't tell from your reply if you had spotted it. The SO community has come to understand that it is detrimental if askers of a Q omit a summary of what they've already tried. I understand your preference for minimalism, but it backfires badly on everyone else (and often the asker too) in that particular respect. (See link in my first comment for discussion of why.) Commented May 19 at 12:23

1 Answer 1

3

TL;DR I've guessed at what you mean. I may be miles off, so this answer may be useless. Even if I guessed correctly, the answer may still be useless or hide problems that will surface in months/years to come because there are related open package loading issues that have not been fully debugged so any solution is just a workaround that happens to work in a given Rakudo release but may not work in the next.


My guess is that you're looking for something like this, which works in the Rakudo (from 2022) that's installed on glot.io at the time of writing this (2024):

for <A B C> {
  my $OK = True;
  require ::("Plugin::$_");
  CATCH { $OK = False; .resume }
  ::("Plugin::$_").new.enable($rdp) if $OK
}

I say "at a guess" because there are many ways to interpret what you wrote and you didn't provide a Minimal, Reproducible Example to help reduce the ambiguity.


My first key discovery as I researched your question was that using the try prefix on require seems to break Rakudo's anticipatory compile time work related to the package symbol corresponding to the package being required. So even if a package was found I couldn't then make use of the package symbol.


My second key discovery was there seem to be many related open issues in the Rakudo issue queue. For example: 1, 2, 3.

Sign up to request clarification or add additional context in comments.

3 Comments

You guessed right about Q. I thought I had got the syntax wrong because I could not get code to run. Turns out it may be a RakuAst problem, or something elsewhere. I should have included the error message: Cannot look up attributes in a RakuAST::Package type object. Did you forget a '.new'? Thank you for confirming I had the syntax correct, and that something like this works without the use experimental pragma
update: require in a loop is now working with use experimental :rakuast
Hi @RichardHainsworth Thx. Afaik I don't currently have access to a latest Rakudo that'll let me test code to figure out what you mean but am curious whether you're saying your original code (in your question) currently works on the latest Rakudo if use experimental :rakuast is in effect, or just the code in my answer, or both. Also, I'm wondering if a simple try require ::("Plugin::$_"); (which did not work when I tried it) now works? (I'm guessing it still doesn't.) Also, do you mean that, if a plug-in does not exist, there's no warning or error messages by default?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.