Search code examples
requirereadlineraku

require Readline - You cannot create an instance of this type (Readline)


When I run this code

require Readline;
my $rl = Readline.new;
my $string = $rl.readline( ':');
$string.say;

I get this error-message:

You cannot create an instance of this type (Readline)

When I use useto load Readline it works. Why does require Readline not work?


Solution

  • Since require causes the module to be loaded at runtime, the lookup of the Readline symbol must also be deferred until runtime. This can be done using the ::('Type::Name') syntax, as follows:

    require Readline;
    my $rl = ::('Readline').new;
    my $string = $rl.readline( ':');
    $string.say;