從SQL語句:: ::功能文檔:DBD :: CSV:問題與用戶定義的函數
創建用戶定義的函數
...
更復雜的功能,可以利用一些的參數總是自動傳遞給函數。函數總是在@_中接收這些值:
sub FOO {my($ self,$ sth,$ rowhash,@params); }
#!/usr/bin/env perl
use 5.012;
use warnings; use strict;
use DBI;
my $dbh = DBI->connect("DBI:CSV:", undef, undef, { RaiseError => 1, });
my $table = 'wages';
my $array_ref = [ [ 'id', 'number' ],
[ 0, 6900 ],
[ 1, 3200 ],
[ 2, 1800 ], ];
$dbh->do("CREATE TEMP TABLE $table AS import(?)", {}, $array_ref);
sub routine {
my $self = shift;
my $sth = shift;
my $rowhash = shift; #
return $_[0]/30;
};
$dbh->do("CREATE FUNCTION routine");
my $sth = $dbh->prepare("SELECT id, routine(number) AS result FROM $table");
$sth->execute();
$sth->dump_results();
當我嘗試這個,我得到一個錯誤消息:
DBD :: CSV :: ST執行所失敗:未初始化值$使用_ [0]除法(/ )在./so.pl線27
[用於聲明 「選擇ID,例程(號碼)AS結果FROM 」工資「」]在./so.pl線34
當我註釋掉我按預期工作的第三個參數(因爲它看起來如同第三個參數丟失):
#!/usr/bin/env perl
...
sub routine {
my $self = shift;
my $sth = shift;
#my $rowhash = shift;
return $_[0]/30;
};
...
0,230
1,106.667
2,60個
3行
這是一個錯誤?
你也可以嘗試,因爲,不使用$ rowhash,但SEMS像依靠無證件和可能改變行爲。 這個錯誤並不是說你的函數不能正常工作,而是它根據文檔工作。 – MkV 2010-05-09 10:19:18
如果我對這部分文檔的解釋是正確的,那麼同樣適用於內置函數 - 這太遺憾了。 – 2010-05-09 15:17:14