Skip to content

Instantly share code, notes, and snippets.

@Xliff
Created January 7, 2021 02:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xliff/fcf214158ad5d9da0bf5ae25b9b72dc0 to your computer and use it in GitHub Desktop.
Save Xliff/fcf214158ad5d9da0bf5ae25b9b72dc0 to your computer and use it in GitHub Desktop.
Why is this Trait definition giving me this weird error…

This trait definition:

role Query                   { }
role DefaultQuery does Query { }
multi sub trait_mod:<is> (Method $m, :$default-query is required) {
  $m does DefaultQuery;
  $m.wrap(method (|c) {
    my $sth = $dbh.prepare( _getSQL($m) );
    $sth.execute;
    $sth.allrows;
  });
 }

Now when I use it on a method like this:

 #| SELECT url, name
 #| FROM control_records
 #| WHERE state = 1
 #| ORDER BY url, name
 multi method getActiveSites {
 
 }

It gives me the following error when I try to compile it using cro run:

Lexical with name '$m' does not exist in this frame
at /home/cbwood/Work/.../CountCollector/app/lib/MyApp/Queries.pm6 (MyApp::Queries):101
at /home/cbwood/Work/.../CountCollector/app/service.p6:3

Might someone familiar with Cro have some idea as to why this is occurring?

@Xliff
Copy link
Author

Xliff commented Jan 9, 2021

So removing BRT solved the issue. All that was supposed to do would be to allow Cro::WebApp::Templates to come from strings rather than files. Here's that code:

use v6;

use Cro::WebApp::Template::ASTBuilder;
use Cro::WebApp::Template::Parser;
use Cro::WebApp::Template::Repository;

role Bizzell::Roles::Template {
  has $!template-repo;

  submethod TWEAK {
    $!template-repo = Cro::WebApp::Template::Repository.new
  }

  method render ($template, *%params) {
    my $*TEMPLATE-REPOSITORY = $!template-repo;
    my $ast = Cro::WebApp::Template::Parser.parse(
      $template,
      actions => Cro::WebApp::Template::ASTBuilder
    ).ast;

    Cro::WebApp::Template::Compiled.new(
      |$ast.compile,
      repository => $!template-repo
    ).render(%params);
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment