perl 的 Getopt::Long 模組,在下指令時讓參數得到值
作者:cross 日期:2011-08-06 16:00
perl 所謂的參照
作者:cross 日期:2011-08-02 23:59
perl 的map功用
作者:cross 日期:2011-08-02 23:18
quote : http://note.tcc.edu.tw/217.html
1. 將 qw LIST 中的數值開根號 x10
my @array = map { sqrt($_)*10 } qw/50 60 70 75 91 18 39 66/;2. 將 0-9 平方後丟到 @array中 @array=(0,1,4,9,16,25,36,49,64,81)
my @array = map { $_ ** 2 } (0..9);3. 將清單加上@domain name,注意這裡的 '@note.tcc.edu.tw' 是以單引號括起,否則用雙引號會把 @note 視為陣列
@user= (' axer', 'john', 'peter', 'lee');
@mail = map { $_ . '@note.tcc.edu.tw' } @user;結果 @mail= ( ' axer@note.tcc.edu.tw', 'john@note.tcc.edu.tw', 'peter@note.tcc.edu.tw', 'lee@note.tcc.edu.tw' );
4. 下例map 將@m 陣列中的字串空白移除,@m 本身被改變
@m=(" www", "facility ", " mail ");
map { s/s+//g } @m;
print "@m ";結果 www facility mail
perl 關於排序功能
作者:cross 日期:2011-08-02 22:41
foreach $value (sort {$coins{$a} cmp $coins{$b} } keys %coins)
{
print "$value $coins{$value}";
}
# ref :http://note.tcc.edu.tw/285.html
sort 以 ASCII 順序來將陣列排序
sort 以數值大小排序@a =qw(11 1 5 7 2);
@b = sort @a;
print join ',',@b," ";
perl 如何輸入密碼時以星號*顯示
作者:cross 日期:2011-05-14 19:31
# ref: http://zh-tw.w3support.net/index.php?db=so&id=701078use Term::ReadKey;
my $key = 0;
my $password = "";
print "\nPlease input your password: ";
# Start reading the keys
#ReadMode(4); # Disable the control keys for linux
ReadMode(2); # for Windows
while(ord($key = ReadKey(0)) != 10)
# This will continue until the Enter key is pressed (decimal value of 10)
{
$password = $password.$key;
print "*";
}
ReadMode(0); #Reset the terminal once we are done
print "\n\nYour super secret password is: $password\n";
# ref: http://forums.devshed.com/perl-programming-6/problem-with-readmode-under-use-term-readkey-462494.html
perl 怎麼達到 ls 功能 (續)
作者:cross 日期:2011-04-19 12:05
續: http://ssorc.tw/rewrite.php/read-1010.html
用 opendir 的方式
opendir open_dir , "/path";
# 打開 /path裡面的東西,去除 是 . 開頭的,及名稱是有包含 other的
my @dirs = grep !/^\.|other/,readdir open_dir;
foreach my $dir (@dirs) {
chomp($dir);
print " $dir";
}
相較之下 while比較簡化 !?
perl 模組 - package範例
作者:cross 日期:2011-04-03 22:43
cross.pl 程式內容
#!/usr/bin/perl
use strict;
use lib "/home/cross/test/lib"; # 我把自寫的perl module 寫在/home/cross/test/lib/Cross.pm
# 所以我要用 use lib 引用路徑
use Cross; # 再用 use 去使用 Cross模組my $c = new Cross('cross','yes'); # 名子是 cross,說了 yes
print " my name is ",$c->getname,", say ",$c->getyes; # 取得值
/home/cross/test/lib/Cross.pm 的內容



