Perl6: How to dynamically load a class module?

How can I dynamically load an OO style module?

#!/usr/bin/env perl6 use v6; my $r = prompt ':'; if $r { require Text::CSV; # Error: my $csv = Text::CSV.new; # Could not find symbol '&CSV' } else { require File::Temp <&tempfile>; my ( $filename , $filehandle ) = tempfile; # this works } 
+7
oop module perl6
source share
1 answer

As explained in the perl6 doco here , you can dynamically load the module, but

To import characters, you must define them at compile time.

So, the code in the else works because of an explicit import request <&tempfile> .

The closest that the code in the if works, what I see is this (which is mostly taken from the previous doco link):

 use v6.c ; sub load-a-module($name) { require ::($name) ; my $instance = ::($name).new() ; return $instance ; } my $module = "Text::CSV" ; my $csv = load-a-module $module ; say $csv.WHAT ; # say $csv.^methods ; # if you really want to be convinced # outputs: (CSV) 
+9
source share

All Articles