因需要,要在
VC
中调用
PERL
子程序对文件进行处理。通过看
PERL
说明文档折腾了好长时间,可以运行。现与大家分享,呵呵,希望对大家有所帮助
^ -^
1
在工程文件中加入
perl58.dll
2.
在VC中加入头文件
#include <EXTERN.h>
#include <perl.h>
3
.
void perl_sub(char*a)
{
int retval;
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv(a, 0)));
PUTBACK;
retval=call_pv("readfile",G_ARRAY);
SPAGAIN;
for (int i = 1; i <= retval;++i)
{ int t=POPl;
printf ("Value %d = %d/n", i, t);
}
PUTBACK;
FREETMPS;
LEAVE;
}
int main(int argc, char **argv, char **env)
{
char *args[] = { " ","sub.pl" };
PERL_SYS_INIT3(&argc,&argv,&env);
my_perl = perl_alloc();
perl_construct(my_perl);
perl_parse(my_perl, NULL, argc, args,(char**)NULL);
PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
perl_sub("test.txt");
perl_destruct(my_perl);
perl_free(my_perl);
PERL_SYS_TERM();
int i;
scanf("%d",&i);
}/
调用PERL结束
注:
sub.pl
的内容
sub readfile
{
my($s) = @_;
#my($s) ="test.txt";
open(FH,$s)||die "can't open FH;$!"; #
读取文本文件
my @readstr=<FH>;
#print @readstr;
$filename="22.bin";
open(IN,">:raw",$filename) or die "cannot open $filename !/n"; #open(IN,">>:raw",$filename)
是追加写
seek(IN,0,SEEK_SET);
foreach my $one (@readstr) #
对每行进行处理
{
#chop($one);
my @words3=split(//s+/,$one);
if ($words3[12] eq "
张
")
{$words3[12]=0;}
if ($words3[12] eq "
李
")
{$words3[12]=1;}
#print "/n";
#print @words3;
print IN pack("n16",@words3);
}
#close(IN) or die "cannot close/n";
}
附加:
1
.
VC
中嵌入
PERL
片段
#include
"stdafx.h"
#include
<EXTERN.h>
#include
<perl.h>
static
PerlInterpreter *my_perl;
main (int argc, char **argv, char **env)
{
STRLEN n_a;
char *embedding[] = { "", "-e", "0" };
PERL_SYS_INIT3(&argc,&argv,&env);
my_perl = perl_alloc();
perl_construct( my_perl );
perl_parse(my_perl, NULL, 3, embedding, NULL);
PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
perl_run(my_perl);
// eval_pv(" $a = 3; $a **= 2", TRUE);
eval_pv(" $a = 3; $a **= 2", TRUE);
printf("a = %d/n", SvIV(get_sv("a", FALSE)));
eval_pv("$a = 3.14; $a **= 2", TRUE);
printf("a = %f/n", SvNV(get_sv("a", FALSE)));
eval_pv(" {$a = 'rekcaH lreP rehtonA tsuJ'; $a = reverse($a);}", TRUE);
printf("a = %s/n", SvPV(get_sv("a", FALSE), n_a));
eval_pv("print 'Please enter first number:';$number1 = <STDIN>;chomp $number1;$number2 = <STDIN>;chomp $number2;$sum = $number1 + $number2;", TRUE);
printf("sum = %f/n", SvNV(get_sv("sum", FALSE)));
eval_pv("print 'hello'", TRUE);
// eval_pv("my $today = new Date;$today->setDate( 7, 14, 2000 );print( $today->month() );print( '/n' );$today->print();print( '/n' );", TRUE);
//eval_pv("use strict; $a = 'rekcaH lreP rehtonA tsuJ'; $a = reverse($a);", TRUE);
//printf("a = %s/n", SvPV(get_sv("a", FALSE), n_a));
perl_destruct(my_perl);
perl_free(my_perl);
PERL_SYS_TERM();
int i;
scanf("%d",&i);
}
2
.在
PERL
中嵌入
C
use Inline C;
{
# $a=5;
#$b=19;
@c=(9,19);
print "begin/n";
print "9 + 19 = ", add(@c,"STRING"), "/n";
print "9 - 16 = ", subtract(9, 16), "/n";
print "end/n";
}
__END__
__C__
int add(int a,int b,char* z) {
int words[2]={0,0};
words[0]=a;
words[1]=b;
printf("%s",z);
return words[0]+words[1];
}
int subtract(int x, int y) {
return x - y;
}