Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is "require" defined?

Tags:

raku

rakudo

mop

I have been looking in Rakudo source for the implementation of require, first out of curiosity and second because I wanted to know if it was returning something.

I looked up sub require and it returned this hit, which actually seems to be the source for require, but it's called sub REQUIRE_IMPORT. It returns Nil and is declared as such, which pretty much answers my original question. But now my question is: Where's the mapping from that sub to require? Is it really the implementation for that function? Are there some other functions that are declared that way?

like image 937
jjmerelo Avatar asked Dec 05 '18 11:12

jjmerelo


1 Answers

require is not a sub, but rather a statement control (so, in the same category of things like use, if, for, etc.) It is parsed by the Perl 6 grammar and there are a few different cases that are accepted. It is compiled in the Perl 6 actions, which has quite a bit to handle.

Much of the work is delegated to the various CompUnit objects, which are also involved with use/need. It also has to take care of stubbing symbols that the require will bring in, since the set of symbols in a given lexical scope is fixed at compile time, and the REQUIRE_IMPORT utility sub is involved with the runtime symbol import too.

The answer to your question as to what it will evaluate to comes at the end of the method:

    $past.push($<module_name>
               ?? self.make_indirect_lookup($longname.components())
               !! $<file>.ast);

Which means:

  • If it was a require Some::Module then evaluate to a lookup of Some::Module
  • If it was a require $file style case, evaluate to the filename
like image 56
Jonathan Worthington Avatar answered Sep 30 '22 23:09

Jonathan Worthington