# 函数多返回值
$v1 = 'abc';
$v2 = 'bcd';
($v3, $v4) = upcase($v1, $v2);
sub upcase{
my @parms = @_;
for (@parms)
{
tr/a-z/A-Z/
}
return wantarray ? @parms : $parms[0];
}
print $v3, $v4;
# 取得数组长度
@a=(1, 2, 3);
my $alen = @a;
print $alen;
@aa = ([1,"111"], [2, '2222'], [3, '3333'], [4, '4444']);
my $aalen = @aa;
print $aalen;
#遍历数组
my @key_words = (
'泰迪',
'哈士奇',
'德牧'
);
foreach my $v (@key_words) {
print $v, "\n";
}
# 操作MySQL
use DBI;
my $dsn = "DBI:mysql:database=db1;host=localhost";
my $user = 'root';
my $password = 'mypassword';
$dbh = DBI->connect($dsn,$user,$password); #连接数据库
$dbh->do("SET NAMES gbk");
my ($dbh,$sth,@ary);
$dbh->do("update test set f1 = 1"); # 修改
$sth = $dbh->prepare("select f1,f2,f3 from test"); #查询
$sth->execute();
while(my(@row) = $sth->fetchrow_array()){
push @ary, [@row];# 取记录到数组
my($f1, $f2, $f3)=@{$row};#取记录到变量
}
$sth->finish;
$dbh->disconnect;
# 创建模块BankAccount.pm, 内容如下:
package BankAccount;
use Exporter;
@ISA=('Exporter');
@EXPORT_OK = ( 'deposit');
$total = 0;
sub deposit {
my($amount) = @_;
$total += $amount;
print "you now have $total dollars \n";
}
return 1;
# 使用模块BankAccount
use BankAccount;
BankAccount::deposit(10);
print $BankAccount::total;
# 操作文本文件
open TXT,">c:/a.txt";
print TXT "test...\n";
close TXT;