先安裝需用到的 module
#ppm-shell
#install DBI
#install DBD-ODBC
view plain
PERL:
#!/usr/bin/perl -w
use strict; # 使用變數前一定要定義
use DBI; # 使用DBI模組, 連結資料庫
use DBD::ODBC;
my $server='localhost\SQLEXPRESS';
my $database="your_db";
my $user="sa";
my $password="123456";
my $DSN = "driver={SQL Server};Server=$server;database=$database;uid=$user;pwd=$password;";
my $dbh = DBI->connect("dbi:ODBC:$DSN", { RaiseError => 1, AutoCommit => 1 }) || die "Can't connect: $DBI::errstr\n";
# 執行 sql 敘述
my $sql_statement = "select * from user_data";
my $sth = $dbh->prepare($sql_statement) || die "Can't prepare the [...]
view plain
PERL:
sub escape {
my($str) = splice(@_);
$str =~ s/(\W)/sprintf('%%%02X', ord($1))/eg;
return $str;
}
sub unescape {
my($str) = splice(@_);
$str =~ s/%(..)/chr(hex($1))/eg;
return $str;
}
http post file upload example
view plain
CODE:
use LWP::UserAgent;
use HTTP::Request::Common;
my $fname1 = "lastUtt.amr";
my $fname2 = "format.txt";
my $url = "http://10.129.6.228/post3.php";
my $ua = LWP::UserAgent->new();
my $req = POST $url, Content_Type => 'form-data',
Content => [
submit => 1,
uploadedfile => [ $fname1 ],
uploadedfile2 => [ $fname2 ]
];
my $response = $ua->request($req);
if ($response->is_success()) {
print "OK: ", $response->content;
} else {
print $response->as_string;
}
VC DLL實作,calling convention 必需是__stdcall
view plain
CODE:
#include <windows.h>
#include <stdio.h>
int __stdcall GetIntFromVC(int x )
{
x *= 2;
return x;
}
int __stdcall GetStrFromVC(char* pbstr, char *lpBuffer)
{
return sprintf(lpBuffer, "*** %s ***", pbstr);
}
避免 name mangling ,加入 .def
view plain
CODE:
LIBRARY test
EXPORTS
GetIntFromVC @1
GetStrFromVC @2
Perl call dll
view plain
CODE:
#! perl -slw
use strict;
use Win32::API;
#傳入一個參數,回傳一個值 ,N: value is a number (long)
my $fun1 = new Win32::API('test.dll', 'GetIntFromVC', [...]
範例包含 server、client,執行前先使用 ppm 安裝「Frontier-RPC」、「Frontier-Daemon-Forking」modules。
參考「用 XML-RPC 開發 Web 服務: 針對 Perl 的 XML-RPC 入門」
Server
view plain
PERL:
#!/usr/bin/perl
# sum() server
use strict;
use warnings;
use Frontier::Daemon;
my $d = Frontier::Daemon->new(
methods => {
sum => \&sum,
},
LocalAddr => '127.0.0.1',
LocalPort => 1234,
);
sub sum {
my ($arg1, $arg2) = @_;
return $arg1 + $arg2;
}
Client
view plain
PERL:
#!/usr/bin/perl
# Testing sum()
use strict;
use warnings;
use Frontier::Client;
my $url = "http://127.0.0.1:1234/RPC2";
my @args = (2,5);
my $client = Frontier::Client->new( [...]