Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Perl 6 string can be converted to Perl 5 strings.
  • Loading branch information
pmurias committed Oct 2, 2011
1 parent 7dbc457 commit e1988ad
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
5 changes: 2 additions & 3 deletions examples/perl5.pl
Expand Up @@ -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'");
Expand Down
6 changes: 6 additions & 0 deletions lib/Perl5Interpreter.cs
Expand Up @@ -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));
Expand Down Expand Up @@ -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");
}
Expand Down
3 changes: 3 additions & 0 deletions lib/p5embed.c
Expand Up @@ -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.