<?xml version="1.0" encoding="UTF-8"?>

<rss version="2.0" xmlns:blogChannel="http://backend.userland.com/blogChannelModule">

<channel>
<title><![CDATA[Perl 6: Using Perl 5 Embedding]]></title>
<link>http://www.perlfoundation.org/perl6/index.cgi?using_perl_5_embedding</link>
<description></description>
<pubDate>Wed, 09 Jul 2008 04:26:39 -0000</pubDate>
<webMaster>synedra@gmail.com</webMaster>
<generator>Socialtext Workspace v2.14.7.2</generator>

<item>
<title><![CDATA[Using Perl 5 Embedding]]></title>
<link>http://www.perlfoundation.org/perl6/index.cgi?using_perl_5_embedding</link>
<description><![CDATA[<div class="wiki">
<p>
Perl 5 embedding is enabled by default in Pugs. This allows to you use some Perl 5 modules from Perl 6.</p>
<h2 id="loading_and_importing_from_perl_5">Loading and importing from Perl 5</h2>
<pre>
use perl5:CGI;
</pre>
<br /><p>
Currently explict imports work, but implicit ones do not. This works:</p>
<pre>
use perl5:Time::gmtime &lt;gmtime&gt;;
say gmtime.mday;
</pre>
<br /><h2 id="object_oriented_classes">Object-Oriented Classes</h2>
<p>
After loading a Perl5 OO clas, you can use Perl 6 OO notation to call methods on it. See CGI examples below.</p>
<h2 id="examples_of_passing_data_to_perl_5">Examples of Passing Data to Perl 5</h2>
<h3 id="quick_reference">Quick Reference</h3>
<table style="border-collapse: collapse;" class="formatter_table">
<tr>
<td style="border: 1px solid black;padding: .2em;">Target Perl5 type</td>
<td style="border: 1px solid black;padding: .2em;">Perl 6 syntax</td>
</tr>
<tr>
<td style="border: 1px solid black;padding: .2em;">Array</td>
<td style="border: 1px solid black;padding: .2em;">@a</td>
</tr>
<tr>
<td style="border: 1px solid black;padding: .2em;">Array Ref</td>
<td style="border: 1px solid black;padding: .2em;">\@a</td>
</tr>
<tr>
<td style="border: 1px solid black;padding: .2em;">Code Ref</td>
<td style="border: 1px solid black;padding: .2em;">&amp;perl6func</td>
</tr>
<tr>
<td style="border: 1px solid black;padding: .2em;">Hash Ref</td>
<td style="border: 1px solid black;padding: .2em;">{...} or \%h</td>
</tr>
<tr>
<td style="border: 1px solid black;padding: .2em;">Hash</td>
<td style="border: 1px solid black;padding: .2em;">%h.kv</td>
</tr>
<tr>
<td style="border: 1px solid black;padding: .2em;">Scalar</td>
<td style="border: 1px solid black;padding: .2em;">'str'</td>
</tr>
<tr>
<td style="border: 1px solid black;padding: .2em;">Scalar Ref</td>
<td style="border: 1px solid black;padding: .2em;">\$str</td>
</tr>
</table>
<br /><h3 id="strings">Strings</h3>
<pre>
 use perl5:CGI;
 my $q = CGI.new('a=Hello');
 say $q.param('a');
</pre>
<br /><h3 id="hash_references">Hash references</h3>
<pre>
 use perl5:CGI;
 my $q = CGI.new( { a =&gt; &quot;Hello&quot; } );
 say $q.param('a');
</pre>
<br /><p>
This works too:</p>
<pre>
 my %h = ( a =&gt; &quot;Hello&quot; );
 use perl5:CGI;
 my $q = CGI.new(\%h);
 say $q.param('a');
</pre>
<br /><h3 id="hashes_and_named_arguments">Hashes and Named Arguments</h3>
<pre>
your_perl5_func(%perl6_hash.kv)
</pre>
<br /><p>
Note, Perl 6 does not allow named arguments to start with a dash but Perl 5 does. <br />
Here's an example of using it to pass a named argument starting with a dash:</p>
<pre>
use perl5:CGI;
my $q = CGI.new;
my $cookie = $q.cookie( %( '-name' =&gt; 'Hello', value =&gt; 'World' ).kv );
print $cookie;
</pre>
<br /><h2 id="code_references">Code references</h2>
<p>
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.</p>
<pre>
sub  plus_one (Int $int) { $int+1 }
my $sub = eval('sub { my $p6_coderef = shift; $p6_coderef-&gt;(3) }', :lang&lt;perl5&gt;);
my $result = $sub(&amp;plus_one);
print $result; # Correctly prints &quot;4&quot; !
</pre>
<br /><h2 id="compiling_code_as_perl_5">Compiling code as Perl 5</h2>
<p>
That final example showed how to use <tt>eval</tt> to compile arbitary code as Perl 5, and have a Perl 5 structure returned:</p>
<pre>
my $perl5_result = eval('some code',:lang&lt;perl5&gt;);
</pre>
<br /><h2 id="examples">Examples</h2>
<ul>
<li><a target="_blank" title="(external link)" href="http://svn.openfoundry.org/pugs/examples/perl5/cpan-upload.pl">cpan-upload.pl<!-- wiki-renamed-hyperlink "cpan=-upload.pl"<http://svn.openfoundry.org/pugs/examples/perl5/cpan=-upload.pl> --></a> is a Perl 6 program that uses several Perl 5 modules</li>
<li><a target="_blank" title="(external link)" href="http://svn.openfoundry.org/pugs/t/perl5/">t/perl5<!-- wiki-renamed-hyperlink "t/perl5"<http://svn.openfoundry.org/pugs/t/perl5/> --></a> is the code in the test suite that tests Perl 5 embedding</li>
</ul>
</div>
]]></description>
<author>sheepmullet@hidden</author>
<guid isPermaLink="true">http://www.perlfoundation.org/perl6/index.cgi?using_perl_5_embedding</guid>
<pubDate>Wed, 09 Jul 2008 04:26:39 -0000</pubDate>
</item>

</channel>
</rss>