Use another language to call a function: Difference between revisions
Use another language to call a function (view source)
Revision as of 20:31, 14 March 2020
, 4 years agoRename Perl 6 -> Raku, alphabetize, minor clean-up
(added Perl 6 programming solution) |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 1,159:
=={{header|Pascal}}==
See [[Use_another_language_to_call_a_function#Delphi | Delphi]]
=={{header|Perl 6}}==▼
This [https://stackoverflow.com/a/50771971/3386748 SO answer], by JJ Merelo, explained the difficulty on providing a simple solution. So this attempt takes the same approach as PicoLisp by summoning the interpreter at run time.▼
{{trans|PicoLisp}}▼
query.p6▼
<lang perl6>#!/usr/bin/env perl6▼
sub MAIN (Int :l(:len(:$length))) {▼
my Str $String = "Here am I";▼
$*OUT.print: $String if $String.codes ≤ $length▼
}</lang>▼
query.c▼
<lang c>#include<stdio.h>▼
#include<stddef.h>▼
#include<string.h>▼
int Query(char *Data, size_t *Length) {▼
FILE *fp;▼
char buf[64];▼
sprintf(buf, "/home/user/query.p6 --len=%zu", *Length);▼
if (!(fp = popen(buf, "r")))▼
return 0;▼
fgets(Data, *Length, fp);▼
*Length = strlen(Data);▼
return pclose(fp) >= 0 && *Length != 0;▼
}</lang>▼
{{out}}▼
<pre>▼
gcc -Wall -o main main.c query.c▼
./main▼
Here am I▼
</pre>▼
=={{header|Phix}}==
Line 1,384 ⟶ 1,351:
The output is the expected “<tt>Here I am</tt>” line.
(formerly Perl 6)
▲This [https://stackoverflow.com/a/50771971/3386748 SO answer], by JJ Merelo, explained the difficulty on providing a simple solution. So this attempt takes the same approach as PicoLisp by summoning the interpreter at run time.
▲{{trans|PicoLisp}}
▲query.p6
▲<lang perl6>#!/usr/bin/env perl6
▲sub MAIN (Int :l(:len(:$length))) {
▲ my Str $String = "Here am I";
▲ $*OUT.print: $String if $String.codes ≤ $length
▲}</lang>
▲query.c
▲<lang c>#include<stdio.h>
▲#include<stddef.h>
▲#include<string.h>
▲int Query(char *Data, size_t *Length) {
▲ FILE *fp;
▲ char buf[64];
▲ sprintf(buf, "/home/user/query.p6 --len=%zu", *Length);
▲ if (!(fp = popen(buf, "r")))
▲ return 0;
▲ fgets(Data, *Length, fp);
▲ *Length = strlen(Data);
▲ return pclose(fp) >= 0 && *Length != 0;
▲}</lang>
▲{{out}}
▲<pre>
▲gcc -Wall -o main main.c query.c
▲./main
▲Here am I
▲</pre>
=={{header|Ruby}}==
Line 1,683 ⟶ 1,684:
clean:
rm -f calljava main.o query-jni.o Query.class</lang>
=={{header|Tcl}}==
The way you would tackle this problem depends on whether you are working with ‘In’ or ‘Out’ parameters. (It is normal model ‘inout’ parameters as Tcl variables; omitted for brevity.)
|