Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import simple module

Tags:

module

raku

I have a function written in a module file (.pm), and want to use it in a Perl6 file (.pl6). The two files are in the same folder:

C:\Users\Christian\Dropbox\ChristianPrivatefiler\Programmering\Perl6\perlCode

I tried to use the answer to Perl6: implicit and explicit import but my code returned this error:

===SORRY!=== Could not find chrmodule1 at line 5 in:
    C:\Users\Christian\Dropbox\ChristianPrivatefiler\Programmering\Perl6\modules
    C:\Users\Christian\.perl6
    C:\rakudo\share\perl6\site
    C:\rakudo\share\perl6\vendor
    C:\rakudo\share\perl6
    CompUnit::Repository::AbsolutePath<84241584>
    CompUnit::Repository::NQP<86530680>
    CompUnit::Repository::Perl5<86530720> [Finished in 0.436s]

Here is the .pm file, chrmodule1.pm:

module chrmodule1 {
    sub foo is export(:DEFAULT, :one) {
        say 'in foo';
    }
}

Here is the .pl6 file, testOfCode3.pl6:

use v6;
use lib 'modules';
use chrmodule1;

foo();
  • I use this editor: atom.io
  • Here is another solution to the problem that returned the same error when I tried it: perl6maven.com/tutorial/perl6-modules-export
  • The desired result was:

     in foo [Finished in 0.317s]
    
like image 499
Arimaafan Avatar asked Dec 06 '25 05:12

Arimaafan


1 Answers

The second line of testOfCode3.pl6 should be use lib 'perlCode';.


You wrote:

I have a function written in a module file (.pm), and want to use it in a Perl6 file (.pl6). The two files are in the same folder:

C:\Users\Christian\Dropbox\ChristianPrivatefiler\Programmering\Perl6\perlCode

So, you've stored the module in a folder called perlCode.

When you run testOfCode3.pl6 you get an error:

===SORRY!=== Could not find chrmodule1 at line 5 in:
    C:\Users\Christian\Dropbox\ChristianPrivatefiler\Programmering\Perl6\modules

So, the Rakudo Perl 6 compiler looked for chrmodule in a folder called modules. Why? Because you told it to:

Here is the .pl6 file, testOfCode3.pl6:

use v6;
use lib 'modules';

A use lib ... statement tells a Perl 6 compiler where to look first for modules. You've added modules, so the Rakudo Perl 6 compiler looked in the modules folder first.

It didn't find your module there so it continued on, looking elsewhere. Hence the lines listing C:\Users\Christian\.perl6 etc.

In the end it never finds your module because your module is in perlCode and you haven't told the compiler to look there. (And it refuses to just look in the current directory for solid security reasons.)

like image 65
raiph Avatar answered Dec 09 '25 17:12

raiph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!