CAD CAM EDM DRO - Yahoo Group Archive

Re: Conversational programming in Perl for EMC

Posted by Matt Shaver
on 2000-05-09 21:47:21 UTC
> From: Fred Proctor <frederick.proctor@...>
> Something equivalent can be done in Perl. I have never done this, but I
> see from the O'Reilly Perl book, 2nd Edition, on page 371 that there are
> references to man pages for perlxstut, perlxs, perlguts, and perlcall
> that describe this. XS is a preprocessor that spits out the glue
> routines that tie C++ into Perl.
>
> Rather than waiting for me to never get around to it, this could be a
> plan for Perl-ifying the EMC:
>
> 1. Someone with Perl experience extends Perl with a C++ function that
> just increments its argument, e.g.,
>
> int myfunc(int arg)
> {
> return arg + 1;
> }
>
> and then does the gluing to the string "myfunc" so if you do this in a
> Perl script:
>
> print myfunc(2)
>
> it prints 3. Forgive my Perl syntax.
>
> 2. Once this is done...

OK, it's done.

1. I found that how to do this is described in simple enough terms (even for
me!) in the perlxstut manpage which is available at:

http://www.perl.com/pub/doc/manual/html/pod/perlxstut.html

and by typing 'man perlxstut' at any convenient linux command prompt.

2. I made a scratch directory to work in and then followed the directions in
the manpage, mainly examples 1 and 2. I edited two of the files created by
the h2xs program:

Filename: Mytest.xs

<SOF>
#ifdef __cplusplus
extern "C" {
#endif
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#ifdef __cplusplus
}
#endif


MODULE = Mytest PACKAGE = Mytest

<NOTE> I added the stuff between here and EOF </NOTE>
int
myfunc(arg)
int arg
CODE:
RETVAL = arg +1;
OUTPUT:
RETVAL
<EOF>

Filename: test.pl

<SOF>
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl test.pl'

######################### We start with some black magic to print on failure.

# Change 1..1 below to 1..last_test_to_print .
# (It may become useful if the test is moved to ./t subdirectory.)

BEGIN { $| = 1; print "1..1\n"; }
END {print "not ok 1\n" unless $loaded;}
use Mytest;
$loaded = 1;
print "ok 1\n";

######################### End of black magic.

# Insert your test code below (better if it prints "ok 13"
# (correspondingly "not ok 13") depending on the success of chunk 13
# of the test code):

<NOTE> I added the one line between here and EOF </NOTE>
print "myfunc(2) = ", &Mytest::myfunc(2), "\n";
<EOF>

Running the test per the instructions produced the following output:

<OUTPUT>
[root@LINUX Mytest]# make test
PERL_DL_NONLAZY=1 /usr/bin/perl -I./blib/arch -I./blib/lib
-I/usr/lib/perl5/i386-linux/5.00405 -I/usr/lib/perl5 test.pl
1..1
ok 1
myfunc(2) = 3
</OUTPUT>

So, it works. Now what?

Matt

P.S. You have to wonder if this is going to work for Perl under Windows!
make...?

Discussion Thread

Fred Proctor 2000-05-09 12:42:09 UTC Conversational programming in Perl for EMC Matt Shaver 2000-05-09 21:47:21 UTC Re: Conversational programming in Perl for EMC Ron Ginger 2000-05-10 11:06:09 UTC Re: Conversational programming in Perl for EMC