How can I load a OO-style module dynamically?
#!/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
}
As explained in the perl6 doco here, you can dynamically load the module but;
To import symbols you must define them at compile time.
So, the code in the else
clause works because of the explicit request to import <&tempfile>
.
The closest thing to getting the code in the if
clause to work that I can see is this (which is mostly taken from that earlier doco reference):
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With