Permalink
Browse files

Perl 6 string can be converted to Perl 5 strings.

  • Loading branch information...
1 parent 7dbc457 commit e1988ad77504b0712bcc1960b7e3cc5609f384c1 @pmurias pmurias committed Oct 2, 2011
Showing with 11 additions and 3 deletions.
  1. +2 −3 examples/perl5.pl
  2. +6 −0 lib/Perl5Interpreter.cs
  3. +3 −0 lib/p5embed.c
View
@@ -11,16 +11,15 @@
package Foo;
sub baz {
my ($self,$arg) = @_;
- my $who = $$arg;
- print "Just another $who\n";
+ print "Just another $arg\n";
return 44;
}
sub new {
bless {},"Foo";
}
PERL5
my $foo = eval(:lang<perl5>,'Foo->new');
-say $foo.baz(eval(:lang<perl5>,'\"Perl hacker"'));
+say $foo.baz("Perl hacker");
say eval(:lang<perl5>,"125");
say eval(:lang<perl5>,"13.5");
say eval(:lang<perl5>,"'Hello there'");
View
@@ -30,6 +30,9 @@ public class Perl5Interpreter : IForeignInterpreter {
[DllImport("obj/p5embed.so", EntryPoint="p5embed_SvPOKp")]
public static extern int SvPOKp(IntPtr sv);
+ [DllImport("obj/p5embed.so", EntryPoint="p5embed_newSVpvn")]
+ public static extern IntPtr newSVpvn(string s,int length);
+
public static Variable SVToVariable(IntPtr sv) {
if (SvIOKp(sv) != 0) {
return Builtins.MakeInt(SvIV(sv));
@@ -81,6 +84,9 @@ int argument_n
P6any obj = var.Fetch();
if (obj is SVany) {
return ((SVany)obj).sv;
+ } else if (obj.Does(Kernel.StrMO)) {
+ string s = Kernel.UnboxAny<string>(obj);
+ return Perl5Interpreter.newSVpvn(s,s.Length);
} else {
throw new NieczaException("can't convert argument to p5 type");
}
View
@@ -89,3 +89,6 @@ double p5embed_SvNV(SV* sv) {
char* p5embed_SvPV_nolen(SV* sv) {
return SvPV_nolen(sv);
}
+SV* p5embed_newSVpvn(char* str,int len) {
+ return newSVpvn(str,len);
+}

0 comments on commit e1988ad

Please sign in to comment.