今天遇到了一個現象,讓我花了好多時間,最後才查清楚怎麼回事,手動查詢資料庫,明明 table 內有資料,程式用 fetchrow_hashref 查詢就是沒資料....
my $ref = $sth->fetchrow_hashref();
print "name=$ref->{'NAME'}, address=$ref->{'ADDR'}\n";
執行結果:
name=, address=
會造成這樣的原因是因為大部分的資料庫,資料表的欄位名稱不分大小寫,而 perl 的 hash ,其 key 的部分是區分大小寫的,在這情況下會帶來不小的困擾,造成上面語法無法取得資料。
為了避免潛在大小寫不匹配的問題,必須透過 NAME_uc 或 NAME_lc 屬性來告知 fetchrow_hashref 強制欄位名稱為大寫或小寫。
mark528 發表在 痞客邦 留言(0) 人氣(179)
不管使用什麼程式語言,常常會遇到要拆解檔案路徑和名稱的情況,有些有提供api使用,有些要自己手動來完成,Perl 則提供了一個好用模組來快速解決這問題。
範例如下:
#!/usr/bin/perl
use strict;
use warnings;
use File::Basename;
my $fullpath='YourFileFullpath';
my ($name, $path, $suffix) = fileparse($fullpath);
print "name=$name\n";
print "path=$path\n";
print "suffix=$suffix\n";
結果:
# On Unix
$fullpath="/foo/bar/baz";
# returns
$name="baz", $path="/foo/bar/", suffix="";
# On Windows
$fullpath='C:\foo\bar\baz';
# returns
$name="baz", $path='C:\foo\bar\', suffix="";
# get file name, It does NOT always return the file name portion of a path as you might expect. To be safe, if you want the file name portion of a path use fileparse() .
$fullpath="/foo/bar/baz";
my $name = basename($fullpath);
#return
$name="baz"
mark528 發表在 痞客邦 留言(0) 人氣(1,927)
#!/usr/bin/perl
use warnings;
use strict;
use Net::FTP;
my $host="yourIP";
my $usr="yourUsr";
my $pass="yourPass";
my $path="/yourPub";
my $newerr=0;
my $ftp = Net::FTP->new($host,Debug =>0) or die "Cannot connect to $host: $@";
$ftp->login($usr,$pass) or die "Cannot login ", $ftp->message;
$ftp->cwd($path) or die "Cannot change working directory ", $ftp->message;
$ftp->get("test.file") or die "get failed ", $ftp->message;
$ftp->quit;
mark528 發表在 痞客邦 留言(0) 人氣(454)
底下說明了Perl如何連接PostgreSQL和操作sql。
事前準備
範例:
1. load module
2. 初始 database handle
3. connect to database
4. 執行SQL
5. 取得結果列數
6. 處理每一row資料
mark528 發表在 痞客邦 留言(0) 人氣(89)
Perl最強項的能力就在於字串的處理能力,不管是比對、擷取或是核心的regular expression(RE),透過這些處理能讓字串變成非常有用的資訊。
mark528 發表在 痞客邦 留言(0) 人氣(8,594)
Perl 的最基本的變數型態,可以是文字,也可以是數字,兩者可以互換。
變數名稱可含有數字、文字、以及下畫線"_",但是它們不可以數字開頭。
mark528 發表在 痞客邦 留言(0) 人氣(1,983)
前言
最好具備有下列條件:
已瞭解 HTML 語法並且寫過程式(C, C++, java...)。
系統已經安裝 Perl 5.x 的環境,Linux 或 Windows 皆可。
mark528 發表在 痞客邦 留言(0) 人氣(1,411)