|
Perl 6
Using Perl 5 Embedding: Revision 5
Currently, Perl 5 embedding is enabled by default in Pugs. This allows to you use some Perl 5 modules from Perl 6. Here's a basic example:
use perl5:CGI;
my $q = CGI.new('a=Hello');
say $q.param('a');
Notice that add the prefix perl5 when "using" the module, but otherwise use normal Perl 6 object notation. In that example were just passing the string 'a=Hello' to Perl 5. Sometimes additional effort is needed to successfully pass a more complex data structure from Perl 6 to Perl 5. However, if you want to pass anything more than strings between Perl 6 and Perl 5, you may soon run into trouble. For example, Perl 6 does not allow named arguments to start with a dash but Perl 5 does. The general tool here will be to find a way to to apply eval('some code',:lang<perl5>), so that it returns some Perl 5 code at the appropriate spot. Here's an example of using it to pass a named argument starting with a dash:
use perl5:CGI;
my $cookie = eval(q"
my $q = CGI->new;
$q->cookie( -name => 'Hello', value => 'World')
",:lang<perl5>);
print $cookie;
Likewise, using "{}" for a hash reference may also need some help:
use perl5:CGI;
my $q = CGI.new( eval('{ a => "Hello" }',:lang<perl5>) );
say $q.param('a');
Importing from Perl 5Currently explict imports work, but implicit ones to not. This works: use perl5:Time::gmtime <gmtime>; say gmtime.mday; Examples
|