瀏覽模式: 普通 | 列表

使用 Getopt::Long

use strict;
use Getopt::Long;
my ($file,$number);
GetOptions (
        "file=s" => \$file,
        "number=i" => \$number,
);

print <<EOF;

file=$file
number=$number

EOF

下了指令

[閱讀全文]

perl 所謂的參照

參照好像很複雜,應該是

我也還沒完全理解,

不過我起麻應該算是小小的用到它了

實際的例子

sub aa {

        my %a = (
                a=>1,
                b=>2,
        );

        return \%a;
}

上面在回傳的時後多了 \

[閱讀全文]

標籤: perl reference ref

perl 的map功用

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 map

perl 關於排序功能

由值排序hash
foreach $value (sort {$coins{$a} cmp $coins{$b} }  keys %coins)
{
    print "$value $coins{$value}";
}
# ref :http://note.tcc.edu.tw/285.html

sort 以 ASCII 順序來將陣列排序

@a =qw(11 1 5 7 2);

@b = sort @a;
print join ',',@b," ";

sort 以數值大小排序

[閱讀全文]

標籤: perl 排序 sort

perl 如何輸入密碼時以星號*顯示

在 Linux 底下可以真的顯示 * 號,但windows要readmode 為2才可讓我能成功enter,不過是enter完後才看到*號
use 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://zh-tw.w3support.net/index.php?db=so&id=701078
# ref: http://forums.devshed.com/perl-programming-6/problem-with-readmode-under-use-term-readkey-462494.html
標籤: perl password

perl 怎麼達到 ls 功能 (續)

續: 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 ls

perl 模組 - package範例

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 的內容

[閱讀全文]

標籤: perl module package