Thread Probleme mit Perl XS (7 answers)
Opened by MechaTikal at 2007-11-26 12:24

MechaTikal
 2007-11-27 12:14
#103102 #103102
User since
2007-10-10
20 Artikel
BenutzerIn
[default_avatar]
Mytest.pm:
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package Mytest;

use 5.008008;
#use strict;
use warnings;
use Errno;
use AutoLoader;
use Carp;

require Exporter;
our @ISA = qw(Exporter);

our %EXPORT_TAGS = ( 'all' => [ qw(

) ] );

our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
#Buch: our @EXPORT_OK = qw/ myfunc /;
our @EXPORT = qw(

);

our $VERSION = '0.01';

require XSLoader;
XSLoader::load('Mytest', $VERSION);

#im Buch so beschrieben, klappt aber nicht
sub myfunc {
my $arg = shift;
return $arg;
}

sub AUTOLOAD {
my $sub = $AUTOLOAD;
(my $constname = $sub) =~ s/.*:://;
my $val =constant($constname);
if ($! != 0) {
if ($! =~ /Invalid/ || $!{EINVAL}) {
$AutoLoader::AUTOLOAD = $sub;
goto &AutoLoader::AUTOLOAD;

}
else {
croak "Your vendor has not defined constant $constname";
}
no strict 'refs';
*$sub = sub () { $val };

}
goto &$sub;

}


1;

__END__
# Below is stub documentation for your module. You'd better edit it!


Mytest.XS:
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include <stdio.h>
#include "ppport.h"
#include <string.h>

MODULE = Mytest PACKAGE = Mytest

int
print_hello_retval()
CODE:
RETVAL = printf("Hello, world!");
OUTPUT:
RETVAL

int
treble(x)
int x
CODE:
RETVAL = 3*x;
OUTPUT:
RETVAL

int
berechne(y)
int y
CODE:
RETVAL = y+22*5-10/2;
OUTPUT:
RETVAL

char *
strconcat(str1, str2, outstr)
char* str1
char* str2
char* outstr = NO_INIT
PREINIT:
STRLEN length;
CODE:
length = strlen(str1) + strlen (str2) +1;
New(0, outstr, length, char);
RETVAL = strconcat(str1, str2, outstr);
OUTPUT:
outstr
RETVAL
CLEANUP:
Safefree( outstr );

View full thread Probleme mit Perl XS