2011-03-21 77 views
2

我正在構建一個模塊,它連接到相機,拍攝照片並將數據讀取到一個piddle中。所有這些都發生在Inline :: C命令中。使用PDL documentation中的程序,我可以創建一個pdl *並將其返回。然而,相機可能無法拍攝照片,在這種情況下,我想按照常規協議my $pic_pdl = $Camera->TakePicture or die "Failed to take image"返回0。這似乎意味着我需要使用Inline_Stack_Push機制,但我不確定如何正確地將pdl *轉換爲SV*。如果可能的話,我也想設置$!以及錯誤代碼。這可以在Inline中完成嗎?Perl Inline :: C失敗時返回pdl或0

+0

你試過只返回NULL嗎? – ikegami 2011-03-21 17:16:20

回答

6

pdl*通過typemap中的代碼轉換爲SV。

$ cat `perl -E'use PDL::Core::Dev; say PDL_TYPEMAP'` 
TYPEMAP 
pdl* T_PDL 
pdl * T_PDL 
Logical T_IV 
float T_NV 

INPUT 

T_PDL 
     $var = PDL->SvPDLV($arg) 


OUTPUT 

T_PDL 
     PDL->SetSV_PDL($arg,$var); 

如果我沒有看錯,你應該能夠做這樣的事情:

SV* my_new { 
    pdl* p = NULL; 

    ... 

    if (error) { 
     if (p) 
      free(p); /* I think */ 
     return &PL_sv_undef; 
    } else { 
     SV* rv = newSV(0); 
     PDL->SetSV_PDL(rv, p); 
     return rv; 
    } 
} 

至於$!,它只是到C的errno的接口。只需設置errno

$ perl -E'use Inline C => "void f(int i) { errno = i; }"; f($ARGV[0]); say 0+$!; say $!;' 2 
2 
No such file or directory 

$ perl -E'use Inline C => "void f(int i) { errno = i; }"; f($ARGV[0]); say 0+$!; say $!;' 3 
3 
No such process 

$ perl -E'use Inline C => "void f(int i) { errno = i; }"; f($ARGV[0]); say 0+$!; say $!;' 4 
4 
Interrupted system call 
+0

你知道,我只是看着你發佈的這個typemap。我有一種感覺,你是對的。感謝「erro」的信息,因爲我不是來自C背景,我不知道。 – 2011-03-21 18:20:41

+1

@Joel,[errno.h](http://linux.die.net/man/3/errno)提供了可用於設置'errno'的可移植常量(例如EACCES拒絕權限)。 – ikegami 2011-03-21 18:29:23