Perl 6
Using Perl 5 Embedding: Revision 11

Perl 5 embedding is enabled by default in Pugs. This allows to you use some Perl 5 modules from Perl 6.

Loading and importing from Perl 5

use perl5:CGI;

Currently explict imports work, but implicit ones do not. This works:

use perl5:Time::gmtime <gmtime>;
say gmtime.mday;

Object-Oriented Classes

After loading a Perl5 OO clas, you can use Perl 6 OO notation to call methods on it. See CGI examples below.

Examples of Passing Data to Perl 5

Quick Reference

Target Perl5 type Perl 6 syntax
Array @a
Array Ref \@a
Code Ref &perl6func
Hash Ref {...} or \%h
Hash %h.kv
Scalar 'str'
Scalar Ref \$str

Strings

 use perl5:CGI;
 my $q = CGI.new('a=Hello');
 say $q.param('a');

Hash references

 use perl5:CGI;
 my $q = CGI.new( { a => "Hello" } );
 say $q.param('a');

This works too:

 my %h = ( a => "Hello" );
 use perl5:CGI;
 my $q = CGI.new(\%h);
 say $q.param('a');

Hashes and Named Arguments

your_perl5_func(%perl6_hash.kv)

Note, Perl 6 does not allow named arguments to start with a dash but Perl 5 does.
Here's an example of using it to pass a named argument starting with a dash:

use perl5:CGI;
my $q = CGI.new;
my $cookie = $q.cookie( %( '-name' => 'Hello', value => 'World' ).kv );
print $cookie;

Code references

You can even pass a Perl 6 code reference to Perl 5 and pass Perl 5 arguments back into the Perl 6 code ref, and have the resulting thing execute.

sub  plus_one (Int $int) { $int+1 }
my $sub = eval('sub { my $p6_coderef = shift; $p6_coderef->(3) }', :lang<perl5>);
my $result = $sub(&plus_one);
print $result; # Correctly prints "4" !

Compiling code as Perl 5

That final example showed how to use eval to compile arbitary code as Perl 5, and have a Perl 5 structure returned:

my $perl5_result = eval('some code',:lang<perl5>);

Examples

  • cpan-upload.pl is a Perl 6 program that uses several Perl 5 modules
  • t/perl5 is the code in the test suite that tests Perl 5 embedding

Upload Files

Click "Browse" to find the file you want to upload. When you click "Upload file" your file will be uploaded and added to the list of attachments for this page.

Maximum file size: 50MB

 
 
 
File Name Author Date Uploaded Size

Save Page As

Enter a meaningful and distinctive title for your page.

Page Title:

Tip: You'll be able to find this page later by using the title you choose.

Page Already Exists

There is already a page named XXX. Would you like to:

Save with a different name:

Save the page with the name "XXX"

Append your text to the bottom of the existing page named: "XXX"

Upload Files

Click "Browse" to find the file you want to upload. When you click "Add file" this file will be added to the list of attachments for this page, and uploaded when you save the page.

 
 
 
Add Tags

Enter a tag and click "Add tag". The tag will be saved when you save the page.

Tag: 

Suggestions: